explore.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. graphRange: DEFAULT_RANGE,
  11. history: [],
  12. queries: [],
  13. queryTransactions: [],
  14. range: DEFAULT_RANGE,
  15. showingGraph: true,
  16. showingLogs: true,
  17. showingTable: true,
  18. supportsGraph: null,
  19. supportsLogs: null,
  20. supportsTable: null,
  21. };
  22. describe('state functions', () => {
  23. describe('parseUrlState', () => {
  24. it('returns default state on empty string', () => {
  25. expect(parseUrlState('')).toMatchObject({
  26. datasource: null,
  27. queries: [],
  28. range: DEFAULT_RANGE,
  29. });
  30. });
  31. it('returns a valid Explore state from URL parameter', () => {
  32. const paramValue =
  33. '%7B"datasource":"Local","queries":%5B%7B"query":"metric"%7D%5D,"range":%7B"from":"now-1h","to":"now"%7D%7D';
  34. expect(parseUrlState(paramValue)).toMatchObject({
  35. datasource: 'Local',
  36. queries: [{ query: 'metric' }],
  37. range: {
  38. from: 'now-1h',
  39. to: 'now',
  40. },
  41. });
  42. });
  43. it('returns a valid Explore state from a compact URL parameter', () => {
  44. const paramValue = '%5B"now-1h","now","Local","metric"%5D';
  45. expect(parseUrlState(paramValue)).toMatchObject({
  46. datasource: 'Local',
  47. queries: [{ query: 'metric' }],
  48. range: {
  49. from: 'now-1h',
  50. to: 'now',
  51. },
  52. });
  53. });
  54. });
  55. describe('serializeStateToUrlParam', () => {
  56. it('returns url parameter value for a state object', () => {
  57. const state = {
  58. ...DEFAULT_EXPLORE_STATE,
  59. datasourceName: 'foo',
  60. range: {
  61. from: 'now-5h',
  62. to: 'now',
  63. },
  64. queries: [
  65. {
  66. query: 'metric{test="a/b"}',
  67. },
  68. {
  69. query: 'super{foo="x/z"}',
  70. },
  71. ],
  72. };
  73. expect(serializeStateToUrlParam(state)).toBe(
  74. '{"datasource":"foo","queries":[{"query":"metric{test=\\"a/b\\"}"},' +
  75. '{"query":"super{foo=\\"x/z\\"}"}],"range":{"from":"now-5h","to":"now"}}'
  76. );
  77. });
  78. it('returns url parameter value for a state object', () => {
  79. const state = {
  80. ...DEFAULT_EXPLORE_STATE,
  81. datasourceName: 'foo',
  82. range: {
  83. from: 'now-5h',
  84. to: 'now',
  85. },
  86. queries: [
  87. {
  88. query: 'metric{test="a/b"}',
  89. },
  90. {
  91. query: 'super{foo="x/z"}',
  92. },
  93. ],
  94. };
  95. expect(serializeStateToUrlParam(state, true)).toBe(
  96. '["now-5h","now","foo","metric{test=\\"a/b\\"}","super{foo=\\"x/z\\"}"]'
  97. );
  98. });
  99. });
  100. describe('interplay', () => {
  101. it('can parse the serialized state into the original state', () => {
  102. const state = {
  103. ...DEFAULT_EXPLORE_STATE,
  104. datasourceName: 'foo',
  105. range: {
  106. from: 'now - 5h',
  107. to: 'now',
  108. },
  109. queries: [
  110. {
  111. query: 'metric{test="a/b"}',
  112. },
  113. {
  114. query: 'super{foo="x/z"}',
  115. },
  116. ],
  117. };
  118. const serialized = serializeStateToUrlParam(state);
  119. const parsed = parseUrlState(serialized);
  120. // Account for datasource vs datasourceName
  121. const { datasource, ...rest } = parsed;
  122. const sameState = {
  123. ...rest,
  124. datasource: DEFAULT_EXPLORE_STATE.datasource,
  125. datasourceName: datasource,
  126. };
  127. expect(state).toMatchObject(sameState);
  128. });
  129. });
  130. });