parseMetricWidgetsQueryParam.spec.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. mri: 'd:transactions/duration@millisecond',
  19. op: 'sum',
  20. query: 'test:query',
  21. groupBy: ['dist'],
  22. displayType: 'line',
  23. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  24. powerUserMode: true,
  25. sort: {order: 'asc'},
  26. },
  27. ])
  28. )
  29. ).toEqual([
  30. {
  31. mri: 'd:transactions/duration@millisecond',
  32. op: 'sum',
  33. query: 'test:query',
  34. groupBy: ['dist'],
  35. displayType: 'line',
  36. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  37. powerUserMode: true,
  38. sort: {name: undefined, order: 'asc'},
  39. },
  40. ]);
  41. });
  42. it('returns multiple widgets', () => {
  43. expect(
  44. parseMetricWidgetsQueryParam(
  45. JSON.stringify([
  46. {
  47. mri: 'd:transactions/duration@millisecond',
  48. op: 'sum',
  49. query: 'test:query',
  50. groupBy: ['dist'],
  51. displayType: 'line',
  52. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  53. powerUserMode: true,
  54. sort: {name: 'avg', order: 'desc'},
  55. },
  56. {
  57. mri: 'd:custom/sentry.event_manager.save@second',
  58. op: 'avg',
  59. query: '',
  60. groupBy: ['event_type'],
  61. displayType: 'line',
  62. powerUserMode: false,
  63. focusedSeries: [{seriesName: 'default', groupBy: {event_type: 'default'}}],
  64. sort: {name: 'sum', order: 'asc'},
  65. },
  66. ])
  67. )
  68. ).toEqual([
  69. {
  70. mri: 'd:transactions/duration@millisecond',
  71. op: 'sum',
  72. query: 'test:query',
  73. groupBy: ['dist'],
  74. displayType: 'line',
  75. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  76. powerUserMode: true,
  77. sort: {name: 'avg', order: 'desc'},
  78. },
  79. {
  80. mri: 'd:custom/sentry.event_manager.save@second',
  81. op: 'avg',
  82. query: '',
  83. groupBy: ['event_type'],
  84. displayType: 'line',
  85. powerUserMode: false,
  86. focusedSeries: [{seriesName: 'default', groupBy: {event_type: 'default'}}],
  87. sort: {name: 'sum', order: 'asc'},
  88. },
  89. ]);
  90. });
  91. it('falls back to defaults', () => {
  92. // Missing values
  93. expect(
  94. parseMetricWidgetsQueryParam(
  95. JSON.stringify([
  96. {
  97. mri: 'd:transactions/duration@millisecond',
  98. },
  99. ])
  100. )
  101. ).toEqual([
  102. {
  103. mri: 'd:transactions/duration@millisecond',
  104. op: 'avg',
  105. query: '',
  106. groupBy: [],
  107. displayType: 'line',
  108. focusedSeries: [],
  109. powerUserMode: false,
  110. sort: {name: undefined, order: 'asc'},
  111. },
  112. ]);
  113. // Invalid values
  114. expect(
  115. parseMetricWidgetsQueryParam(
  116. JSON.stringify([
  117. {
  118. mri: 'd:transactions/duration@millisecond',
  119. op: 1,
  120. query: 12,
  121. groupBy: true,
  122. displayType: 'aasfcsdf',
  123. focusedSeries: {},
  124. powerUserMode: 1,
  125. sort: {name: 1, order: 'invalid'},
  126. },
  127. ])
  128. )
  129. ).toEqual([
  130. {
  131. mri: 'd:transactions/duration@millisecond',
  132. op: 'avg',
  133. query: '',
  134. groupBy: [],
  135. displayType: 'line',
  136. focusedSeries: [],
  137. powerUserMode: false,
  138. sort: {name: undefined, order: 'asc'},
  139. },
  140. ]);
  141. });
  142. it('ignores invalid widgets', () => {
  143. expect(
  144. parseMetricWidgetsQueryParam(
  145. JSON.stringify([
  146. {
  147. mri: 'd:transactions/duration@millisecond',
  148. },
  149. {
  150. // Missing MRI
  151. },
  152. {
  153. // Mallformed MRI
  154. mri: 'transactions/duration@millisecond',
  155. },
  156. ])
  157. )
  158. ).toEqual([
  159. {
  160. mri: 'd:transactions/duration@millisecond',
  161. op: 'avg',
  162. query: '',
  163. groupBy: [],
  164. displayType: 'line',
  165. focusedSeries: [],
  166. powerUserMode: false,
  167. sort: {order: 'asc'},
  168. },
  169. ]);
  170. });
  171. it('returns undefined if there is no valid widget', () => {
  172. expect(
  173. parseMetricWidgetsQueryParam(
  174. JSON.stringify([
  175. {
  176. // Missing MRI
  177. },
  178. ])
  179. )
  180. ).toBe(undefined);
  181. });
  182. it('handles missing array in array params', () => {
  183. expect(
  184. parseMetricWidgetsQueryParam(
  185. JSON.stringify([
  186. {
  187. mri: 'd:transactions/duration@millisecond',
  188. op: 'sum',
  189. query: 'test:query',
  190. groupBy: 'dist',
  191. displayType: 'line',
  192. focusedSeries: {seriesName: 'default', groupBy: {dist: 'default'}},
  193. powerUserMode: true,
  194. sort: {order: 'asc'},
  195. },
  196. ])
  197. )
  198. ).toEqual([
  199. {
  200. mri: 'd:transactions/duration@millisecond',
  201. op: 'sum',
  202. query: 'test:query',
  203. groupBy: ['dist'],
  204. displayType: 'line',
  205. focusedSeries: [{seriesName: 'default', groupBy: {dist: 'default'}}],
  206. powerUserMode: true,
  207. sort: {name: undefined, order: 'asc'},
  208. },
  209. ]);
  210. });
  211. });