index.spec.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import OrganizationProjectsContainer from 'sentry/views/settings/organizationProjects';
  3. describe('OrganizationProjects', function () {
  4. let projectsGetMock: jest.Mock;
  5. let statsGetMock: jest.Mock;
  6. let projectsPutMock: jest.Mock;
  7. const org = TestStubs.Organization();
  8. const project = TestStubs.Project();
  9. const routerProps = TestStubs.routeComponentProps();
  10. const routerContext = TestStubs.routerContext();
  11. const router = TestStubs.router();
  12. beforeEach(function () {
  13. projectsGetMock = MockApiClient.addMockResponse({
  14. url: '/organizations/org-slug/projects/',
  15. body: [project],
  16. });
  17. statsGetMock = MockApiClient.addMockResponse({
  18. url: '/organizations/org-slug/stats/',
  19. body: [[[], 1]],
  20. });
  21. projectsPutMock = MockApiClient.addMockResponse({
  22. method: 'PUT',
  23. url: '/projects/org-slug/project-slug/',
  24. });
  25. });
  26. afterEach(function () {
  27. MockApiClient.clearMockResponses();
  28. });
  29. it('should render the projects in the store', async function () {
  30. const {container} = render(
  31. <OrganizationProjectsContainer
  32. {...routerProps}
  33. location={{...router.location, query: {}}}
  34. />
  35. );
  36. expect(container).toSnapshot();
  37. expect(screen.getByText('project-slug')).toBeInTheDocument();
  38. expect(projectsGetMock).toHaveBeenCalledTimes(1);
  39. expect(statsGetMock).toHaveBeenCalledTimes(1);
  40. expect(projectsPutMock).toHaveBeenCalledTimes(0);
  41. await userEvent.click(screen.getByRole('button', {name: 'Bookmark Project'}));
  42. expect(
  43. screen.getByRole('button', {name: 'Bookmark Project', pressed: true})
  44. ).toBeInTheDocument();
  45. expect(projectsPutMock).toHaveBeenCalledTimes(1);
  46. });
  47. it('should search organization projects', async function () {
  48. const searchMock = MockApiClient.addMockResponse({
  49. url: `/organizations/${org.slug}/projects/`,
  50. body: [],
  51. });
  52. render(
  53. <OrganizationProjectsContainer
  54. {...routerProps}
  55. location={{...router.location, query: {}}}
  56. />,
  57. {
  58. context: routerContext,
  59. }
  60. );
  61. const searchBox = screen.getByRole('textbox');
  62. await userEvent.type(searchBox, project.slug);
  63. expect(searchMock).toHaveBeenLastCalledWith(
  64. `/organizations/${org.slug}/projects/`,
  65. expect.objectContaining({
  66. method: 'GET',
  67. query: {
  68. query: project.slug,
  69. },
  70. })
  71. );
  72. await userEvent.type(searchBox, '{enter}');
  73. expect(routerContext.context.router.push).toHaveBeenCalledTimes(1);
  74. });
  75. });