explore.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { DEFAULT_RANGE, serializeStateToUrlParam, parseUrlState } from './explore';
  2. import { ExploreState } from 'app/types/explore';
  3. const DEFAULT_EXPLORE_STATE: ExploreState = {
  4. datasource: null,
  5. datasourceError: null,
  6. datasourceLoading: null,
  7. datasourceMissing: false,
  8. datasourceName: '',
  9. exploreDatasources: [],
  10. graphResult: null,
  11. history: [],
  12. latency: 0,
  13. loading: false,
  14. logsResult: null,
  15. queries: [],
  16. queryErrors: [],
  17. queryHints: [],
  18. range: DEFAULT_RANGE,
  19. requestOptions: null,
  20. showingGraph: true,
  21. showingLogs: true,
  22. showingTable: true,
  23. supportsGraph: null,
  24. supportsLogs: null,
  25. supportsTable: null,
  26. tableResult: null,
  27. };
  28. describe('state functions', () => {
  29. describe('parseUrlState', () => {
  30. it('returns default state on empty string', () => {
  31. expect(parseUrlState('')).toMatchObject({
  32. datasource: null,
  33. queries: [],
  34. range: DEFAULT_RANGE,
  35. });
  36. });
  37. });
  38. describe('serializeStateToUrlParam', () => {
  39. it('returns url parameter value for a state object', () => {
  40. const state = {
  41. ...DEFAULT_EXPLORE_STATE,
  42. datasourceName: 'foo',
  43. range: {
  44. from: 'now - 5h',
  45. to: 'now',
  46. },
  47. queries: [
  48. {
  49. query: 'metric{test="a/b"}',
  50. },
  51. {
  52. query: 'super{foo="x/z"}',
  53. },
  54. ],
  55. };
  56. expect(serializeStateToUrlParam(state)).toBe(
  57. '{"datasource":"foo","queries":[{"query":"metric{test=\\"a/b\\"}"},' +
  58. '{"query":"super{foo=\\"x/z\\"}"}],"range":{"from":"now - 5h","to":"now"}}'
  59. );
  60. });
  61. });
  62. describe('interplay', () => {
  63. it('can parse the serialized state into the original state', () => {
  64. const state = {
  65. ...DEFAULT_EXPLORE_STATE,
  66. datasourceName: 'foo',
  67. range: {
  68. from: 'now - 5h',
  69. to: 'now',
  70. },
  71. queries: [
  72. {
  73. query: 'metric{test="a/b"}',
  74. },
  75. {
  76. query: 'super{foo="x/z"}',
  77. },
  78. ],
  79. };
  80. const serialized = serializeStateToUrlParam(state);
  81. const parsed = parseUrlState(serialized);
  82. // Account for datasource vs datasourceName
  83. const { datasource, ...rest } = parsed;
  84. const sameState = {
  85. ...rest,
  86. datasource: DEFAULT_EXPLORE_STATE.datasource,
  87. datasourceName: datasource,
  88. };
  89. expect(state).toMatchObject(sameState);
  90. });
  91. });
  92. });