index.spec.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import {DashboardListItemFixture} from 'sentry-fixture/dashboard';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {RouteComponentPropsFixture} from 'sentry-fixture/routeComponentPropsFixture';
  5. import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  6. import selectEvent from 'sentry-test/selectEvent';
  7. import ProjectsStore from 'sentry/stores/projectsStore';
  8. import {useNavigate} from 'sentry/utils/useNavigate';
  9. import ManageDashboards from 'sentry/views/dashboards/manage';
  10. import {getPaginationPageLink} from 'sentry/views/organizationStats/utils';
  11. const FEATURES = [
  12. 'global-views',
  13. 'dashboards-basic',
  14. 'dashboards-edit',
  15. 'discover-query',
  16. ];
  17. jest.mock('sentry/utils/useNavigate', () => ({
  18. useNavigate: jest.fn(),
  19. }));
  20. const mockUseNavigate = jest.mocked(useNavigate);
  21. describe('Dashboards > Detail', function () {
  22. const mockUnauthorizedOrg = OrganizationFixture({
  23. features: ['global-views', 'dashboards-basic', 'discover-query'],
  24. });
  25. const mockAuthorizedOrg = OrganizationFixture({
  26. features: FEATURES,
  27. });
  28. beforeEach(function () {
  29. act(() => ProjectsStore.loadInitialData([ProjectFixture()]));
  30. MockApiClient.addMockResponse({
  31. url: '/organizations/org-slug/projects/',
  32. body: [],
  33. });
  34. MockApiClient.addMockResponse({
  35. url: '/organizations/org-slug/dashboards/',
  36. body: [],
  37. });
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/dashboards/?sort=name&per_page=9',
  40. body: [],
  41. });
  42. });
  43. afterEach(function () {
  44. MockApiClient.clearMockResponses();
  45. });
  46. it('renders', async function () {
  47. MockApiClient.addMockResponse({
  48. url: '/organizations/org-slug/dashboards/',
  49. body: [DashboardListItemFixture({title: 'Test Dashboard'})],
  50. });
  51. render(<ManageDashboards />, {
  52. ...RouteComponentPropsFixture(),
  53. organization: mockAuthorizedOrg,
  54. });
  55. expect(await screen.findByText('Dashboards')).toBeInTheDocument();
  56. expect(await screen.findByText('Test Dashboard')).toBeInTheDocument();
  57. expect(screen.queryAllByTestId('loading-placeholder')).toHaveLength(0);
  58. });
  59. it('shows error message when receiving error', async function () {
  60. MockApiClient.addMockResponse({
  61. url: '/organizations/org-slug/dashboards/',
  62. statusCode: 400,
  63. });
  64. render(<ManageDashboards />, {
  65. ...RouteComponentPropsFixture(),
  66. organization: mockAuthorizedOrg,
  67. });
  68. expect(await screen.findByText('Oops! Something went wrong')).toBeInTheDocument();
  69. });
  70. it('denies access on missing feature', async function () {
  71. render(<ManageDashboards />, {
  72. ...RouteComponentPropsFixture(),
  73. organization: mockUnauthorizedOrg,
  74. });
  75. expect(
  76. await screen.findByText("You don't have access to this feature")
  77. ).toBeInTheDocument();
  78. });
  79. it('denies access on no projects', async function () {
  80. act(() => ProjectsStore.loadInitialData([]));
  81. render(<ManageDashboards />, {
  82. ...RouteComponentPropsFixture(),
  83. organization: mockAuthorizedOrg,
  84. });
  85. expect(
  86. await screen.findByText('You need at least one project to use this view')
  87. ).toBeInTheDocument();
  88. });
  89. it('creates new dashboard', async function () {
  90. const org = OrganizationFixture({features: FEATURES});
  91. const mockNavigate = jest.fn();
  92. mockUseNavigate.mockReturnValue(mockNavigate);
  93. render(<ManageDashboards />, {
  94. ...RouteComponentPropsFixture(),
  95. organization: org,
  96. });
  97. await userEvent.click(await screen.findByTestId('dashboard-create'));
  98. expect(mockNavigate).toHaveBeenCalledWith({
  99. pathname: '/organizations/org-slug/dashboards/new/',
  100. query: {},
  101. });
  102. });
  103. it('can sort', async function () {
  104. const org = OrganizationFixture({features: FEATURES});
  105. const mockNavigate = jest.fn();
  106. mockUseNavigate.mockReturnValue(mockNavigate);
  107. render(<ManageDashboards />, {
  108. ...RouteComponentPropsFixture(),
  109. organization: org,
  110. });
  111. await selectEvent.select(
  112. await screen.findByRole('button', {name: /sort by/i}),
  113. 'Dashboard Name (A-Z)'
  114. );
  115. expect(mockNavigate).toHaveBeenCalledWith(
  116. expect.objectContaining({query: {sort: 'title'}})
  117. );
  118. });
  119. it('can search', async function () {
  120. const org = OrganizationFixture({features: FEATURES});
  121. const mockNavigate = jest.fn();
  122. mockUseNavigate.mockReturnValue(mockNavigate);
  123. render(<ManageDashboards />, {
  124. ...RouteComponentPropsFixture(),
  125. organization: org,
  126. });
  127. await userEvent.click(await screen.findByPlaceholderText('Search Dashboards'));
  128. await userEvent.keyboard('dash');
  129. await userEvent.keyboard('[Enter]');
  130. expect(mockNavigate).toHaveBeenCalledWith(
  131. expect.objectContaining({query: {query: 'dash'}})
  132. );
  133. });
  134. it('uses pagination correctly', async function () {
  135. const mockNavigate = jest.fn();
  136. mockUseNavigate.mockReturnValue(mockNavigate);
  137. MockApiClient.addMockResponse({
  138. url: '/organizations/org-slug/dashboards/',
  139. body: [DashboardListItemFixture({title: 'Test Dashboard 1'})],
  140. headers: {Link: getPaginationPageLink({numRows: 15, pageSize: 9, offset: 0})},
  141. });
  142. render(<ManageDashboards />, {
  143. ...RouteComponentPropsFixture(),
  144. organization: mockAuthorizedOrg,
  145. });
  146. expect(await screen.findByText('Test Dashboard 1')).toBeInTheDocument();
  147. await userEvent.click(await screen.findByLabelText('Next'));
  148. expect(mockNavigate).toHaveBeenCalledWith(
  149. expect.objectContaining({
  150. query: {
  151. cursor: '0:9:0',
  152. },
  153. })
  154. );
  155. });
  156. it('disables pagination correctly', async function () {
  157. const mockNavigate = jest.fn();
  158. mockUseNavigate.mockReturnValue(mockNavigate);
  159. MockApiClient.addMockResponse({
  160. url: '/organizations/org-slug/dashboards/',
  161. body: [DashboardListItemFixture({title: 'Test Dashboard 1'})],
  162. headers: {Link: getPaginationPageLink({numRows: 15, pageSize: 9, offset: 0})},
  163. });
  164. render(<ManageDashboards />, {
  165. ...RouteComponentPropsFixture(),
  166. organization: mockAuthorizedOrg,
  167. });
  168. expect(await screen.findByText('Test Dashboard 1')).toBeInTheDocument();
  169. await userEvent.click(await screen.findByLabelText('Previous'));
  170. expect(mockNavigate).not.toHaveBeenCalled();
  171. });
  172. });