dashboard.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {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/dashboards/dashboard';
  6. import {DisplayType, Widget, WidgetType} from 'sentry/views/dashboards/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: {}, 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. mount(mockDashboardWithIssueWidget, organization);
  244. expect(await screen.findByText('Test Discover Widget')).toBeInTheDocument();
  245. expect(screen.getByText('Test Issue Widget')).toBeInTheDocument();
  246. });
  247. it('renders suggested assignees', async () => {
  248. const mockDashboardWithIssueWidget = {
  249. ...mockDashboard,
  250. widgets: [{...issueWidget}],
  251. };
  252. mount(mockDashboardWithIssueWidget, organization);
  253. expect(await screen.findByText('T')).toBeInTheDocument();
  254. await userEvent.hover(screen.getByText('T'));
  255. expect(await screen.findByText('Suggestion: test@sentry.io')).toBeInTheDocument();
  256. expect(screen.getByText('Matching Issue Owners Rule')).toBeInTheDocument();
  257. });
  258. });
  259. describe('Edit mode', () => {
  260. let widgets: Widget[];
  261. const mount = (
  262. dashboard,
  263. mockedOrg = initialData.organization,
  264. mockedRouter = initialData.router,
  265. mockedLocation = initialData.router.location
  266. ) => {
  267. const getDashboardComponent = () => (
  268. <OrganizationContext.Provider value={initialData.organization}>
  269. <MEPSettingProvider forceTransactions={false}>
  270. <Dashboard
  271. paramDashboardId="1"
  272. dashboard={dashboard}
  273. organization={mockedOrg}
  274. isEditing
  275. onUpdate={newWidgets => {
  276. widgets.splice(0, widgets.length, ...newWidgets);
  277. }}
  278. handleUpdateWidgetList={() => undefined}
  279. handleAddCustomWidget={() => undefined}
  280. router={mockedRouter}
  281. location={mockedLocation}
  282. widgetLimitReached={false}
  283. />
  284. </MEPSettingProvider>
  285. </OrganizationContext.Provider>
  286. );
  287. const {rerender} = render(getDashboardComponent());
  288. return {rerender: () => rerender(getDashboardComponent())};
  289. };
  290. beforeEach(() => {
  291. widgets = [newWidget];
  292. });
  293. it('displays the copy widget button in edit mode', async () => {
  294. const dashboardWithOneWidget = {...mockDashboard, widgets};
  295. mount(dashboardWithOneWidget);
  296. expect(await screen.findByLabelText('Duplicate Widget')).toBeInTheDocument();
  297. });
  298. it('duplicates the widget', async () => {
  299. const dashboardWithOneWidget = {...mockDashboard, widgets};
  300. const {rerender} = mount(dashboardWithOneWidget);
  301. await userEvent.click(await screen.findByLabelText('Duplicate Widget'));
  302. rerender();
  303. await waitFor(() => {
  304. expect(screen.getAllByText('Test Discover Widget')).toHaveLength(2);
  305. });
  306. });
  307. it('opens the widget builder when editing with the modal access flag', async function () {
  308. const testData = initializeOrg({
  309. organization: {
  310. features: ['dashboards-basic', 'dashboards-edit'],
  311. },
  312. });
  313. const dashboardWithOneWidget = {
  314. ...mockDashboard,
  315. widgets: [newWidget],
  316. };
  317. mount(
  318. dashboardWithOneWidget,
  319. testData.organization,
  320. testData.router,
  321. testData.router.location
  322. );
  323. await userEvent.click(await screen.findByLabelText('Edit Widget'));
  324. expect(testData.router.push).toHaveBeenCalledWith(
  325. expect.objectContaining({
  326. pathname: '/organizations/org-slug/dashboard/1/widget/0/edit/',
  327. })
  328. );
  329. });
  330. });
  331. });