parseMetricWidgetsQueryParam.spec.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import {parseMetricWidgetsQueryParam} from 'sentry/views/ddm/utils/parseMetricWidgetsQueryParam';
  2. describe('parseMetricWidgetQueryParam', () => {
  3. it('returns undefined for invalid param', () => {
  4. expect(parseMetricWidgetsQueryParam(undefined)).toBe(undefined);
  5. expect(parseMetricWidgetsQueryParam('')).toBe(undefined);
  6. expect(parseMetricWidgetsQueryParam('{}')).toBe(undefined);
  7. expect(parseMetricWidgetsQueryParam('true')).toBe(undefined);
  8. expect(parseMetricWidgetsQueryParam('2')).toBe(undefined);
  9. expect(parseMetricWidgetsQueryParam('"test"')).toBe(undefined);
  10. // empty array is not valid
  11. expect(parseMetricWidgetsQueryParam('[]')).toEqual(undefined);
  12. });
  13. it('returns a single widget', () => {
  14. expect(
  15. parseMetricWidgetsQueryParam(
  16. JSON.stringify([
  17. {
  18. id: 0,
  19. mri: 'd:transactions/duration@millisecond',
  20. op: 'sum',
  21. query: 'test:query',
  22. groupBy: ['dist'],
  23. displayType: 'line',
  24. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  25. powerUserMode: true,
  26. sort: {order: 'asc'},
  27. },
  28. ])
  29. )
  30. ).toEqual([
  31. {
  32. id: 0,
  33. mri: 'd:transactions/duration@millisecond',
  34. op: 'sum',
  35. query: 'test:query',
  36. groupBy: ['dist'],
  37. displayType: 'line',
  38. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  39. powerUserMode: true,
  40. sort: {name: undefined, order: 'asc'},
  41. },
  42. ]);
  43. });
  44. it('returns multiple widgets', () => {
  45. expect(
  46. parseMetricWidgetsQueryParam(
  47. JSON.stringify([
  48. {
  49. id: 0,
  50. mri: 'd:transactions/duration@millisecond',
  51. op: 'sum',
  52. query: 'test:query',
  53. groupBy: ['dist'],
  54. displayType: 'line',
  55. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  56. powerUserMode: true,
  57. sort: {name: 'avg', order: 'desc'},
  58. },
  59. {
  60. id: 1,
  61. mri: 'd:custom/sentry.event_manager.save@second',
  62. op: 'avg',
  63. query: '',
  64. groupBy: ['event_type'],
  65. displayType: 'line',
  66. powerUserMode: false,
  67. focusedSeries: [{seriesName: 'default', groupBy: {event_type: 'default'}}],
  68. sort: {name: 'sum', order: 'asc'},
  69. },
  70. ])
  71. )
  72. ).toEqual([
  73. {
  74. id: 0,
  75. mri: 'd:transactions/duration@millisecond',
  76. op: 'sum',
  77. query: 'test:query',
  78. groupBy: ['dist'],
  79. displayType: 'line',
  80. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  81. powerUserMode: true,
  82. sort: {name: 'avg', order: 'desc'},
  83. },
  84. {
  85. id: 1,
  86. mri: 'd:custom/sentry.event_manager.save@second',
  87. op: 'avg',
  88. query: '',
  89. groupBy: ['event_type'],
  90. displayType: 'line',
  91. powerUserMode: false,
  92. focusedSeries: [{seriesName: 'default', groupBy: {event_type: 'default'}}],
  93. sort: {name: 'sum', order: 'asc'},
  94. },
  95. ]);
  96. });
  97. it('falls back to defaults', () => {
  98. // Missing values
  99. expect(
  100. parseMetricWidgetsQueryParam(
  101. JSON.stringify([
  102. {
  103. mri: 'd:transactions/duration@millisecond',
  104. },
  105. ])
  106. )
  107. ).toEqual([
  108. {
  109. id: 0,
  110. mri: 'd:transactions/duration@millisecond',
  111. op: 'avg',
  112. query: '',
  113. groupBy: [],
  114. displayType: 'line',
  115. focusedSeries: [],
  116. powerUserMode: false,
  117. sort: {name: undefined, order: 'asc'},
  118. },
  119. ]);
  120. // Invalid values
  121. expect(
  122. parseMetricWidgetsQueryParam(
  123. JSON.stringify([
  124. {
  125. id: 'invalid',
  126. mri: 'd:transactions/duration@millisecond',
  127. op: 1,
  128. query: 12,
  129. groupBy: true,
  130. displayType: 'aasfcsdf',
  131. focusedSeries: {},
  132. powerUserMode: 1,
  133. sort: {name: 1, order: 'invalid'},
  134. },
  135. ])
  136. )
  137. ).toEqual([
  138. {
  139. id: 0,
  140. mri: 'd:transactions/duration@millisecond',
  141. op: 'avg',
  142. query: '',
  143. groupBy: [],
  144. displayType: 'line',
  145. focusedSeries: [],
  146. powerUserMode: false,
  147. sort: {name: undefined, order: 'asc'},
  148. },
  149. ]);
  150. });
  151. it('ignores invalid widgets', () => {
  152. expect(
  153. parseMetricWidgetsQueryParam(
  154. JSON.stringify([
  155. {
  156. id: 0,
  157. mri: 'd:transactions/duration@millisecond',
  158. },
  159. {
  160. // Missing MRI
  161. },
  162. {
  163. // Mallformed MRI
  164. mri: 'transactions/duration@millisecond',
  165. },
  166. {
  167. // Duplicate id
  168. id: 0,
  169. mri: 'd:transactions/duration@second',
  170. },
  171. ])
  172. )
  173. ).toEqual([
  174. {
  175. id: 0,
  176. mri: 'd:transactions/duration@millisecond',
  177. op: 'avg',
  178. query: '',
  179. groupBy: [],
  180. displayType: 'line',
  181. focusedSeries: [],
  182. powerUserMode: false,
  183. sort: {order: 'asc'},
  184. },
  185. ]);
  186. });
  187. it('returns undefined if there is no valid widget', () => {
  188. expect(
  189. parseMetricWidgetsQueryParam(
  190. JSON.stringify([
  191. {
  192. // Missing MRI
  193. },
  194. ])
  195. )
  196. ).toBe(undefined);
  197. });
  198. it('handles missing array in array params', () => {
  199. expect(
  200. parseMetricWidgetsQueryParam(
  201. JSON.stringify([
  202. {
  203. id: 0,
  204. mri: 'd:transactions/duration@millisecond',
  205. op: 'sum',
  206. query: 'test:query',
  207. groupBy: 'dist',
  208. displayType: 'line',
  209. focusedSeries: {seriesName: 'default', groupBy: {dist: 'default'}},
  210. powerUserMode: true,
  211. sort: {order: 'asc'},
  212. },
  213. ])
  214. )
  215. ).toEqual([
  216. {
  217. id: 0,
  218. mri: 'd:transactions/duration@millisecond',
  219. op: 'sum',
  220. query: 'test:query',
  221. groupBy: ['dist'],
  222. displayType: 'line',
  223. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  224. powerUserMode: true,
  225. sort: {name: undefined, order: 'asc'},
  226. },
  227. ]);
  228. });
  229. it('adds missing ids', () => {
  230. const widgetWithId = (id: number | undefined) => ({
  231. id,
  232. mri: 'd:transactions/duration@millisecond',
  233. op: 'sum',
  234. query: 'test:query',
  235. groupBy: ['dist'],
  236. displayType: 'line',
  237. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  238. powerUserMode: true,
  239. sort: {name: 'avg', order: 'desc'},
  240. });
  241. expect(
  242. parseMetricWidgetsQueryParam(
  243. JSON.stringify([
  244. widgetWithId(0),
  245. widgetWithId(undefined),
  246. widgetWithId(2),
  247. widgetWithId(undefined),
  248. widgetWithId(3),
  249. ])
  250. )
  251. ).toEqual([
  252. widgetWithId(0),
  253. widgetWithId(1),
  254. widgetWithId(2),
  255. widgetWithId(4),
  256. widgetWithId(3),
  257. ]);
  258. });
  259. it('resets the id of a single widget to 0', () => {
  260. expect(
  261. parseMetricWidgetsQueryParam(
  262. JSON.stringify([
  263. {
  264. id: 5,
  265. mri: 'd:transactions/duration@millisecond',
  266. op: 'sum',
  267. query: 'test:query',
  268. groupBy: ['dist'],
  269. displayType: 'line',
  270. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  271. powerUserMode: true,
  272. sort: {name: 'avg', order: 'desc'},
  273. },
  274. ])
  275. )
  276. ).toEqual([
  277. {
  278. id: 0,
  279. mri: 'd:transactions/duration@millisecond',
  280. op: 'sum',
  281. query: 'test:query',
  282. groupBy: ['dist'],
  283. displayType: 'line',
  284. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  285. powerUserMode: true,
  286. sort: {name: 'avg', order: 'desc'},
  287. },
  288. ]);
  289. });
  290. });