dashboard.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import MemberListStore from 'sentry/stores/memberListStore';
  5. import Dashboard from 'sentry/views/dashboardsV2/dashboard';
  6. import {DisplayType, Widget, WidgetType} from 'sentry/views/dashboardsV2/types';
  7. describe('Dashboards > Dashboard', () => {
  8. const organization = TestStubs.Organization({
  9. features: ['dashboards-basic', 'dashboards-edit', 'dashboard-grid-layout'],
  10. });
  11. const organizationWithFlag = TestStubs.Organization({
  12. features: ['dashboards-basic', 'dashboards-edit', 'dashboard-grid-layout'],
  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: {}, project: 1, 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: TestStubs.Tags(),
  110. });
  111. });
  112. it('fetches tags', () => {
  113. mountWithTheme(
  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. 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. const wrapper = mountWithTheme(
  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. initialData.routerContext
  149. );
  150. await tick();
  151. wrapper.update();
  152. expect(mockHandleAddCustomWidget).toHaveBeenCalled();
  153. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  154. });
  155. it('dashboard adds new widget if component updated with newWidget prop', async () => {
  156. const mockHandleAddCustomWidget = jest.fn();
  157. const mockCallbackToUnsetNewWidget = jest.fn();
  158. const wrapper = mountWithTheme(
  159. <Dashboard
  160. paramDashboardId="1"
  161. dashboard={mockDashboard}
  162. organization={initialData.organization}
  163. isEditing={false}
  164. onUpdate={() => undefined}
  165. handleUpdateWidgetList={() => undefined}
  166. handleAddCustomWidget={mockHandleAddCustomWidget}
  167. router={initialData.router}
  168. location={initialData.router.location}
  169. widgetLimitReached={false}
  170. onSetNewWidget={mockCallbackToUnsetNewWidget}
  171. />,
  172. initialData.routerContext
  173. );
  174. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  175. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  176. wrapper.setProps({newWidget});
  177. await tick();
  178. wrapper.update();
  179. expect(mockHandleAddCustomWidget).toHaveBeenCalled();
  180. expect(mockCallbackToUnsetNewWidget).toHaveBeenCalled();
  181. });
  182. it('dashboard does not try to add new widget if no newWidget', async () => {
  183. const mockHandleAddCustomWidget = jest.fn();
  184. const mockCallbackToUnsetNewWidget = jest.fn();
  185. const wrapper = mountWithTheme(
  186. <Dashboard
  187. paramDashboardId="1"
  188. dashboard={mockDashboard}
  189. organization={initialData.organization}
  190. isEditing={false}
  191. onUpdate={() => undefined}
  192. handleUpdateWidgetList={() => undefined}
  193. handleAddCustomWidget={mockHandleAddCustomWidget}
  194. router={initialData.router}
  195. location={initialData.router.location}
  196. widgetLimitReached={false}
  197. onSetNewWidget={mockCallbackToUnsetNewWidget}
  198. />,
  199. initialData.routerContext
  200. );
  201. await tick();
  202. wrapper.update();
  203. expect(mockHandleAddCustomWidget).not.toHaveBeenCalled();
  204. expect(mockCallbackToUnsetNewWidget).not.toHaveBeenCalled();
  205. });
  206. describe('Issue Widgets', () => {
  207. beforeEach(() => {
  208. MemberListStore.init();
  209. });
  210. afterEach(() => {
  211. MemberListStore.teardown();
  212. });
  213. const mount = (dashboard, mockedOrg = initialData.organization) => {
  214. render(
  215. <Dashboard
  216. paramDashboardId="1"
  217. dashboard={dashboard}
  218. organization={mockedOrg}
  219. isEditing={false}
  220. onUpdate={() => undefined}
  221. handleUpdateWidgetList={() => undefined}
  222. handleAddCustomWidget={() => undefined}
  223. router={initialData.router}
  224. location={initialData.router.location}
  225. widgetLimitReached={false}
  226. />
  227. );
  228. };
  229. it('dashboard displays issue widgets if the user has issue widgets feature flag', () => {
  230. const mockDashboardWithIssueWidget = {
  231. ...mockDashboard,
  232. widgets: [newWidget, issueWidget],
  233. };
  234. mount(mockDashboardWithIssueWidget, organizationWithFlag);
  235. expect(screen.getByText('Test Discover Widget')).toBeInTheDocument();
  236. expect(screen.getByText('Test Issue Widget')).toBeInTheDocument();
  237. });
  238. it('renders suggested assignees', async () => {
  239. const mockDashboardWithIssueWidget = {
  240. ...mockDashboard,
  241. widgets: [{...issueWidget}],
  242. };
  243. mount(mockDashboardWithIssueWidget, organizationWithFlag);
  244. expect(await screen.findByText('T')).toBeInTheDocument();
  245. userEvent.hover(screen.getByText('T'));
  246. expect(await screen.findByText('Suggestion:')).toBeInTheDocument();
  247. expect(screen.getByText('test@sentry.io')).toBeInTheDocument();
  248. expect(screen.getByText('Matching Issue Owners Rule')).toBeInTheDocument();
  249. });
  250. });
  251. describe('Edit mode', () => {
  252. let widgets: Widget[];
  253. const mount = (
  254. dashboard,
  255. mockedOrg = initialData.organization,
  256. mockedRouter = initialData.router,
  257. mockedLocation = initialData.router.location
  258. ) => {
  259. const getDashboardComponent = () => (
  260. <Dashboard
  261. paramDashboardId="1"
  262. dashboard={dashboard}
  263. organization={mockedOrg}
  264. isEditing
  265. onUpdate={newWidgets => {
  266. widgets.splice(0, widgets.length, ...newWidgets);
  267. }}
  268. handleUpdateWidgetList={() => undefined}
  269. handleAddCustomWidget={() => undefined}
  270. router={mockedRouter}
  271. location={mockedLocation}
  272. widgetLimitReached={false}
  273. />
  274. );
  275. const {rerender} = render(getDashboardComponent());
  276. return {rerender: () => rerender(getDashboardComponent())};
  277. };
  278. beforeEach(() => {
  279. widgets = [newWidget];
  280. });
  281. it('displays the copy widget button in edit mode', () => {
  282. const dashboardWithOneWidget = {...mockDashboard, widgets};
  283. mount(dashboardWithOneWidget);
  284. expect(screen.getByLabelText('Duplicate Widget')).toBeInTheDocument();
  285. });
  286. it('duplicates the widget', () => {
  287. const dashboardWithOneWidget = {...mockDashboard, widgets};
  288. const {rerender} = mount(dashboardWithOneWidget);
  289. userEvent.click(screen.getByLabelText('Duplicate Widget'));
  290. rerender();
  291. expect(screen.getAllByText('Test Discover Widget')).toHaveLength(2);
  292. });
  293. it('opens the widget builder when editing with the modal access flag', function () {
  294. const testData = initializeOrg({
  295. ...initializeOrg(),
  296. organization: {
  297. features: [
  298. 'dashboards-basic',
  299. 'dashboards-edit',
  300. 'dashboard-grid-layout',
  301. 'new-widget-builder-experience-design',
  302. ],
  303. },
  304. });
  305. const dashboardWithOneWidget = {
  306. ...mockDashboard,
  307. widgets: [newWidget],
  308. };
  309. mount(
  310. dashboardWithOneWidget,
  311. testData.organization,
  312. testData.router,
  313. testData.router.location
  314. );
  315. userEvent.click(screen.getByLabelText('Edit Widget'));
  316. expect(testData.router.push).toHaveBeenCalledWith(
  317. expect.objectContaining({
  318. pathname: '/organizations/org-slug/dashboard/1/widget/0/edit/',
  319. })
  320. );
  321. });
  322. });
  323. });