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