explore.test.ts 2.4 KB

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