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