utils.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import {
  2. DashboardDetails,
  3. DisplayType,
  4. Widget,
  5. WidgetType,
  6. } from 'sentry/views/dashboardsV2/types';
  7. import {
  8. constructWidgetFromQuery,
  9. eventViewFromWidget,
  10. flattenErrors,
  11. getDashboardsMEPQueryParams,
  12. getFieldsFromEquations,
  13. getNumEquations,
  14. getWidgetDiscoverUrl,
  15. getWidgetIssueUrl,
  16. hasUnsavedFilterChanges,
  17. isCustomMeasurementWidget,
  18. } from 'sentry/views/dashboardsV2/utils';
  19. describe('Dashboards util', () => {
  20. const selection = {
  21. datetime: {
  22. period: '7d',
  23. utc: null,
  24. start: null,
  25. end: null,
  26. },
  27. environments: [],
  28. projects: [],
  29. };
  30. describe('constructWidgetFromQuery', () => {
  31. let baseQuery;
  32. beforeEach(() => {
  33. baseQuery = {
  34. displayType: 'line',
  35. interval: '5m',
  36. queryConditions: ['title:test', 'event.type:test'],
  37. queryFields: ['count()', 'failure_count()'],
  38. queryAggregates: ['count()', 'failure_count()'],
  39. queryColumns: [],
  40. queryNames: ['1', '2'],
  41. queryOrderby: '',
  42. title: 'Widget Title',
  43. };
  44. });
  45. it('returns a widget when given a valid query', () => {
  46. const widget = constructWidgetFromQuery(baseQuery);
  47. expect(widget?.displayType).toEqual(DisplayType.LINE);
  48. expect(widget?.interval).toEqual('5m');
  49. expect(widget?.title).toEqual('Widget Title');
  50. expect(widget?.queries).toEqual([
  51. {
  52. name: '1',
  53. fields: ['count()', 'failure_count()'],
  54. aggregates: ['count()', 'failure_count()'],
  55. columns: [],
  56. conditions: 'title:test',
  57. orderby: '',
  58. },
  59. {
  60. name: '2',
  61. fields: ['count()', 'failure_count()'],
  62. aggregates: ['count()', 'failure_count()'],
  63. columns: [],
  64. conditions: 'event.type:test',
  65. orderby: '',
  66. },
  67. ]);
  68. expect(widget?.widgetType).toEqual('discover');
  69. });
  70. it('returns undefined if query is missing title', () => {
  71. baseQuery.title = '';
  72. const widget = constructWidgetFromQuery(baseQuery);
  73. expect(widget).toBeUndefined();
  74. });
  75. it('returns undefined if query is missing interval', () => {
  76. baseQuery.interval = '';
  77. const widget = constructWidgetFromQuery(baseQuery);
  78. expect(widget).toBeUndefined();
  79. });
  80. it('returns undefined if query is missing displayType', () => {
  81. baseQuery.displayType = '';
  82. const widget = constructWidgetFromQuery(baseQuery);
  83. expect(widget).toBeUndefined();
  84. });
  85. it('returns a widget when given string fields and conditions', () => {
  86. baseQuery.queryConditions = 'title:test';
  87. baseQuery.queryFields = 'count()';
  88. baseQuery.queryAggregates = 'count()';
  89. const widget = constructWidgetFromQuery(baseQuery);
  90. expect(widget?.displayType).toEqual(DisplayType.LINE);
  91. expect(widget?.interval).toEqual('5m');
  92. expect(widget?.title).toEqual('Widget Title');
  93. expect(widget?.queries).toEqual([
  94. {
  95. name: '1',
  96. fields: ['count()'],
  97. aggregates: ['count()'],
  98. columns: [],
  99. conditions: 'title:test',
  100. orderby: '',
  101. },
  102. ]);
  103. });
  104. });
  105. describe('eventViewFromWidget', () => {
  106. let widget;
  107. beforeEach(() => {
  108. widget = {
  109. title: 'Test Query',
  110. displayType: DisplayType.WORLD_MAP,
  111. widgetType: WidgetType.DISCOVER,
  112. interval: '5m',
  113. queries: [
  114. {
  115. name: '',
  116. conditions: '',
  117. fields: ['count()'],
  118. aggregates: ['count()'],
  119. columns: [],
  120. orderby: '',
  121. },
  122. ],
  123. };
  124. });
  125. it('attaches a geo.country_code condition and field to a World Map widget if it does not already have one', () => {
  126. const eventView = eventViewFromWidget(
  127. widget.title,
  128. widget.queries[0],
  129. selection,
  130. widget.displayType
  131. );
  132. expect(eventView.fields[0].field).toEqual('geo.country_code');
  133. expect(eventView.fields[1].field).toEqual('count()');
  134. expect(eventView.query).toEqual('has:geo.country_code');
  135. });
  136. it('does not attach geo.country_code condition and field to a World Map widget if it already has one', () => {
  137. widget.queries.fields = ['geo.country_code', 'count()'];
  138. widget.conditions = 'has:geo.country_code';
  139. const eventView = eventViewFromWidget(
  140. widget.title,
  141. widget.queries[0],
  142. selection,
  143. widget.displayType
  144. );
  145. expect(eventView.fields[0].field).toEqual('geo.country_code');
  146. expect(eventView.fields[1].field).toEqual('count()');
  147. expect(eventView.query).toEqual('has:geo.country_code');
  148. });
  149. it('handles sorts in function format', () => {
  150. const query = {...widget.queries[0], orderby: '-count()'};
  151. const eventView = eventViewFromWidget(
  152. widget.title,
  153. query,
  154. selection,
  155. widget.displayType
  156. );
  157. expect(eventView.fields[0].field).toEqual('geo.country_code');
  158. expect(eventView.fields[1].field).toEqual('count()');
  159. expect(eventView.query).toEqual('has:geo.country_code');
  160. expect(eventView.sorts).toEqual([{field: 'count', kind: 'desc'}]);
  161. });
  162. });
  163. describe('getFieldsFromEquations', function () {
  164. it('returns a list of fields that includes individual terms of provided equations', () => {
  165. const fields = [
  166. 'equation|(count_if(transaction.duration,greater,300) / count()) * 100',
  167. 'equation|(count_if(transaction.duration,lessOrEquals,300) / count()) * 100',
  168. ];
  169. expect(getFieldsFromEquations(fields)).toEqual(
  170. expect.arrayContaining([
  171. 'count_if(transaction.duration,lessOrEquals,300)',
  172. 'count()',
  173. 'count_if(transaction.duration,greater,300)',
  174. ])
  175. );
  176. });
  177. });
  178. describe('getWidgetDiscoverUrl', function () {
  179. let widget;
  180. beforeEach(() => {
  181. widget = {
  182. title: 'Test Query',
  183. displayType: DisplayType.LINE,
  184. widgetType: WidgetType.DISCOVER,
  185. interval: '5m',
  186. queries: [
  187. {
  188. name: '',
  189. conditions: '',
  190. fields: ['count()'],
  191. aggregates: ['count()'],
  192. columns: [],
  193. orderby: '',
  194. },
  195. ],
  196. };
  197. });
  198. it('returns the discover url of the widget query', () => {
  199. const url = getWidgetDiscoverUrl(widget, selection, TestStubs.Organization());
  200. expect(url).toEqual(
  201. '/organizations/org-slug/discover/results/?field=count%28%29&name=Test%20Query&query=&statsPeriod=7d&yAxis=count%28%29'
  202. );
  203. });
  204. it('returns the discover url of a topn widget query', () => {
  205. widget = {
  206. ...widget,
  207. ...{
  208. displayType: DisplayType.TOP_N,
  209. queries: [
  210. {
  211. name: '',
  212. conditions: 'error.unhandled:true',
  213. fields: ['error.type', 'count()'],
  214. aggregates: ['count()'],
  215. columns: ['error.type'],
  216. orderby: '-count',
  217. },
  218. ],
  219. },
  220. };
  221. const url = getWidgetDiscoverUrl(widget, selection, TestStubs.Organization());
  222. expect(url).toEqual(
  223. '/organizations/org-slug/discover/results/?display=top5&field=error.type&field=count%28%29&name=Test%20Query&query=error.unhandled%3Atrue&sort=-count&statsPeriod=7d&yAxis=count%28%29'
  224. );
  225. });
  226. });
  227. describe('getWidgetIssueUrl', function () {
  228. let widget;
  229. beforeEach(() => {
  230. widget = {
  231. title: 'Test Query',
  232. displayType: DisplayType.TABLE,
  233. widgetType: WidgetType.ISSUE,
  234. interval: '5m',
  235. queries: [
  236. {
  237. name: '',
  238. conditions: 'is:unresolved',
  239. fields: ['events'],
  240. orderby: 'date',
  241. },
  242. ],
  243. };
  244. });
  245. it('returns the issue url of the widget query', () => {
  246. const url = getWidgetIssueUrl(widget, selection, TestStubs.Organization());
  247. expect(url).toEqual(
  248. '/organizations/org-slug/issues/?query=is%3Aunresolved&sort=date&statsPeriod=7d'
  249. );
  250. });
  251. });
  252. describe('flattenErrors', function () {
  253. it('flattens nested errors', () => {
  254. const errorResponse = {
  255. widgets: [
  256. {
  257. title: ['Ensure this field has no more than 3 characters.'],
  258. },
  259. ],
  260. };
  261. expect(flattenErrors(errorResponse, {})).toEqual({
  262. title: 'Ensure this field has no more than 3 characters.',
  263. });
  264. });
  265. it('does not spread error strings', () => {
  266. const errorResponse = 'Dashboard title already taken.';
  267. expect(flattenErrors(errorResponse, {})).toEqual({
  268. error: 'Dashboard title already taken.',
  269. });
  270. });
  271. });
  272. describe('getDashboardsMEPQueryParams', function () {
  273. it('returns correct params if enabled', function () {
  274. expect(getDashboardsMEPQueryParams(true)).toEqual({
  275. dataset: 'metricsEnhanced',
  276. });
  277. });
  278. it('returns empty object if disabled', function () {
  279. expect(getDashboardsMEPQueryParams(false)).toEqual({});
  280. });
  281. });
  282. describe('getNumEquations', function () {
  283. it('returns 0 if there are no equations', function () {
  284. expect(getNumEquations(['count()', 'epm()', 'count_unique(user)'])).toBe(0);
  285. });
  286. it('returns the count of equations if there are multiple', function () {
  287. expect(
  288. getNumEquations([
  289. 'count()',
  290. 'equation|count_unique(user) * 2',
  291. 'count_unique(user)',
  292. 'equation|count_unique(user) * 3',
  293. ])
  294. ).toBe(2);
  295. });
  296. it('returns 0 if the possible equations array is empty', function () {
  297. expect(getNumEquations([])).toBe(0);
  298. });
  299. });
  300. describe('isCustomMeasurementWidget', function () {
  301. it('returns false on a non custom measurement widget', function () {
  302. const widget: Widget = {
  303. title: 'Title',
  304. interval: '5m',
  305. displayType: DisplayType.LINE,
  306. widgetType: WidgetType.DISCOVER,
  307. queries: [
  308. {
  309. conditions: '',
  310. fields: [],
  311. aggregates: ['count()', 'p99(measurements.lcp)'],
  312. columns: [],
  313. name: 'widget',
  314. orderby: '',
  315. },
  316. ],
  317. };
  318. expect(isCustomMeasurementWidget(widget)).toBe(false);
  319. });
  320. it('returns true on a custom measurement widget', function () {
  321. const widget: Widget = {
  322. title: 'Title',
  323. interval: '5m',
  324. displayType: DisplayType.LINE,
  325. widgetType: WidgetType.DISCOVER,
  326. queries: [
  327. {
  328. conditions: '',
  329. fields: [],
  330. aggregates: ['p99(measurements.custom.measurement)'],
  331. columns: [],
  332. name: 'widget',
  333. orderby: '',
  334. },
  335. ],
  336. };
  337. expect(isCustomMeasurementWidget(widget)).toBe(true);
  338. });
  339. });
  340. describe('hasUnsavedFilterChanges', function () {
  341. it('ignores the order of projects', function () {
  342. const initialDashboard = {
  343. projects: [1, 2],
  344. } as DashboardDetails;
  345. const location = {
  346. ...TestStubs.location(),
  347. query: {
  348. project: ['2', '1'],
  349. },
  350. };
  351. expect(hasUnsavedFilterChanges(initialDashboard, location)).toBe(false);
  352. });
  353. it('ignores the order of environments', function () {
  354. const initialDashboard = {
  355. environment: ['alpha', 'beta'],
  356. } as DashboardDetails;
  357. const location = {
  358. ...TestStubs.location(),
  359. query: {
  360. environment: ['beta', 'alpha'],
  361. },
  362. };
  363. expect(hasUnsavedFilterChanges(initialDashboard, location)).toBe(false);
  364. });
  365. it('ignores the order of releases', function () {
  366. const initialDashboard = {
  367. filters: {
  368. release: ['v1', 'v2'],
  369. },
  370. } as DashboardDetails;
  371. expect(
  372. hasUnsavedFilterChanges(initialDashboard, {
  373. ...TestStubs.location(),
  374. query: {
  375. release: ['v2', 'v1'],
  376. },
  377. })
  378. ).toBe(false);
  379. });
  380. });
  381. });