dashboard.spec.tsx 17 KB

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