explore.test.ts 9.7 KB

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