useAddToDashboard.spec.tsx 8.3 KB

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