dashboardList.spec.tsx 7.3 KB

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