dashboard.spec.tsx 11 KB

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