dashboard.spec.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {RouterFixture} from 'sentry-fixture/routerFixture';
  4. import {TagsFixture} from 'sentry-fixture/tags';
  5. import {WidgetFixture} from 'sentry-fixture/widget';
  6. import {initializeOrg} from 'sentry-test/initializeOrg';
  7. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  8. import MemberListStore from 'sentry/stores/memberListStore';
  9. import {DatasetSource} from 'sentry/utils/discover/types';
  10. import {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  11. import Dashboard from 'sentry/views/dashboards/dashboard';
  12. import type {Widget} from 'sentry/views/dashboards/types';
  13. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  14. import {OrganizationContext} from '../organizationContext';
  15. import WidgetLegendSelectionState from './widgetLegendSelectionState';
  16. jest.mock('sentry/components/lazyRender', () => ({
  17. LazyRender: ({children}: {children: React.ReactNode}) => children,
  18. }));
  19. describe('Dashboards > Dashboard', () => {
  20. const organization = OrganizationFixture({
  21. features: ['dashboards-basic', 'dashboards-edit'],
  22. });
  23. const mockDashboard = {
  24. dateCreated: '2021-08-10T21:20:46.798237Z',
  25. id: '1',
  26. title: 'Test Dashboard',
  27. widgets: [],
  28. projects: [],
  29. filters: {},
  30. };
  31. const newWidget: Widget = {
  32. id: '1',
  33. title: 'Test Discover Widget',
  34. displayType: DisplayType.LINE,
  35. widgetType: WidgetType.DISCOVER,
  36. interval: '5m',
  37. queries: [
  38. {
  39. name: '',
  40. conditions: '',
  41. fields: ['count()'],
  42. aggregates: ['count()'],
  43. columns: [],
  44. orderby: '',
  45. },
  46. ],
  47. };
  48. const issueWidget: Widget = {
  49. id: '2',
  50. title: 'Test Issue Widget',
  51. displayType: DisplayType.TABLE,
  52. widgetType: WidgetType.ISSUE,
  53. interval: '5m',
  54. queries: [
  55. {
  56. name: '',
  57. conditions: '',
  58. fields: ['title', 'assignee'],
  59. aggregates: [],
  60. columns: ['title', 'assignee'],
  61. orderby: '',
  62. },
  63. ],
  64. };
  65. const widgetLegendState = new WidgetLegendSelectionState({
  66. organization,
  67. dashboard: mockDashboard,
  68. router: RouterFixture(),
  69. location: LocationFixture(),
  70. });
  71. let initialData: ReturnType<typeof initializeOrg>;
  72. let tagsMock: jest.Mock;
  73. beforeEach(() => {
  74. initialData = initializeOrg({organization, router: {}, projects: []});
  75. MockApiClient.addMockResponse({
  76. url: `/organizations/org-slug/dashboards/widgets/`,
  77. method: 'POST',
  78. body: [],
  79. });
  80. MockApiClient.addMockResponse({
  81. url: '/organizations/org-slug/events-stats/',
  82. method: 'GET',
  83. body: [],
  84. });
  85. MockApiClient.addMockResponse({
  86. url: '/organizations/org-slug/issues/',
  87. method: 'GET',
  88. body: [
  89. {
  90. annotations: [],
  91. id: '1',
  92. title: 'Error: Failed',
  93. project: {
  94. id: '3',
  95. },
  96. owners: [
  97. {
  98. type: 'ownershipRule',
  99. owner: 'user:2',
  100. },
  101. ],
  102. },
  103. ],
  104. });
  105. MockApiClient.addMockResponse({
  106. url: '/organizations/org-slug/users/',
  107. method: 'GET',
  108. body: [
  109. {
  110. user: {
  111. id: '2',
  112. name: 'test@sentry.io',
  113. email: 'test@sentry.io',
  114. avatar: {
  115. avatarType: 'letter_avatar',
  116. avatarUuid: null,
  117. },
  118. },
  119. },
  120. ],
  121. });
  122. tagsMock = MockApiClient.addMockResponse({
  123. url: '/organizations/org-slug/tags/',
  124. method: 'GET',
  125. body: TagsFixture(),
  126. });
  127. });
  128. it('fetches tags', () => {
  129. render(
  130. <Dashboard
  131. paramDashboardId="1"
  132. dashboard={mockDashboard}
  133. organization={initialData.organization}
  134. onUpdate={() => undefined}
  135. handleUpdateWidgetList={() => undefined}
  136. handleAddCustomWidget={() => undefined}
  137. router={initialData.router}
  138. location={initialData.router.location}
  139. widgetLimitReached={false}
  140. isEditingDashboard={false}
  141. widgetLegendState={widgetLegendState}
  142. />,
  143. {router: initialData.router}
  144. );
  145. expect(tagsMock).toHaveBeenCalled();
  146. });
  147. it('dashboard adds new widget if component is mounted with newWidget prop', async () => {
  148. const mockHandleAddCustomWidget = jest.fn();
  149. const mockCallbackToUnsetNewWidget = jest.fn();
  150. render(
  151. <Dashboard
  152. paramDashboardId="1"
  153. dashboard={mockDashboard}
  154. organization={initialData.organization}
  155. isEditingDashboard={false}
  156. onUpdate={() => undefined}
  157. handleUpdateWidgetList={() => undefined}
  158. handleAddCustomWidget={mockHandleAddCustomWidget}
  159. router={initialData.router}
  160. location={initialData.router.location}
  161. newWidget={newWidget}
  162. widgetLimitReached={false}
  163. onSetNewWidget={mockCallbackToUnsetNewWidget}
  164. widgetLegendState={widgetLegendState}
  165. />,
  166. {router: initialData.router}
  167. );
  168. await waitFor(() => expect(mockHandleAddCustomWidget).toHaveBeenCalled());
  169. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  170. });
  171. it('dashboard adds new widget if component updated with newWidget prop', async () => {
  172. const mockHandleAddCustomWidget = jest.fn();
  173. const mockCallbackToUnsetNewWidget = jest.fn();
  174. const {rerender} = render(
  175. <Dashboard
  176. paramDashboardId="1"
  177. dashboard={mockDashboard}
  178. organization={initialData.organization}
  179. isEditingDashboard={false}
  180. onUpdate={() => undefined}
  181. handleUpdateWidgetList={() => undefined}
  182. handleAddCustomWidget={mockHandleAddCustomWidget}
  183. router={initialData.router}
  184. location={initialData.router.location}
  185. widgetLimitReached={false}
  186. onSetNewWidget={mockCallbackToUnsetNewWidget}
  187. widgetLegendState={widgetLegendState}
  188. />,
  189. {router: initialData.router}
  190. );
  191. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  192. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  193. // Re-render with newWidget prop
  194. rerender(
  195. <Dashboard
  196. paramDashboardId="1"
  197. dashboard={mockDashboard}
  198. organization={initialData.organization}
  199. isEditingDashboard={false}
  200. onUpdate={() => undefined}
  201. handleUpdateWidgetList={() => undefined}
  202. handleAddCustomWidget={mockHandleAddCustomWidget}
  203. router={initialData.router}
  204. location={initialData.router.location}
  205. widgetLimitReached={false}
  206. onSetNewWidget={mockCallbackToUnsetNewWidget}
  207. newWidget={newWidget}
  208. widgetLegendState={widgetLegendState}
  209. />
  210. );
  211. await waitFor(() => expect(mockHandleAddCustomWidget).toHaveBeenCalled());
  212. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  213. });
  214. it('dashboard does not try to add new widget if no newWidget', () => {
  215. const mockHandleAddCustomWidget = jest.fn();
  216. const mockCallbackToUnsetNewWidget = jest.fn();
  217. render(
  218. <Dashboard
  219. paramDashboardId="1"
  220. dashboard={mockDashboard}
  221. organization={initialData.organization}
  222. isEditingDashboard={false}
  223. onUpdate={() => undefined}
  224. handleUpdateWidgetList={() => undefined}
  225. handleAddCustomWidget={mockHandleAddCustomWidget}
  226. router={initialData.router}
  227. location={initialData.router.location}
  228. widgetLimitReached={false}
  229. onSetNewWidget={mockCallbackToUnsetNewWidget}
  230. widgetLegendState={widgetLegendState}
  231. />,
  232. {router: initialData.router}
  233. );
  234. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  235. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  236. });
  237. it('updates the widget dataset split', async () => {
  238. const splitWidget = {
  239. ...newWidget,
  240. widgetType: WidgetType.ERRORS,
  241. datasetSource: DatasetSource.FORCED,
  242. };
  243. const splitWidgets = [splitWidget];
  244. const dashboardWithOneWidget = {...mockDashboard, widgets: splitWidgets};
  245. const mockOnUpdate = jest.fn();
  246. const mockHandleUpdateWidgetList = jest.fn();
  247. render(
  248. <OrganizationContext.Provider value={initialData.organization}>
  249. <MEPSettingProvider forceTransactions={false}>
  250. <Dashboard
  251. paramDashboardId="1"
  252. dashboard={dashboardWithOneWidget}
  253. organization={initialData.organization}
  254. isEditingDashboard={false}
  255. onUpdate={mockOnUpdate}
  256. handleUpdateWidgetList={mockHandleUpdateWidgetList}
  257. handleAddCustomWidget={() => undefined}
  258. router={initialData.router}
  259. location={initialData.router.location}
  260. widgetLimitReached={false}
  261. onSetNewWidget={() => undefined}
  262. widgetLegendState={widgetLegendState}
  263. />
  264. </MEPSettingProvider>
  265. </OrganizationContext.Provider>
  266. );
  267. await userEvent.hover(screen.getByLabelText('Widget warnings'));
  268. expect(
  269. await screen.findByText(/We're splitting our datasets up/)
  270. ).toBeInTheDocument();
  271. await userEvent.click(await screen.findByText(/Switch to Transactions/));
  272. await waitFor(() => {
  273. expect(mockOnUpdate).toHaveBeenCalled();
  274. expect(mockHandleUpdateWidgetList).toHaveBeenCalled();
  275. });
  276. });
  277. it('handles duplicate widget in view mode', async () => {
  278. const mockOnUpdate = jest.fn();
  279. const mockHandleUpdateWidgetList = jest.fn();
  280. const dashboardWithOneWidget = {
  281. ...mockDashboard,
  282. widgets: [
  283. WidgetFixture({
  284. id: '1',
  285. layout: {
  286. h: 1,
  287. w: 1,
  288. x: 0,
  289. y: 0,
  290. minH: 1,
  291. },
  292. }),
  293. ],
  294. };
  295. render(
  296. <OrganizationContext.Provider value={initialData.organization}>
  297. <MEPSettingProvider forceTransactions={false}>
  298. <Dashboard
  299. paramDashboardId="1"
  300. dashboard={dashboardWithOneWidget}
  301. organization={initialData.organization}
  302. isEditingDashboard={false}
  303. onUpdate={mockOnUpdate}
  304. handleUpdateWidgetList={mockHandleUpdateWidgetList}
  305. handleAddCustomWidget={() => undefined}
  306. router={initialData.router}
  307. location={initialData.router.location}
  308. widgetLimitReached={false}
  309. onSetNewWidget={() => undefined}
  310. widgetLegendState={widgetLegendState}
  311. />
  312. </MEPSettingProvider>
  313. </OrganizationContext.Provider>
  314. );
  315. await userEvent.click(await screen.findByLabelText('Widget actions'));
  316. await userEvent.click(await screen.findByText('Duplicate Widget'));
  317. // The new widget is inserted before the duplicated widget
  318. const expectedWidgets = [
  319. // New Widget
  320. expect.objectContaining(
  321. WidgetFixture({
  322. id: undefined,
  323. layout: expect.objectContaining({h: 1, w: 1, x: 0, y: 0, minH: 1}),
  324. })
  325. ),
  326. // Duplicated Widget
  327. expect.objectContaining(
  328. WidgetFixture({
  329. id: '1',
  330. layout: expect.objectContaining({h: 1, w: 1, x: 0, y: 1, minH: 1}),
  331. })
  332. ),
  333. ];
  334. expect(mockHandleUpdateWidgetList).toHaveBeenCalledWith(expectedWidgets);
  335. expect(mockOnUpdate).toHaveBeenCalledWith(expectedWidgets);
  336. });
  337. describe('Issue Widgets', () => {
  338. beforeEach(() => {
  339. MemberListStore.init();
  340. });
  341. const mount = (dashboard, mockedOrg = initialData.organization) => {
  342. render(
  343. <OrganizationContext.Provider value={initialData.organization}>
  344. <MEPSettingProvider forceTransactions={false}>
  345. <Dashboard
  346. paramDashboardId="1"
  347. dashboard={dashboard}
  348. organization={mockedOrg}
  349. isEditingDashboard={false}
  350. onUpdate={() => undefined}
  351. handleUpdateWidgetList={() => undefined}
  352. handleAddCustomWidget={() => undefined}
  353. router={initialData.router}
  354. location={initialData.router.location}
  355. widgetLimitReached={false}
  356. widgetLegendState={widgetLegendState}
  357. />
  358. </MEPSettingProvider>
  359. </OrganizationContext.Provider>
  360. );
  361. };
  362. it('dashboard displays issue widgets if the user has issue widgets feature flag', async () => {
  363. const mockDashboardWithIssueWidget = {
  364. ...mockDashboard,
  365. widgets: [newWidget, issueWidget],
  366. };
  367. mount(mockDashboardWithIssueWidget, organization);
  368. expect(await screen.findByText('Test Discover Widget')).toBeInTheDocument();
  369. expect(screen.getByText('Test Issue Widget')).toBeInTheDocument();
  370. });
  371. it('renders suggested assignees', async () => {
  372. const mockDashboardWithIssueWidget = {
  373. ...mockDashboard,
  374. widgets: [{...issueWidget}],
  375. };
  376. mount(mockDashboardWithIssueWidget, organization);
  377. expect(await screen.findByText('T')).toBeInTheDocument();
  378. await userEvent.hover(screen.getByText('T'));
  379. expect(await screen.findByText('Suggestion: test@sentry.io')).toBeInTheDocument();
  380. expect(screen.getByText('Matching Issue Owners Rule')).toBeInTheDocument();
  381. });
  382. });
  383. describe('Edit mode', () => {
  384. let widgets: Widget[];
  385. const mount = ({
  386. dashboard,
  387. org = initialData.organization,
  388. router = initialData.router,
  389. location = initialData.router.location,
  390. isPreview = false,
  391. }) => {
  392. const getDashboardComponent = () => (
  393. <OrganizationContext.Provider value={initialData.organization}>
  394. <MEPSettingProvider forceTransactions={false}>
  395. <Dashboard
  396. paramDashboardId="1"
  397. dashboard={dashboard}
  398. organization={org}
  399. isEditingDashboard
  400. onUpdate={newWidgets => {
  401. widgets.splice(0, widgets.length, ...newWidgets);
  402. }}
  403. handleUpdateWidgetList={() => undefined}
  404. handleAddCustomWidget={() => undefined}
  405. router={router}
  406. location={location}
  407. widgetLimitReached={false}
  408. isPreview={isPreview}
  409. widgetLegendState={widgetLegendState}
  410. />
  411. </MEPSettingProvider>
  412. </OrganizationContext.Provider>
  413. );
  414. const {rerender} = render(getDashboardComponent());
  415. return {rerender: () => rerender(getDashboardComponent())};
  416. };
  417. beforeEach(() => {
  418. widgets = [newWidget];
  419. });
  420. it('displays the copy widget button in edit mode', async () => {
  421. const dashboardWithOneWidget = {...mockDashboard, widgets};
  422. mount({dashboard: dashboardWithOneWidget});
  423. expect(await screen.findByLabelText('Duplicate Widget')).toBeInTheDocument();
  424. });
  425. it('duplicates the widget', async () => {
  426. const dashboardWithOneWidget = {...mockDashboard, widgets};
  427. const {rerender} = mount({dashboard: dashboardWithOneWidget});
  428. await userEvent.click(await screen.findByLabelText('Duplicate Widget'));
  429. rerender();
  430. await waitFor(() => {
  431. expect(screen.getAllByText('Test Discover Widget')).toHaveLength(2);
  432. });
  433. });
  434. it('opens the widget builder when editing with the modal access flag', async function () {
  435. const testData = initializeOrg({
  436. organization: {
  437. features: ['dashboards-basic', 'dashboards-edit'],
  438. },
  439. });
  440. const dashboardWithOneWidget = {
  441. ...mockDashboard,
  442. widgets: [newWidget],
  443. };
  444. mount({
  445. dashboard: dashboardWithOneWidget,
  446. org: testData.organization,
  447. router: testData.router,
  448. location: testData.router.location,
  449. });
  450. await userEvent.click(await screen.findByLabelText('Edit Widget'));
  451. expect(testData.router.push).toHaveBeenCalledWith(
  452. expect.objectContaining({
  453. pathname: '/organizations/org-slug/dashboard/1/widget/0/edit/',
  454. })
  455. );
  456. });
  457. it('does not show the add widget button if dashboard is in preview mode', async function () {
  458. const testData = initializeOrg({
  459. organization: {
  460. features: ['dashboards-basic', 'dashboards-edit', 'custom-metrics'],
  461. },
  462. });
  463. const dashboardWithOneWidget = {
  464. ...mockDashboard,
  465. widgets: [newWidget],
  466. };
  467. mount({
  468. dashboard: dashboardWithOneWidget,
  469. org: testData.organization,
  470. isPreview: true,
  471. });
  472. await screen.findByText('Test Discover Widget');
  473. expect(screen.queryByRole('button', {name: /add widget/i})).not.toBeInTheDocument();
  474. });
  475. });
  476. });