dashboard.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import MemberListStore from 'sentry/stores/memberListStore';
  4. import {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  5. import Dashboard from 'sentry/views/dashboardsV2/dashboard';
  6. import {DisplayType, Widget, WidgetType} from 'sentry/views/dashboardsV2/types';
  7. import {OrganizationContext} from '../organizationContext';
  8. describe('Dashboards > Dashboard', () => {
  9. const organization = TestStubs.Organization({
  10. features: ['dashboards-basic', 'dashboards-edit', 'dashboard-grid-layout'],
  11. });
  12. const organizationWithFlag = TestStubs.Organization({
  13. features: ['dashboards-basic', 'dashboards-edit', 'dashboard-grid-layout'],
  14. });
  15. const mockDashboard = {
  16. dateCreated: '2021-08-10T21:20:46.798237Z',
  17. id: '1',
  18. title: 'Test Dashboard',
  19. widgets: [],
  20. projects: [],
  21. filters: {},
  22. };
  23. const newWidget: Widget = {
  24. id: '1',
  25. title: 'Test Discover Widget',
  26. displayType: DisplayType.LINE,
  27. widgetType: WidgetType.DISCOVER,
  28. interval: '5m',
  29. queries: [
  30. {
  31. name: '',
  32. conditions: '',
  33. fields: ['count()'],
  34. aggregates: ['count()'],
  35. columns: [],
  36. orderby: '',
  37. },
  38. ],
  39. };
  40. const issueWidget: Widget = {
  41. id: '2',
  42. title: 'Test Issue Widget',
  43. displayType: DisplayType.TABLE,
  44. widgetType: WidgetType.ISSUE,
  45. interval: '5m',
  46. queries: [
  47. {
  48. name: '',
  49. conditions: '',
  50. fields: ['title', 'assignee'],
  51. aggregates: [],
  52. columns: ['title', 'assignee'],
  53. orderby: '',
  54. },
  55. ],
  56. };
  57. let initialData, tagsMock;
  58. beforeEach(() => {
  59. initialData = initializeOrg({organization, router: {}, project: 1, projects: []});
  60. MockApiClient.addMockResponse({
  61. url: `/organizations/org-slug/dashboards/widgets/`,
  62. method: 'POST',
  63. body: [],
  64. });
  65. MockApiClient.addMockResponse({
  66. url: '/organizations/org-slug/events-stats/',
  67. method: 'GET',
  68. body: [],
  69. });
  70. MockApiClient.addMockResponse({
  71. url: '/organizations/org-slug/issues/',
  72. method: 'GET',
  73. body: [
  74. {
  75. annotations: [],
  76. id: '1',
  77. title: 'Error: Failed',
  78. project: {
  79. id: '3',
  80. },
  81. owners: [
  82. {
  83. type: 'ownershipRule',
  84. owner: 'user:2',
  85. },
  86. ],
  87. },
  88. ],
  89. });
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/users/',
  92. method: 'GET',
  93. body: [
  94. {
  95. user: {
  96. id: '2',
  97. name: 'test@sentry.io',
  98. email: 'test@sentry.io',
  99. avatar: {
  100. avatarType: 'letter_avatar',
  101. avatarUuid: null,
  102. },
  103. },
  104. },
  105. ],
  106. });
  107. tagsMock = MockApiClient.addMockResponse({
  108. url: '/organizations/org-slug/tags/',
  109. method: 'GET',
  110. body: TestStubs.Tags(),
  111. });
  112. });
  113. it('fetches tags', () => {
  114. render(
  115. <Dashboard
  116. paramDashboardId="1"
  117. dashboard={mockDashboard}
  118. organization={initialData.organization}
  119. onUpdate={() => undefined}
  120. handleUpdateWidgetList={() => undefined}
  121. handleAddCustomWidget={() => undefined}
  122. router={initialData.router}
  123. location={initialData.router.location}
  124. widgetLimitReached={false}
  125. isEditing={false}
  126. />,
  127. {context: initialData.routerContext}
  128. );
  129. expect(tagsMock).toHaveBeenCalled();
  130. });
  131. it('dashboard adds new widget if component is mounted with newWidget prop', async () => {
  132. const mockHandleAddCustomWidget = jest.fn();
  133. const mockCallbackToUnsetNewWidget = jest.fn();
  134. render(
  135. <Dashboard
  136. paramDashboardId="1"
  137. dashboard={mockDashboard}
  138. organization={initialData.organization}
  139. isEditing={false}
  140. onUpdate={() => undefined}
  141. handleUpdateWidgetList={() => undefined}
  142. handleAddCustomWidget={mockHandleAddCustomWidget}
  143. router={initialData.router}
  144. location={initialData.router.location}
  145. newWidget={newWidget}
  146. widgetLimitReached={false}
  147. onSetNewWidget={mockCallbackToUnsetNewWidget}
  148. />,
  149. {context: initialData.routerContext}
  150. );
  151. await waitFor(() => expect(mockHandleAddCustomWidget).toHaveBeenCalled());
  152. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  153. });
  154. it('dashboard adds new widget if component updated with newWidget prop', async () => {
  155. const mockHandleAddCustomWidget = jest.fn();
  156. const mockCallbackToUnsetNewWidget = jest.fn();
  157. const {rerender} = render(
  158. <Dashboard
  159. paramDashboardId="1"
  160. dashboard={mockDashboard}
  161. organization={initialData.organization}
  162. isEditing={false}
  163. onUpdate={() => undefined}
  164. handleUpdateWidgetList={() => undefined}
  165. handleAddCustomWidget={mockHandleAddCustomWidget}
  166. router={initialData.router}
  167. location={initialData.router.location}
  168. widgetLimitReached={false}
  169. onSetNewWidget={mockCallbackToUnsetNewWidget}
  170. />,
  171. {context: initialData.routerContext}
  172. );
  173. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  174. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  175. // Re-render with newWidget prop
  176. rerender(
  177. <Dashboard
  178. paramDashboardId="1"
  179. dashboard={mockDashboard}
  180. organization={initialData.organization}
  181. isEditing={false}
  182. onUpdate={() => undefined}
  183. handleUpdateWidgetList={() => undefined}
  184. handleAddCustomWidget={mockHandleAddCustomWidget}
  185. router={initialData.router}
  186. location={initialData.router.location}
  187. widgetLimitReached={false}
  188. onSetNewWidget={mockCallbackToUnsetNewWidget}
  189. newWidget={newWidget}
  190. />
  191. );
  192. await waitFor(() => expect(mockHandleAddCustomWidget).toHaveBeenCalled());
  193. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  194. });
  195. it('dashboard does not try to add new widget if no newWidget', () => {
  196. const mockHandleAddCustomWidget = jest.fn();
  197. const mockCallbackToUnsetNewWidget = jest.fn();
  198. render(
  199. <Dashboard
  200. paramDashboardId="1"
  201. dashboard={mockDashboard}
  202. organization={initialData.organization}
  203. isEditing={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. />,
  212. {context: initialData.routerContext}
  213. );
  214. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  215. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  216. });
  217. describe('Issue Widgets', () => {
  218. beforeEach(() => {
  219. MemberListStore.init();
  220. });
  221. const mount = (dashboard, mockedOrg = initialData.organization) => {
  222. render(
  223. <OrganizationContext.Provider value={initialData.organization}>
  224. <MEPSettingProvider forceTransactions={false}>
  225. <Dashboard
  226. paramDashboardId="1"
  227. dashboard={dashboard}
  228. organization={mockedOrg}
  229. isEditing={false}
  230. onUpdate={() => undefined}
  231. handleUpdateWidgetList={() => undefined}
  232. handleAddCustomWidget={() => undefined}
  233. router={initialData.router}
  234. location={initialData.router.location}
  235. widgetLimitReached={false}
  236. />
  237. </MEPSettingProvider>
  238. </OrganizationContext.Provider>
  239. );
  240. };
  241. it('dashboard displays issue widgets if the user has issue widgets feature flag', async () => {
  242. const mockDashboardWithIssueWidget = {
  243. ...mockDashboard,
  244. widgets: [newWidget, issueWidget],
  245. };
  246. await act(async () => {
  247. mount(mockDashboardWithIssueWidget, organizationWithFlag);
  248. await tick();
  249. });
  250. expect(screen.getByText('Test Discover Widget')).toBeInTheDocument();
  251. expect(screen.getByText('Test Issue Widget')).toBeInTheDocument();
  252. });
  253. it('renders suggested assignees', async () => {
  254. const mockDashboardWithIssueWidget = {
  255. ...mockDashboard,
  256. widgets: [{...issueWidget}],
  257. };
  258. mount(mockDashboardWithIssueWidget, organizationWithFlag);
  259. expect(await screen.findByText('T')).toBeInTheDocument();
  260. userEvent.hover(screen.getByText('T'));
  261. expect(await screen.findByText('Suggestion:')).toBeInTheDocument();
  262. expect(screen.getByText('test@sentry.io')).toBeInTheDocument();
  263. expect(screen.getByText('Matching Issue Owners Rule')).toBeInTheDocument();
  264. });
  265. });
  266. describe('Edit mode', () => {
  267. let widgets: Widget[];
  268. const mount = (
  269. dashboard,
  270. mockedOrg = initialData.organization,
  271. mockedRouter = initialData.router,
  272. mockedLocation = initialData.router.location
  273. ) => {
  274. const getDashboardComponent = () => (
  275. <OrganizationContext.Provider value={initialData.organization}>
  276. <MEPSettingProvider forceTransactions={false}>
  277. <Dashboard
  278. paramDashboardId="1"
  279. dashboard={dashboard}
  280. organization={mockedOrg}
  281. isEditing
  282. onUpdate={newWidgets => {
  283. widgets.splice(0, widgets.length, ...newWidgets);
  284. }}
  285. handleUpdateWidgetList={() => undefined}
  286. handleAddCustomWidget={() => undefined}
  287. router={mockedRouter}
  288. location={mockedLocation}
  289. widgetLimitReached={false}
  290. />
  291. </MEPSettingProvider>
  292. </OrganizationContext.Provider>
  293. );
  294. const {rerender} = render(getDashboardComponent());
  295. return {rerender: () => rerender(getDashboardComponent())};
  296. };
  297. beforeEach(() => {
  298. widgets = [newWidget];
  299. });
  300. it('displays the copy widget button in edit mode', async () => {
  301. const dashboardWithOneWidget = {...mockDashboard, widgets};
  302. await act(async () => {
  303. mount(dashboardWithOneWidget);
  304. await tick();
  305. });
  306. expect(screen.getByLabelText('Duplicate Widget')).toBeInTheDocument();
  307. });
  308. it('duplicates the widget', async () => {
  309. const dashboardWithOneWidget = {...mockDashboard, widgets};
  310. await act(async () => {
  311. const {rerender} = mount(dashboardWithOneWidget);
  312. userEvent.click(screen.getByLabelText('Duplicate Widget'));
  313. rerender();
  314. await tick();
  315. });
  316. expect(screen.getAllByText('Test Discover Widget')).toHaveLength(2);
  317. });
  318. it('opens the widget builder when editing with the modal access flag', async function () {
  319. const testData = initializeOrg({
  320. ...initializeOrg(),
  321. organization: {
  322. features: ['dashboards-basic', 'dashboards-edit', 'dashboard-grid-layout'],
  323. },
  324. });
  325. const dashboardWithOneWidget = {
  326. ...mockDashboard,
  327. widgets: [newWidget],
  328. };
  329. await act(async () => {
  330. mount(
  331. dashboardWithOneWidget,
  332. testData.organization,
  333. testData.router,
  334. testData.router.location
  335. );
  336. await tick();
  337. userEvent.click(screen.getByLabelText('Edit Widget'));
  338. });
  339. expect(testData.router.push).toHaveBeenCalledWith(
  340. expect.objectContaining({
  341. pathname: '/organizations/org-slug/dashboard/1/widget/0/edit/',
  342. })
  343. );
  344. });
  345. });
  346. });