dashboard.spec.tsx 11 KB

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