explore.test.ts 5.1 KB

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