dashboardList.spec.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import {DashboardListItemFixture} from 'sentry-fixture/dashboard';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {UserFixture} from 'sentry-fixture/user';
  6. import {initializeOrg} from 'sentry-test/initializeOrg';
  7. import {
  8. render,
  9. renderGlobalModal,
  10. screen,
  11. userEvent,
  12. waitFor,
  13. within,
  14. } from 'sentry-test/reactTestingLibrary';
  15. import DashboardList from 'sentry/views/dashboards/manage/dashboardList';
  16. import {DisplayType} from 'sentry/views/dashboards/types';
  17. describe('Dashboards - DashboardList', function () {
  18. let dashboards, deleteMock, dashboardUpdateMock, createMock;
  19. const organization = OrganizationFixture({
  20. features: ['global-views', 'dashboards-basic', 'dashboards-edit', 'discover-query'],
  21. projects: [ProjectFixture()],
  22. });
  23. const {router, routerContext} = initializeOrg();
  24. beforeEach(function () {
  25. MockApiClient.clearMockResponses();
  26. MockApiClient.addMockResponse({
  27. url: '/organizations/org-slug/projects/',
  28. body: [],
  29. });
  30. dashboards = [
  31. DashboardListItemFixture({
  32. id: '1',
  33. title: 'Dashboard 1',
  34. dateCreated: '2021-04-19T13:13:23.962105Z',
  35. createdBy: UserFixture({id: '1'}),
  36. }),
  37. DashboardListItemFixture({
  38. id: '2',
  39. title: 'Dashboard 2',
  40. dateCreated: '2021-04-19T13:13:23.962105Z',
  41. createdBy: UserFixture({id: '1'}),
  42. widgetPreview: [
  43. {
  44. displayType: DisplayType.LINE,
  45. layout: null,
  46. },
  47. {
  48. displayType: DisplayType.TABLE,
  49. layout: null,
  50. },
  51. ],
  52. }),
  53. ];
  54. deleteMock = MockApiClient.addMockResponse({
  55. url: '/organizations/org-slug/dashboards/2/',
  56. method: 'DELETE',
  57. statusCode: 200,
  58. });
  59. MockApiClient.addMockResponse({
  60. url: '/organizations/org-slug/dashboards/2/',
  61. method: 'GET',
  62. statusCode: 200,
  63. body: {
  64. id: '2',
  65. title: 'Dashboard Demo',
  66. widgets: [
  67. {
  68. id: '1',
  69. title: 'Errors',
  70. displayType: 'big_number',
  71. interval: '5m',
  72. },
  73. {
  74. id: '2',
  75. title: 'Transactions',
  76. displayType: 'big_number',
  77. interval: '5m',
  78. },
  79. {
  80. id: '3',
  81. title: 'p50 of /api/cat',
  82. displayType: 'big_number',
  83. interval: '5m',
  84. },
  85. ],
  86. },
  87. });
  88. createMock = MockApiClient.addMockResponse({
  89. url: '/organizations/org-slug/dashboards/',
  90. method: 'POST',
  91. statusCode: 200,
  92. });
  93. dashboardUpdateMock = jest.fn();
  94. });
  95. it('renders an empty list', function () {
  96. render(
  97. <DashboardList
  98. onDashboardsChange={jest.fn()}
  99. organization={organization}
  100. dashboards={[]}
  101. pageLinks=""
  102. location={router.location}
  103. />
  104. );
  105. expect(screen.getByTestId('empty-state')).toBeInTheDocument();
  106. });
  107. it('renders dashboard list', function () {
  108. render(
  109. <DashboardList
  110. onDashboardsChange={jest.fn()}
  111. organization={organization}
  112. dashboards={dashboards}
  113. pageLinks=""
  114. location={router.location}
  115. />
  116. );
  117. expect(screen.getByText('Dashboard 1')).toBeInTheDocument();
  118. expect(screen.getByText('Dashboard 2')).toBeInTheDocument();
  119. });
  120. it('returns landing page url for dashboards', function () {
  121. render(
  122. <DashboardList
  123. onDashboardsChange={jest.fn()}
  124. organization={organization}
  125. dashboards={dashboards}
  126. pageLinks=""
  127. location={router.location}
  128. />,
  129. {context: routerContext}
  130. );
  131. expect(screen.getByRole('link', {name: 'Dashboard 1'})).toHaveAttribute(
  132. 'href',
  133. '/organizations/org-slug/dashboard/1/?'
  134. );
  135. expect(screen.getByRole('link', {name: 'Dashboard 2'})).toHaveAttribute(
  136. 'href',
  137. '/organizations/org-slug/dashboard/2/?'
  138. );
  139. });
  140. it('persists global selection headers', function () {
  141. render(
  142. <DashboardList
  143. onDashboardsChange={jest.fn()}
  144. organization={organization}
  145. dashboards={dashboards}
  146. pageLinks=""
  147. location={{...LocationFixture(), query: {statsPeriod: '7d'}}}
  148. />,
  149. {context: routerContext}
  150. );
  151. expect(screen.getByRole('link', {name: 'Dashboard 1'})).toHaveAttribute(
  152. 'href',
  153. '/organizations/org-slug/dashboard/1/?statsPeriod=7d'
  154. );
  155. });
  156. it('can delete dashboards', async function () {
  157. render(
  158. <DashboardList
  159. organization={organization}
  160. dashboards={dashboards}
  161. pageLinks=""
  162. location={{...LocationFixture(), query: {}}}
  163. onDashboardsChange={dashboardUpdateMock}
  164. />,
  165. {context: routerContext}
  166. );
  167. renderGlobalModal();
  168. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  169. await userEvent.click(screen.getByTestId('dashboard-delete'));
  170. expect(deleteMock).not.toHaveBeenCalled();
  171. await userEvent.click(
  172. within(screen.getByRole('dialog')).getByRole('button', {name: /confirm/i})
  173. );
  174. await waitFor(() => {
  175. expect(deleteMock).toHaveBeenCalled();
  176. expect(dashboardUpdateMock).toHaveBeenCalled();
  177. });
  178. });
  179. it('cannot delete last dashboard', async function () {
  180. const singleDashboard = [
  181. DashboardListItemFixture({
  182. id: '1',
  183. title: 'Dashboard 1',
  184. dateCreated: '2021-04-19T13:13:23.962105Z',
  185. createdBy: UserFixture({id: '1'}),
  186. widgetPreview: [],
  187. }),
  188. ];
  189. render(
  190. <DashboardList
  191. organization={organization}
  192. dashboards={singleDashboard}
  193. pageLinks=""
  194. location={LocationFixture()}
  195. onDashboardsChange={dashboardUpdateMock}
  196. />
  197. );
  198. await userEvent.click(screen.getByRole('button', {name: /dashboard actions/i}));
  199. expect(screen.getByTestId('dashboard-delete')).toHaveAttribute(
  200. 'aria-disabled',
  201. 'true'
  202. );
  203. });
  204. it('can duplicate dashboards', async function () {
  205. render(
  206. <DashboardList
  207. organization={organization}
  208. dashboards={dashboards}
  209. pageLinks=""
  210. location={{...LocationFixture(), query: {}}}
  211. onDashboardsChange={dashboardUpdateMock}
  212. />
  213. );
  214. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  215. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  216. await waitFor(() => {
  217. expect(createMock).toHaveBeenCalled();
  218. expect(dashboardUpdateMock).toHaveBeenCalled();
  219. });
  220. });
  221. it('does not throw an error if the POST fails during duplication', async function () {
  222. const postMock = MockApiClient.addMockResponse({
  223. url: '/organizations/org-slug/dashboards/',
  224. method: 'POST',
  225. statusCode: 404,
  226. });
  227. render(
  228. <DashboardList
  229. organization={organization}
  230. dashboards={dashboards}
  231. pageLinks=""
  232. location={{...LocationFixture(), query: {}}}
  233. onDashboardsChange={dashboardUpdateMock}
  234. />
  235. );
  236. await userEvent.click(screen.getAllByRole('button', {name: /dashboard actions/i})[1]);
  237. await userEvent.click(screen.getByTestId('dashboard-duplicate'));
  238. await waitFor(() => {
  239. expect(postMock).toHaveBeenCalled();
  240. // Should not update, and not throw error
  241. expect(dashboardUpdateMock).not.toHaveBeenCalled();
  242. });
  243. });
  244. });