dashboardGrid.spec.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  178. await userEvent.click(screen.getByTestId('dashboard-delete'));
  179. expect(deleteMock).not.toHaveBeenCalled();
  180. await userEvent.click(
  181. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  182. );
  183. await waitFor(() => {
  184. expect(deleteMock).toHaveBeenCalled();
  185. expect(dashboardUpdateMock).toHaveBeenCalled();
  186. });
  187. });
  188. it('cannot delete last dashboard', async function () {
  189. const singleDashboard = [
  190. DashboardListItemFixture({
  191. id: '1',
  192. title: 'Dashboard 1',
  193. dateCreated: '2021-04-19T13:13:23.962105Z',
  194. createdBy: UserFixture({id: '1'}),
  195. widgetPreview: [],
  196. }),
  197. ];
  198. render(
  199. <DashboardGrid
  200. organization={organization}
  201. dashboards={singleDashboard}
  202. location={LocationFixture()}
  203. onDashboardsChange={dashboardUpdateMock}
  204. columnCount={3}
  205. rowCount={3}
  206. />
  207. );
  208. await userEvent.click(screen.getByRole('button', {name: /dashboard actions/i}));
  209. expect(screen.getByTestId('dashboard-delete')).toHaveAttribute(
  210. 'aria-disabled',
  211. 'true'
  212. );
  213. });
  214. it('can duplicate dashboards', async function () {
  215. render(
  216. <DashboardGrid
  217. organization={organization}
  218. dashboards={dashboards}
  219. location={{...LocationFixture(), query: {}}}
  220. onDashboardsChange={dashboardUpdateMock}
  221. columnCount={3}
  222. rowCount={3}
  223. />
  224. );
  225. renderGlobalModal();
  226. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  227. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  228. expect(createMock).not.toHaveBeenCalled();
  229. await userEvent.click(
  230. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  231. );
  232. await waitFor(() => {
  233. expect(createMock).toHaveBeenCalled();
  234. expect(dashboardUpdateMock).toHaveBeenCalled();
  235. });
  236. });
  237. it('does not throw an error if the POST fails during duplication', async function () {
  238. const postMock = MockApiClient.addMockResponse({
  239. url: '/organizations/org-slug/dashboards/',
  240. method: 'POST',
  241. statusCode: 404,
  242. });
  243. render(
  244. <DashboardGrid
  245. organization={organization}
  246. dashboards={dashboards}
  247. location={{...LocationFixture(), query: {}}}
  248. onDashboardsChange={dashboardUpdateMock}
  249. columnCount={3}
  250. rowCount={3}
  251. />
  252. );
  253. renderGlobalModal();
  254. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  255. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  256. expect(postMock).not.toHaveBeenCalled();
  257. await userEvent.click(
  258. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  259. );
  260. await waitFor(() => {
  261. expect(postMock).toHaveBeenCalled();
  262. // Should not update, and not throw error
  263. expect(dashboardUpdateMock).not.toHaveBeenCalled();
  264. });
  265. });
  266. });