explore.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import {
  2. DEFAULT_RANGE,
  3. serializeStateToUrlParam,
  4. parseUrlState,
  5. updateHistory,
  6. clearHistory,
  7. hasNonEmptyQuery,
  8. } from './explore';
  9. import { ExploreUrlState } from 'app/types/explore';
  10. import store from 'app/core/store';
  11. const DEFAULT_EXPLORE_STATE: ExploreUrlState = {
  12. datasource: null,
  13. queries: [],
  14. range: DEFAULT_RANGE,
  15. };
  16. describe('state functions', () => {
  17. describe('parseUrlState', () => {
  18. it('returns default state on empty string', () => {
  19. expect(parseUrlState('')).toMatchObject({
  20. datasource: null,
  21. queries: [],
  22. range: DEFAULT_RANGE,
  23. });
  24. });
  25. it('returns a valid Explore state from URL parameter', () => {
  26. const paramValue =
  27. '%7B"datasource":"Local","queries":%5B%7B"expr":"metric"%7D%5D,"range":%7B"from":"now-1h","to":"now"%7D%7D';
  28. expect(parseUrlState(paramValue)).toMatchObject({
  29. datasource: 'Local',
  30. queries: [{ expr: 'metric' }],
  31. range: {
  32. from: 'now-1h',
  33. to: 'now',
  34. },
  35. });
  36. });
  37. it('returns a valid Explore state from a compact URL parameter', () => {
  38. const paramValue = '%5B"now-1h","now","Local",%7B"expr":"metric"%7D%5D';
  39. expect(parseUrlState(paramValue)).toMatchObject({
  40. datasource: 'Local',
  41. queries: [{ expr: 'metric' }],
  42. range: {
  43. from: 'now-1h',
  44. to: 'now',
  45. },
  46. });
  47. });
  48. });
  49. describe('serializeStateToUrlParam', () => {
  50. it('returns url parameter value for a state object', () => {
  51. const state = {
  52. ...DEFAULT_EXPLORE_STATE,
  53. datasource: 'foo',
  54. queries: [
  55. {
  56. expr: 'metric{test="a/b"}',
  57. },
  58. {
  59. expr: 'super{foo="x/z"}',
  60. },
  61. ],
  62. range: {
  63. from: 'now-5h',
  64. to: 'now',
  65. },
  66. };
  67. expect(serializeStateToUrlParam(state)).toBe(
  68. '{"datasource":"foo","queries":[{"expr":"metric{test=\\"a/b\\"}"},' +
  69. '{"expr":"super{foo=\\"x/z\\"}"}],"range":{"from":"now-5h","to":"now"}}'
  70. );
  71. });
  72. it('returns url parameter value for a state object', () => {
  73. const state = {
  74. ...DEFAULT_EXPLORE_STATE,
  75. datasource: 'foo',
  76. queries: [
  77. {
  78. expr: 'metric{test="a/b"}',
  79. },
  80. {
  81. expr: 'super{foo="x/z"}',
  82. },
  83. ],
  84. range: {
  85. from: 'now-5h',
  86. to: 'now',
  87. },
  88. };
  89. expect(serializeStateToUrlParam(state, true)).toBe(
  90. '["now-5h","now","foo",{"expr":"metric{test=\\"a/b\\"}"},{"expr":"super{foo=\\"x/z\\"}"}]'
  91. );
  92. });
  93. });
  94. describe('interplay', () => {
  95. it('can parse the serialized state into the original state', () => {
  96. const state = {
  97. ...DEFAULT_EXPLORE_STATE,
  98. datasource: 'foo',
  99. queries: [
  100. {
  101. expr: 'metric{test="a/b"}',
  102. },
  103. {
  104. expr: 'super{foo="x/z"}',
  105. },
  106. ],
  107. range: {
  108. from: 'now - 5h',
  109. to: 'now',
  110. },
  111. };
  112. const serialized = serializeStateToUrlParam(state);
  113. const parsed = parseUrlState(serialized);
  114. expect(state).toMatchObject(parsed);
  115. });
  116. });
  117. });
  118. describe('updateHistory()', () => {
  119. const datasourceId = 'myDatasource';
  120. const key = `grafana.explore.history.${datasourceId}`;
  121. beforeEach(() => {
  122. clearHistory(datasourceId);
  123. expect(store.exists(key)).toBeFalsy();
  124. });
  125. test('should save history item to localStorage', () => {
  126. const expected = [
  127. {
  128. query: { refId: '1', expr: 'metric' },
  129. },
  130. ];
  131. expect(updateHistory([], datasourceId, [{ refId: '1', expr: 'metric' }])).toMatchObject(expected);
  132. expect(store.exists(key)).toBeTruthy();
  133. expect(store.getObject(key)).toMatchObject(expected);
  134. });
  135. });
  136. describe('hasNonEmptyQuery', () => {
  137. test('should return true if one query is non-empty', () => {
  138. expect(hasNonEmptyQuery([{ refId: '1', key: '2', expr: 'foo' }])).toBeTruthy();
  139. });
  140. test('should return false if query is empty', () => {
  141. expect(hasNonEmptyQuery([{ refId: '1', key: '2' }])).toBeFalsy();
  142. });
  143. test('should return false if no queries exist', () => {
  144. expect(hasNonEmptyQuery([])).toBeFalsy();
  145. });
  146. });