dashboard.spec.tsx 11 KB

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