dashboard.spec.tsx 13 KB

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