explore.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. it('returns a valid Explore state from URL parameter', () => {
  38. const paramValue =
  39. '%7B"datasource":"Local","queries":%5B%7B"query":"metric"%7D%5D,"range":%7B"from":"now-1h","to":"now"%7D%7D';
  40. expect(parseUrlState(paramValue)).toMatchObject({
  41. datasource: 'Local',
  42. queries: [{ query: 'metric' }],
  43. range: {
  44. from: 'now-1h',
  45. to: 'now',
  46. },
  47. });
  48. });
  49. it('returns a valid Explore state from a compact URL parameter', () => {
  50. const paramValue = '%5B"now-1h","now","Local","metric"%5D';
  51. expect(parseUrlState(paramValue)).toMatchObject({
  52. datasource: 'Local',
  53. queries: [{ query: 'metric' }],
  54. range: {
  55. from: 'now-1h',
  56. to: 'now',
  57. },
  58. });
  59. });
  60. });
  61. describe('serializeStateToUrlParam', () => {
  62. it('returns url parameter value for a state object', () => {
  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. expect(serializeStateToUrlParam(state)).toBe(
  80. '{"datasource":"foo","queries":[{"query":"metric{test=\\"a/b\\"}"},' +
  81. '{"query":"super{foo=\\"x/z\\"}"}],"range":{"from":"now-5h","to":"now"}}'
  82. );
  83. });
  84. it('returns url parameter value for a state object', () => {
  85. const state = {
  86. ...DEFAULT_EXPLORE_STATE,
  87. datasourceName: 'foo',
  88. range: {
  89. from: 'now-5h',
  90. to: 'now',
  91. },
  92. queries: [
  93. {
  94. query: 'metric{test="a/b"}',
  95. },
  96. {
  97. query: 'super{foo="x/z"}',
  98. },
  99. ],
  100. };
  101. expect(serializeStateToUrlParam(state, true)).toBe(
  102. '["now-5h","now","foo","metric{test=\\"a/b\\"}","super{foo=\\"x/z\\"}"]'
  103. );
  104. });
  105. });
  106. describe('interplay', () => {
  107. it('can parse the serialized state into the original state', () => {
  108. const state = {
  109. ...DEFAULT_EXPLORE_STATE,
  110. datasourceName: 'foo',
  111. range: {
  112. from: 'now - 5h',
  113. to: 'now',
  114. },
  115. queries: [
  116. {
  117. query: 'metric{test="a/b"}',
  118. },
  119. {
  120. query: 'super{foo="x/z"}',
  121. },
  122. ],
  123. };
  124. const serialized = serializeStateToUrlParam(state);
  125. const parsed = parseUrlState(serialized);
  126. // Account for datasource vs datasourceName
  127. const { datasource, ...rest } = parsed;
  128. const sameState = {
  129. ...rest,
  130. datasource: DEFAULT_EXPLORE_STATE.datasource,
  131. datasourceName: datasource,
  132. };
  133. expect(state).toMatchObject(sameState);
  134. });
  135. });
  136. });