useAddToDashboard.spec.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {openAddToDashboardModal} from 'sentry/actionCreators/modal';
  3. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  4. import {useAddToDashboard} from 'sentry/views/explore/hooks/useAddToDashboard';
  5. import {useResultMode} from 'sentry/views/explore/hooks/useResultsMode';
  6. import {useVisualizes} from 'sentry/views/explore/hooks/useVisualizes';
  7. import {ChartType} from 'sentry/views/insights/common/components/chart';
  8. jest.mock('sentry/actionCreators/modal');
  9. jest.mock('sentry/views/explore/hooks/useVisualizes');
  10. jest.mock('sentry/views/explore/hooks/useResultsMode');
  11. function TestPage({visualizeIndex}: {visualizeIndex: number}) {
  12. const {addToDashboard} = useAddToDashboard();
  13. return <button onClick={() => addToDashboard(visualizeIndex)}>Add to Dashboard</button>;
  14. }
  15. describe('AddToDashboardButton', () => {
  16. beforeEach(() => {
  17. jest.clearAllMocks();
  18. jest.mocked(useVisualizes).mockReturnValue([
  19. [
  20. {
  21. yAxes: ['avg(span.duration)'],
  22. chartType: ChartType.LINE,
  23. label: 'Custom Explore Widget',
  24. },
  25. ],
  26. jest.fn(),
  27. ]);
  28. jest.mocked(useResultMode).mockReturnValue(['samples', jest.fn()]);
  29. });
  30. it('opens the dashboard modal with the correct query for samples mode', async () => {
  31. render(<TestPage visualizeIndex={0} />);
  32. await userEvent.click(screen.getByText('Add to Dashboard'));
  33. // The table columns are encoded as the fields for the defaultWidgetQuery
  34. expect(openAddToDashboardModal).toHaveBeenCalledWith(
  35. expect.objectContaining({
  36. // For Add + Stay on Page
  37. widget: {
  38. title: 'Custom Explore Widget',
  39. displayType: DisplayType.LINE,
  40. interval: undefined,
  41. limit: undefined,
  42. widgetType: WidgetType.SPANS,
  43. queries: [
  44. {
  45. aggregates: ['avg(span.duration)'],
  46. columns: [],
  47. fields: ['avg(span.duration)'],
  48. conditions: '',
  49. orderby: '-timestamp',
  50. name: '',
  51. },
  52. ],
  53. },
  54. // For Open in Widget Builder
  55. widgetAsQueryParams: expect.objectContaining({
  56. dataset: WidgetType.SPANS,
  57. defaultTableColumns: [
  58. 'id',
  59. 'project',
  60. 'span.op',
  61. 'span.description',
  62. 'span.duration',
  63. 'timestamp',
  64. ],
  65. defaultTitle: 'Custom Explore Widget',
  66. defaultWidgetQuery:
  67. 'name=&aggregates=avg(span.duration)&columns=&fields=avg(span.duration)&conditions=&orderby=-timestamp',
  68. displayType: DisplayType.LINE,
  69. field: [
  70. 'id',
  71. 'project',
  72. 'span.op',
  73. 'span.description',
  74. 'span.duration',
  75. 'timestamp',
  76. ],
  77. }),
  78. })
  79. );
  80. });
  81. it('opens the dashboard modal with the correct query based on the visualize index', async () => {
  82. // Mock a second visualize object
  83. jest.mocked(useVisualizes).mockReturnValue([
  84. [
  85. {
  86. yAxes: ['avg(span.duration)'],
  87. chartType: ChartType.LINE,
  88. label: 'Custom Explore Widget',
  89. },
  90. {
  91. yAxes: ['max(span.duration)'],
  92. chartType: ChartType.LINE,
  93. label: 'Custom Explore Widget',
  94. },
  95. ],
  96. jest.fn(),
  97. ]);
  98. render(<TestPage visualizeIndex={1} />);
  99. await userEvent.click(screen.getByText('Add to Dashboard'));
  100. // The group by and the yAxes are encoded as the fields for the defaultTableQuery
  101. expect(openAddToDashboardModal).toHaveBeenCalledWith(
  102. expect.objectContaining({
  103. // For Add + Stay on Page
  104. widget: {
  105. title: 'Custom Explore Widget',
  106. displayType: DisplayType.LINE,
  107. interval: undefined,
  108. limit: undefined,
  109. widgetType: WidgetType.SPANS,
  110. queries: [
  111. {
  112. aggregates: ['max(span.duration)'],
  113. columns: [],
  114. fields: ['max(span.duration)'],
  115. conditions: '',
  116. orderby: '-timestamp',
  117. name: '',
  118. },
  119. ],
  120. },
  121. // For Open in Widget Builder
  122. widgetAsQueryParams: expect.objectContaining({
  123. dataset: WidgetType.SPANS,
  124. defaultTableColumns: [
  125. 'id',
  126. 'project',
  127. 'span.op',
  128. 'span.description',
  129. 'span.duration',
  130. 'timestamp',
  131. ],
  132. defaultTitle: 'Custom Explore Widget',
  133. defaultWidgetQuery:
  134. 'name=&aggregates=max(span.duration)&columns=&fields=max(span.duration)&conditions=&orderby=-timestamp',
  135. displayType: DisplayType.LINE,
  136. field: [
  137. 'id',
  138. 'project',
  139. 'span.op',
  140. 'span.description',
  141. 'span.duration',
  142. 'timestamp',
  143. ],
  144. }),
  145. })
  146. );
  147. });
  148. it('uses the yAxes for the aggregate mode', async () => {
  149. jest.mocked(useResultMode).mockReturnValue(['aggregate', jest.fn()]);
  150. render(<TestPage visualizeIndex={0} />);
  151. await userEvent.click(screen.getByText('Add to Dashboard'));
  152. expect(openAddToDashboardModal).toHaveBeenCalledWith(
  153. expect.objectContaining({
  154. // For Add + Stay on Page
  155. widget: {
  156. title: 'Custom Explore Widget',
  157. displayType: DisplayType.LINE,
  158. interval: undefined,
  159. limit: undefined,
  160. widgetType: WidgetType.SPANS,
  161. queries: [
  162. {
  163. aggregates: ['avg(span.duration)'],
  164. columns: [],
  165. fields: ['avg(span.duration)'],
  166. conditions: '',
  167. orderby: '-avg(span.duration)',
  168. name: '',
  169. },
  170. ],
  171. },
  172. // For Open in Widget Builder
  173. widgetAsQueryParams: expect.objectContaining({
  174. dataset: WidgetType.SPANS,
  175. defaultTableColumns: ['avg(span.duration)'],
  176. defaultTitle: 'Custom Explore Widget',
  177. defaultWidgetQuery:
  178. 'name=&aggregates=avg(span.duration)&columns=&fields=avg(span.duration)&conditions=&orderby=-avg(span.duration)',
  179. displayType: DisplayType.LINE,
  180. field: ['avg(span.duration)'],
  181. }),
  182. })
  183. );
  184. });
  185. it('takes the first 3 yAxes', async () => {
  186. jest.mocked(useResultMode).mockReturnValue(['aggregate', jest.fn()]);
  187. jest.mocked(useVisualizes).mockReturnValue([
  188. [
  189. {
  190. yAxes: [
  191. 'avg(span.duration)',
  192. 'max(span.duration)',
  193. 'min(span.duration)',
  194. 'p90(span.duration)',
  195. ],
  196. chartType: ChartType.LINE,
  197. label: 'Custom Explore Widget',
  198. },
  199. ],
  200. jest.fn(),
  201. ]);
  202. render(<TestPage visualizeIndex={0} />);
  203. await userEvent.click(screen.getByText('Add to Dashboard'));
  204. expect(openAddToDashboardModal).toHaveBeenCalledWith(
  205. expect.objectContaining({
  206. // For Add + Stay on Page
  207. widget: {
  208. title: 'Custom Explore Widget',
  209. displayType: DisplayType.LINE,
  210. interval: undefined,
  211. limit: undefined,
  212. widgetType: WidgetType.SPANS,
  213. queries: [
  214. {
  215. aggregates: [
  216. 'avg(span.duration)',
  217. 'max(span.duration)',
  218. 'min(span.duration)',
  219. ],
  220. columns: [],
  221. fields: ['avg(span.duration)', 'max(span.duration)', 'min(span.duration)'],
  222. conditions: '',
  223. orderby: '-avg(span.duration)',
  224. name: '',
  225. },
  226. ],
  227. },
  228. // For Open in Widget Builder
  229. widgetAsQueryParams: expect.objectContaining({
  230. dataset: WidgetType.SPANS,
  231. defaultTableColumns: [
  232. 'avg(span.duration)',
  233. 'max(span.duration)',
  234. 'min(span.duration)',
  235. ],
  236. defaultTitle: 'Custom Explore Widget',
  237. defaultWidgetQuery:
  238. 'name=&aggregates=avg(span.duration)%2Cmax(span.duration)%2Cmin(span.duration)&columns=&fields=avg(span.duration)%2Cmax(span.duration)%2Cmin(span.duration)&conditions=&orderby=-avg(span.duration)',
  239. displayType: DisplayType.LINE,
  240. field: ['avg(span.duration)', 'max(span.duration)', 'min(span.duration)'],
  241. }),
  242. })
  243. );
  244. });
  245. });