dashboard.spec.tsx 11 KB

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