dashboard.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. {router: initialData.router}
  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. {router: initialData.router}
  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. {router: initialData.router}
  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. {router: initialData.router}
  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. org = initialData.organization,
  267. router = initialData.router,
  268. location = initialData.router.location,
  269. isPreview = false,
  270. }) => {
  271. const getDashboardComponent = () => (
  272. <OrganizationContext.Provider value={initialData.organization}>
  273. <MEPSettingProvider forceTransactions={false}>
  274. <Dashboard
  275. paramDashboardId="1"
  276. dashboard={dashboard}
  277. organization={org}
  278. isEditingDashboard
  279. onUpdate={newWidgets => {
  280. widgets.splice(0, widgets.length, ...newWidgets);
  281. }}
  282. handleUpdateWidgetList={() => undefined}
  283. handleAddCustomWidget={() => undefined}
  284. router={router}
  285. location={location}
  286. widgetLimitReached={false}
  287. isPreview={isPreview}
  288. />
  289. </MEPSettingProvider>
  290. </OrganizationContext.Provider>
  291. );
  292. const {rerender} = render(getDashboardComponent());
  293. return {rerender: () => rerender(getDashboardComponent())};
  294. };
  295. beforeEach(() => {
  296. widgets = [newWidget];
  297. });
  298. it('displays the copy widget button in edit mode', async () => {
  299. const dashboardWithOneWidget = {...mockDashboard, widgets};
  300. mount({dashboard: dashboardWithOneWidget});
  301. expect(await screen.findByLabelText('Duplicate Widget')).toBeInTheDocument();
  302. });
  303. it('duplicates the widget', async () => {
  304. const dashboardWithOneWidget = {...mockDashboard, widgets};
  305. const {rerender} = mount({dashboard: dashboardWithOneWidget});
  306. await userEvent.click(await screen.findByLabelText('Duplicate Widget'));
  307. rerender();
  308. await waitFor(() => {
  309. expect(screen.getAllByText('Test Discover Widget')).toHaveLength(2);
  310. });
  311. });
  312. it('opens the widget builder when editing with the modal access flag', async function () {
  313. const testData = initializeOrg({
  314. organization: {
  315. features: ['dashboards-basic', 'dashboards-edit'],
  316. },
  317. });
  318. const dashboardWithOneWidget = {
  319. ...mockDashboard,
  320. widgets: [newWidget],
  321. };
  322. mount({
  323. dashboard: dashboardWithOneWidget,
  324. org: testData.organization,
  325. router: testData.router,
  326. location: testData.router.location,
  327. });
  328. await userEvent.click(await screen.findByLabelText('Edit Widget'));
  329. expect(testData.router.push).toHaveBeenCalledWith(
  330. expect.objectContaining({
  331. pathname: '/organizations/org-slug/dashboard/1/widget/0/edit/',
  332. })
  333. );
  334. });
  335. it('does not show the add widget button if dashboard is in preview mode', async function () {
  336. const testData = initializeOrg({
  337. organization: {
  338. features: [
  339. 'dashboards-basic',
  340. 'dashboards-edit',
  341. 'custom-metrics',
  342. 'ddm-sidebar-item-hidden',
  343. ],
  344. },
  345. });
  346. const dashboardWithOneWidget = {
  347. ...mockDashboard,
  348. widgets: [newWidget],
  349. };
  350. mount({
  351. dashboard: dashboardWithOneWidget,
  352. org: testData.organization,
  353. isPreview: true,
  354. });
  355. await screen.findByText('Test Discover Widget');
  356. expect(screen.queryByRole('button', {name: /add widget/i})).not.toBeInTheDocument();
  357. });
  358. });
  359. });