dashboardGrid.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import {DashboardListItemFixture} from 'sentry-fixture/dashboard';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {UserFixture} from 'sentry-fixture/user';
  5. import {initializeOrg} from 'sentry-test/initializeOrg';
  6. import {
  7. render,
  8. renderGlobalModal,
  9. screen,
  10. userEvent,
  11. waitFor,
  12. within,
  13. } from 'sentry-test/reactTestingLibrary';
  14. import DashboardGrid from 'sentry/views/dashboards/manage/dashboardGrid';
  15. import {type DashboardListItem, DisplayType} from 'sentry/views/dashboards/types';
  16. describe('Dashboards - DashboardGrid', function () {
  17. let dashboards: DashboardListItem[];
  18. let deleteMock: jest.Mock;
  19. let dashboardUpdateMock: jest.Mock;
  20. let createMock: jest.Mock;
  21. const organization = OrganizationFixture({
  22. features: ['global-views', 'dashboards-basic', 'dashboards-edit', 'discover-query'],
  23. });
  24. const {router} = initializeOrg();
  25. beforeEach(function () {
  26. MockApiClient.clearMockResponses();
  27. MockApiClient.addMockResponse({
  28. url: '/organizations/org-slug/projects/',
  29. body: [],
  30. });
  31. dashboards = [
  32. DashboardListItemFixture({
  33. id: '1',
  34. title: 'Dashboard 1',
  35. dateCreated: '2021-04-19T13:13:23.962105Z',
  36. createdBy: UserFixture({id: '1'}),
  37. }),
  38. DashboardListItemFixture({
  39. id: '2',
  40. title: 'Dashboard 2',
  41. dateCreated: '2021-04-19T13:13:23.962105Z',
  42. createdBy: UserFixture({id: '1'}),
  43. widgetPreview: [
  44. {
  45. displayType: DisplayType.LINE,
  46. layout: null,
  47. },
  48. {
  49. displayType: DisplayType.TABLE,
  50. layout: null,
  51. },
  52. ],
  53. }),
  54. ];
  55. deleteMock = MockApiClient.addMockResponse({
  56. url: '/organizations/org-slug/dashboards/2/',
  57. method: 'DELETE',
  58. statusCode: 200,
  59. });
  60. MockApiClient.addMockResponse({
  61. url: '/organizations/org-slug/dashboards/2/',
  62. method: 'GET',
  63. statusCode: 200,
  64. body: {
  65. id: '2',
  66. title: 'Dashboard Demo',
  67. widgets: [
  68. {
  69. id: '1',
  70. title: 'Errors',
  71. displayType: 'big_number',
  72. interval: '5m',
  73. },
  74. {
  75. id: '2',
  76. title: 'Transactions',
  77. displayType: 'big_number',
  78. interval: '5m',
  79. },
  80. {
  81. id: '3',
  82. title: 'p50 of /api/cat',
  83. displayType: 'big_number',
  84. interval: '5m',
  85. },
  86. ],
  87. },
  88. });
  89. createMock = MockApiClient.addMockResponse({
  90. url: '/organizations/org-slug/dashboards/',
  91. method: 'POST',
  92. statusCode: 200,
  93. });
  94. dashboardUpdateMock = jest.fn();
  95. });
  96. it('renders an empty list', async function () {
  97. render(
  98. <DashboardGrid
  99. onDashboardsChange={jest.fn()}
  100. organization={organization}
  101. dashboards={[]}
  102. location={router.location}
  103. columnCount={3}
  104. rowCount={3}
  105. />
  106. );
  107. expect(screen.getByTestId('empty-state')).toBeInTheDocument();
  108. expect(
  109. await screen.findByText('Sorry, no Dashboards match your filters.')
  110. ).toBeInTheDocument();
  111. });
  112. it('renders dashboard list', function () {
  113. render(
  114. <DashboardGrid
  115. onDashboardsChange={jest.fn()}
  116. organization={organization}
  117. dashboards={dashboards}
  118. location={router.location}
  119. columnCount={3}
  120. rowCount={3}
  121. />
  122. );
  123. expect(screen.getByText('Dashboard 1')).toBeInTheDocument();
  124. expect(screen.getByText('Dashboard 2')).toBeInTheDocument();
  125. });
  126. it('returns landing page url for dashboards', function () {
  127. render(
  128. <DashboardGrid
  129. onDashboardsChange={jest.fn()}
  130. organization={organization}
  131. dashboards={dashboards}
  132. location={router.location}
  133. columnCount={3}
  134. rowCount={3}
  135. />,
  136. {router}
  137. );
  138. expect(screen.getByRole('link', {name: 'Dashboard 1'})).toHaveAttribute(
  139. 'href',
  140. '/organizations/org-slug/dashboard/1/'
  141. );
  142. expect(screen.getByRole('link', {name: 'Dashboard 2'})).toHaveAttribute(
  143. 'href',
  144. '/organizations/org-slug/dashboard/2/'
  145. );
  146. });
  147. it('persists global selection headers', function () {
  148. render(
  149. <DashboardGrid
  150. onDashboardsChange={jest.fn()}
  151. organization={organization}
  152. dashboards={dashboards}
  153. location={{...LocationFixture(), query: {statsPeriod: '7d'}}}
  154. columnCount={3}
  155. rowCount={3}
  156. />,
  157. {router}
  158. );
  159. expect(screen.getByRole('link', {name: 'Dashboard 1'})).toHaveAttribute(
  160. 'href',
  161. '/organizations/org-slug/dashboard/1/?statsPeriod=7d'
  162. );
  163. });
  164. it('can delete dashboards', async function () {
  165. render(
  166. <DashboardGrid
  167. organization={organization}
  168. dashboards={dashboards}
  169. location={{...LocationFixture(), query: {}}}
  170. onDashboardsChange={dashboardUpdateMock}
  171. columnCount={3}
  172. rowCount={3}
  173. />,
  174. {router}
  175. );
  176. renderGlobalModal();
  177. await userEvent.click(
  178. screen.getAllByRole('button', {name: /dashboard actions/i})[1]!
  179. );
  180. await userEvent.click(screen.getByTestId('dashboard-delete'));
  181. expect(deleteMock).not.toHaveBeenCalled();
  182. await userEvent.click(
  183. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  184. );
  185. await waitFor(() => {
  186. expect(deleteMock).toHaveBeenCalled();
  187. });
  188. expect(dashboardUpdateMock).toHaveBeenCalled();
  189. });
  190. it('cannot delete last dashboard', async function () {
  191. const singleDashboard = [
  192. DashboardListItemFixture({
  193. id: '1',
  194. title: 'Dashboard 1',
  195. dateCreated: '2021-04-19T13:13:23.962105Z',
  196. createdBy: UserFixture({id: '1'}),
  197. widgetPreview: [],
  198. }),
  199. ];
  200. render(
  201. <DashboardGrid
  202. organization={organization}
  203. dashboards={singleDashboard}
  204. location={LocationFixture()}
  205. onDashboardsChange={dashboardUpdateMock}
  206. columnCount={3}
  207. rowCount={3}
  208. />
  209. );
  210. await userEvent.click(screen.getByRole('button', {name: /dashboard actions/i}));
  211. expect(screen.getByTestId('dashboard-delete')).toHaveAttribute(
  212. 'aria-disabled',
  213. 'true'
  214. );
  215. });
  216. it('can duplicate dashboards', async function () {
  217. render(
  218. <DashboardGrid
  219. organization={organization}
  220. dashboards={dashboards}
  221. location={{...LocationFixture(), query: {}}}
  222. onDashboardsChange={dashboardUpdateMock}
  223. columnCount={3}
  224. rowCount={3}
  225. />
  226. );
  227. renderGlobalModal();
  228. await userEvent.click(
  229. screen.getAllByRole('button', {name: /dashboard actions/i})[1]!
  230. );
  231. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  232. expect(createMock).not.toHaveBeenCalled();
  233. await userEvent.click(
  234. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  235. );
  236. await waitFor(() => {
  237. expect(createMock).toHaveBeenCalled();
  238. });
  239. expect(dashboardUpdateMock).toHaveBeenCalled();
  240. });
  241. it('does not throw an error if the POST fails during duplication', async function () {
  242. const postMock = MockApiClient.addMockResponse({
  243. url: '/organizations/org-slug/dashboards/',
  244. method: 'POST',
  245. statusCode: 404,
  246. });
  247. render(
  248. <DashboardGrid
  249. organization={organization}
  250. dashboards={dashboards}
  251. location={{...LocationFixture(), query: {}}}
  252. onDashboardsChange={dashboardUpdateMock}
  253. columnCount={3}
  254. rowCount={3}
  255. />
  256. );
  257. renderGlobalModal();
  258. await userEvent.click(
  259. screen.getAllByRole('button', {name: /dashboard actions/i})[1]!
  260. );
  261. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  262. expect(postMock).not.toHaveBeenCalled();
  263. await userEvent.click(
  264. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  265. );
  266. await waitFor(() => {
  267. expect(postMock).toHaveBeenCalled();
  268. });
  269. // Should not update, and not throw error
  270. expect(dashboardUpdateMock).not.toHaveBeenCalled();
  271. });
  272. it('renders favorite and unfavorite buttons on cards', function () {
  273. dashboards = [
  274. DashboardListItemFixture({
  275. id: '1',
  276. title: 'Dashboard 1',
  277. createdBy: UserFixture({id: '1'}),
  278. isFavorited: true,
  279. }),
  280. DashboardListItemFixture({
  281. id: '2',
  282. title: 'Dashboard 2',
  283. createdBy: UserFixture({id: '1'}),
  284. isFavorited: false,
  285. }),
  286. ];
  287. render(
  288. <DashboardGrid
  289. onDashboardsChange={jest.fn()}
  290. organization={organization}
  291. dashboards={dashboards}
  292. location={router.location}
  293. columnCount={3}
  294. rowCount={3}
  295. />,
  296. {
  297. router,
  298. organization: {
  299. features: ['dashboards-favourite', ...organization.features],
  300. },
  301. }
  302. );
  303. expect(screen.queryAllByLabelText('Dashboards Favorite')).toHaveLength(2);
  304. expect(screen.queryAllByLabelText('Favorite')).toHaveLength(1);
  305. expect(screen.queryAllByLabelText('UnFavorite')).toHaveLength(1);
  306. });
  307. it('makes PUT requests when favoriting', async function () {
  308. const putMock = MockApiClient.addMockResponse({
  309. url: '/organizations/org-slug/dashboards/2/favorite/',
  310. method: 'PUT',
  311. body: {isFavorited: false},
  312. });
  313. dashboards = [
  314. DashboardListItemFixture({
  315. id: '1',
  316. title: 'Dashboard 1',
  317. createdBy: UserFixture({id: '1'}),
  318. isFavorited: true,
  319. }),
  320. DashboardListItemFixture({
  321. id: '2',
  322. title: 'Dashboard 2',
  323. createdBy: UserFixture({id: '1'}),
  324. isFavorited: false,
  325. }),
  326. ];
  327. render(
  328. <DashboardGrid
  329. onDashboardsChange={jest.fn()}
  330. organization={organization}
  331. dashboards={dashboards}
  332. location={router.location}
  333. columnCount={3}
  334. rowCount={3}
  335. />,
  336. {
  337. router,
  338. organization: {
  339. features: ['dashboards-favourite', ...organization.features],
  340. },
  341. }
  342. );
  343. expect(screen.queryAllByLabelText('Favorite')).toHaveLength(1);
  344. const favoriteButton = screen.queryAllByLabelText('Favorite')[0]!;
  345. await userEvent.click(favoriteButton);
  346. await waitFor(() => {
  347. expect(putMock).toHaveBeenCalled();
  348. });
  349. expect(screen.queryAllByLabelText('Favorite')).toHaveLength(0);
  350. });
  351. });