explore.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import {
  2. DEFAULT_RANGE,
  3. serializeStateToUrlParam,
  4. parseUrlState,
  5. updateHistory,
  6. clearHistory,
  7. hasNonEmptyQuery,
  8. instanceOfDataQueryError,
  9. getValueWithRefId,
  10. getFirstQueryErrorWithoutRefId,
  11. getRefIds,
  12. } from './explore';
  13. import { ExploreUrlState, ExploreMode } from 'app/types/explore';
  14. import store from 'app/core/store';
  15. import { DataQueryError, LogsDedupStrategy } from '@grafana/ui';
  16. const DEFAULT_EXPLORE_STATE: ExploreUrlState = {
  17. datasource: null,
  18. queries: [],
  19. range: DEFAULT_RANGE,
  20. mode: ExploreMode.Metrics,
  21. ui: {
  22. showingGraph: true,
  23. showingTable: true,
  24. showingLogs: true,
  25. dedupStrategy: LogsDedupStrategy.none,
  26. },
  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"expr":"metric"%7D%5D,"range":%7B"from":"now-1h","to":"now"%7D%7D';
  40. expect(parseUrlState(paramValue)).toMatchObject({
  41. datasource: 'Local',
  42. queries: [{ expr: '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","5m",%7B"expr":"metric"%7D,"ui"%5D';
  51. expect(parseUrlState(paramValue)).toMatchObject({
  52. datasource: 'Local',
  53. queries: [{ expr: '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. datasource: 'foo',
  66. queries: [
  67. {
  68. expr: 'metric{test="a/b"}',
  69. },
  70. {
  71. expr: 'super{foo="x/z"}',
  72. },
  73. ],
  74. range: {
  75. from: 'now-5h',
  76. to: 'now',
  77. },
  78. };
  79. expect(serializeStateToUrlParam(state)).toBe(
  80. '{"datasource":"foo","queries":[{"expr":"metric{test=\\"a/b\\"}"},' +
  81. '{"expr":"super{foo=\\"x/z\\"}"}],"range":{"from":"now-5h","to":"now"},' +
  82. '"mode":"Metrics",' +
  83. '"ui":{"showingGraph":true,"showingTable":true,"showingLogs":true,"dedupStrategy":"none"}}'
  84. );
  85. });
  86. it('returns url parameter value for a state object', () => {
  87. const state = {
  88. ...DEFAULT_EXPLORE_STATE,
  89. datasource: 'foo',
  90. queries: [
  91. {
  92. expr: 'metric{test="a/b"}',
  93. },
  94. {
  95. expr: 'super{foo="x/z"}',
  96. },
  97. ],
  98. range: {
  99. from: 'now-5h',
  100. to: 'now',
  101. },
  102. };
  103. expect(serializeStateToUrlParam(state, true)).toBe(
  104. '["now-5h","now","foo",{"expr":"metric{test=\\"a/b\\"}"},{"expr":"super{foo=\\"x/z\\"}"},{"mode":"Metrics"},{"ui":[true,true,true,"none"]}]'
  105. );
  106. });
  107. });
  108. describe('interplay', () => {
  109. it('can parse the serialized state into the original state', () => {
  110. const state = {
  111. ...DEFAULT_EXPLORE_STATE,
  112. datasource: 'foo',
  113. queries: [
  114. {
  115. expr: 'metric{test="a/b"}',
  116. },
  117. {
  118. expr: 'super{foo="x/z"}',
  119. },
  120. ],
  121. range: {
  122. from: 'now - 5h',
  123. to: 'now',
  124. },
  125. };
  126. const serialized = serializeStateToUrlParam(state);
  127. const parsed = parseUrlState(serialized);
  128. expect(state).toMatchObject(parsed);
  129. });
  130. it('can parse the compact serialized state into the original state', () => {
  131. const state = {
  132. ...DEFAULT_EXPLORE_STATE,
  133. datasource: 'foo',
  134. queries: [
  135. {
  136. expr: 'metric{test="a/b"}',
  137. },
  138. {
  139. expr: 'super{foo="x/z"}',
  140. },
  141. ],
  142. range: {
  143. from: 'now - 5h',
  144. to: 'now',
  145. },
  146. };
  147. const serialized = serializeStateToUrlParam(state, true);
  148. const parsed = parseUrlState(serialized);
  149. expect(state).toMatchObject(parsed);
  150. });
  151. });
  152. });
  153. describe('updateHistory()', () => {
  154. const datasourceId = 'myDatasource';
  155. const key = `grafana.explore.history.${datasourceId}`;
  156. beforeEach(() => {
  157. clearHistory(datasourceId);
  158. expect(store.exists(key)).toBeFalsy();
  159. });
  160. test('should save history item to localStorage', () => {
  161. const expected = [
  162. {
  163. query: { refId: '1', expr: 'metric' },
  164. },
  165. ];
  166. expect(updateHistory([], datasourceId, [{ refId: '1', expr: 'metric' }])).toMatchObject(expected);
  167. expect(store.exists(key)).toBeTruthy();
  168. expect(store.getObject(key)).toMatchObject(expected);
  169. });
  170. });
  171. describe('hasNonEmptyQuery', () => {
  172. test('should return true if one query is non-empty', () => {
  173. expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'explore', expr: 'foo' }])).toBeTruthy();
  174. });
  175. test('should return false if query is empty', () => {
  176. expect(hasNonEmptyQuery([{ refId: '1', key: '2', context: 'panel' }])).toBeFalsy();
  177. });
  178. test('should return false if no queries exist', () => {
  179. expect(hasNonEmptyQuery([])).toBeFalsy();
  180. });
  181. });
  182. describe('instanceOfDataQueryError', () => {
  183. describe('when called with a DataQueryError', () => {
  184. it('then it should return true', () => {
  185. const error: DataQueryError = {
  186. message: 'A message',
  187. status: '200',
  188. statusText: 'Ok',
  189. };
  190. const result = instanceOfDataQueryError(error);
  191. expect(result).toBe(true);
  192. });
  193. });
  194. describe('when called with a non DataQueryError', () => {
  195. it('then it should return false', () => {
  196. const error = {};
  197. const result = instanceOfDataQueryError(error);
  198. expect(result).toBe(false);
  199. });
  200. });
  201. });
  202. describe('hasRefId', () => {
  203. describe('when called with a null value', () => {
  204. it('then it should return null', () => {
  205. const input = null;
  206. const result = getValueWithRefId(input);
  207. expect(result).toBeNull();
  208. });
  209. });
  210. describe('when called with a non object value', () => {
  211. it('then it should return null', () => {
  212. const input = 123;
  213. const result = getValueWithRefId(input);
  214. expect(result).toBeNull();
  215. });
  216. });
  217. describe('when called with an object that has refId', () => {
  218. it('then it should return the object', () => {
  219. const input = { refId: 'A' };
  220. const result = getValueWithRefId(input);
  221. expect(result).toBe(input);
  222. });
  223. });
  224. describe('when called with an array that has refId', () => {
  225. it('then it should return the object', () => {
  226. const input = [123, null, {}, { refId: 'A' }];
  227. const result = getValueWithRefId(input);
  228. expect(result).toBe(input[3]);
  229. });
  230. });
  231. describe('when called with an object that has refId somewhere in the object tree', () => {
  232. it('then it should return the object', () => {
  233. const input: any = { data: [123, null, {}, { series: [123, null, {}, { refId: 'A' }] }] };
  234. const result = getValueWithRefId(input);
  235. expect(result).toBe(input.data[3].series[3]);
  236. });
  237. });
  238. });
  239. describe('getFirstQueryErrorWithoutRefId', () => {
  240. describe('when called with a null value', () => {
  241. it('then it should return null', () => {
  242. const errors: DataQueryError[] = null;
  243. const result = getFirstQueryErrorWithoutRefId(errors);
  244. expect(result).toBeNull();
  245. });
  246. });
  247. describe('when called with an array with only refIds', () => {
  248. it('then it should return undefined', () => {
  249. const errors: DataQueryError[] = [{ refId: 'A' }, { refId: 'B' }];
  250. const result = getFirstQueryErrorWithoutRefId(errors);
  251. expect(result).toBeUndefined();
  252. });
  253. });
  254. describe('when called with an array with and without refIds', () => {
  255. it('then it should return undefined', () => {
  256. const errors: DataQueryError[] = [
  257. { refId: 'A' },
  258. { message: 'A message' },
  259. { refId: 'B' },
  260. { message: 'B message' },
  261. ];
  262. const result = getFirstQueryErrorWithoutRefId(errors);
  263. expect(result).toBe(errors[1]);
  264. });
  265. });
  266. });
  267. describe('getRefIds', () => {
  268. describe('when called with a null value', () => {
  269. it('then it should return empty array', () => {
  270. const input = null;
  271. const result = getRefIds(input);
  272. expect(result).toEqual([]);
  273. });
  274. });
  275. describe('when called with a non object value', () => {
  276. it('then it should return empty array', () => {
  277. const input = 123;
  278. const result = getRefIds(input);
  279. expect(result).toEqual([]);
  280. });
  281. });
  282. describe('when called with an object that has refId', () => {
  283. it('then it should return an array with that refId', () => {
  284. const input = { refId: 'A' };
  285. const result = getRefIds(input);
  286. expect(result).toEqual(['A']);
  287. });
  288. });
  289. describe('when called with an array that has refIds', () => {
  290. it('then it should return an array with unique refIds', () => {
  291. const input = [123, null, {}, { refId: 'A' }, { refId: 'A' }, { refId: 'B' }];
  292. const result = getRefIds(input);
  293. expect(result).toEqual(['A', 'B']);
  294. });
  295. });
  296. describe('when called with an object that has refIds somewhere in the object tree', () => {
  297. it('then it should return return an array with unique refIds', () => {
  298. const input: any = {
  299. data: [
  300. 123,
  301. null,
  302. { refId: 'B', series: [{ refId: 'X' }] },
  303. { refId: 'B' },
  304. {},
  305. { series: [123, null, {}, { refId: 'A' }] },
  306. ],
  307. };
  308. const result = getRefIds(input);
  309. expect(result).toEqual(['B', 'X', 'A']);
  310. });
  311. });
  312. });