dashboardList.spec.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 DashboardList from 'sentry/views/dashboards/manage/dashboardList';
  15. import {type DashboardListItem, DisplayType} from 'sentry/views/dashboards/types';
  16. describe('Dashboards - DashboardList', 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', function () {
  97. render(
  98. <DashboardList
  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. });
  109. it('renders dashboard list', function () {
  110. render(
  111. <DashboardList
  112. onDashboardsChange={jest.fn()}
  113. organization={organization}
  114. dashboards={dashboards}
  115. location={router.location}
  116. columnCount={3}
  117. rowCount={3}
  118. />
  119. );
  120. expect(screen.getByText('Dashboard 1')).toBeInTheDocument();
  121. expect(screen.getByText('Dashboard 2')).toBeInTheDocument();
  122. });
  123. it('returns landing page url for dashboards', function () {
  124. render(
  125. <DashboardList
  126. onDashboardsChange={jest.fn()}
  127. organization={organization}
  128. dashboards={dashboards}
  129. location={router.location}
  130. columnCount={3}
  131. rowCount={3}
  132. />,
  133. {router}
  134. );
  135. expect(screen.getByRole('link', {name: 'Dashboard 1'})).toHaveAttribute(
  136. 'href',
  137. '/organizations/org-slug/dashboard/1/'
  138. );
  139. expect(screen.getByRole('link', {name: 'Dashboard 2'})).toHaveAttribute(
  140. 'href',
  141. '/organizations/org-slug/dashboard/2/'
  142. );
  143. });
  144. it('persists global selection headers', function () {
  145. render(
  146. <DashboardList
  147. onDashboardsChange={jest.fn()}
  148. organization={organization}
  149. dashboards={dashboards}
  150. location={{...LocationFixture(), query: {statsPeriod: '7d'}}}
  151. columnCount={3}
  152. rowCount={3}
  153. />,
  154. {router}
  155. );
  156. expect(screen.getByRole('link', {name: 'Dashboard 1'})).toHaveAttribute(
  157. 'href',
  158. '/organizations/org-slug/dashboard/1/?statsPeriod=7d'
  159. );
  160. });
  161. it('can delete dashboards', async function () {
  162. render(
  163. <DashboardList
  164. organization={organization}
  165. dashboards={dashboards}
  166. location={{...LocationFixture(), query: {}}}
  167. onDashboardsChange={dashboardUpdateMock}
  168. columnCount={3}
  169. rowCount={3}
  170. />,
  171. {router}
  172. );
  173. renderGlobalModal();
  174. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  175. await userEvent.click(screen.getByTestId('dashboard-delete'));
  176. expect(deleteMock).not.toHaveBeenCalled();
  177. await userEvent.click(
  178. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  179. );
  180. await waitFor(() => {
  181. expect(deleteMock).toHaveBeenCalled();
  182. expect(dashboardUpdateMock).toHaveBeenCalled();
  183. });
  184. });
  185. it('cannot delete last dashboard', async function () {
  186. const singleDashboard = [
  187. DashboardListItemFixture({
  188. id: '1',
  189. title: 'Dashboard 1',
  190. dateCreated: '2021-04-19T13:13:23.962105Z',
  191. createdBy: UserFixture({id: '1'}),
  192. widgetPreview: [],
  193. }),
  194. ];
  195. render(
  196. <DashboardList
  197. organization={organization}
  198. dashboards={singleDashboard}
  199. location={LocationFixture()}
  200. onDashboardsChange={dashboardUpdateMock}
  201. columnCount={3}
  202. rowCount={3}
  203. />
  204. );
  205. await userEvent.click(screen.getByRole('button', {name: /dashboard actions/i}));
  206. expect(screen.getByTestId('dashboard-delete')).toHaveAttribute(
  207. 'aria-disabled',
  208. 'true'
  209. );
  210. });
  211. it('can duplicate dashboards', async function () {
  212. render(
  213. <DashboardList
  214. organization={organization}
  215. dashboards={dashboards}
  216. location={{...LocationFixture(), query: {}}}
  217. onDashboardsChange={dashboardUpdateMock}
  218. columnCount={3}
  219. rowCount={3}
  220. />
  221. );
  222. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  223. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  224. await waitFor(() => {
  225. expect(createMock).toHaveBeenCalled();
  226. expect(dashboardUpdateMock).toHaveBeenCalled();
  227. });
  228. });
  229. it('does not throw an error if the POST fails during duplication', async function () {
  230. const postMock = MockApiClient.addMockResponse({
  231. url: '/organizations/org-slug/dashboards/',
  232. method: 'POST',
  233. statusCode: 404,
  234. });
  235. render(
  236. <DashboardList
  237. organization={organization}
  238. dashboards={dashboards}
  239. location={{...LocationFixture(), query: {}}}
  240. onDashboardsChange={dashboardUpdateMock}
  241. columnCount={3}
  242. rowCount={3}
  243. />
  244. );
  245. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  246. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  247. await waitFor(() => {
  248. expect(postMock).toHaveBeenCalled();
  249. // Should not update, and not throw error
  250. expect(dashboardUpdateMock).not.toHaveBeenCalled();
  251. });
  252. });
  253. });