dashboardList.spec.tsx 7.4 KB

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