bookmarkStar.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import BookmarkStar from 'sentry/components/projects/bookmarkStar';
  3. import ProjectsStore from 'sentry/stores/projectsStore';
  4. describe('BookmarkStar', function () {
  5. const project = TestStubs.Project();
  6. beforeEach(function () {
  7. ProjectsStore.loadInitialData([project]);
  8. });
  9. afterEach(function () {
  10. ProjectsStore.reset();
  11. MockApiClient.clearMockResponses();
  12. });
  13. it('renders', function () {
  14. const {container} = render(
  15. <BookmarkStar organization={TestStubs.Organization()} project={project} />
  16. );
  17. expect(container).toSnapshot();
  18. });
  19. it('can star', async function () {
  20. render(<BookmarkStar organization={TestStubs.Organization()} project={project} />);
  21. const projectMock = MockApiClient.addMockResponse({
  22. url: '/projects/org-slug/project-slug/',
  23. method: 'PUT',
  24. body: TestStubs.Project({isBookmarked: true, platform: 'javascript'}),
  25. });
  26. expect(screen.getByRole('button', {pressed: false})).toBeInTheDocument();
  27. userEvent.click(screen.getByRole('button'));
  28. // Visually optimistically updated
  29. expect(screen.getByRole('button', {pressed: true})).toBeInTheDocument();
  30. expect(projectMock).toHaveBeenCalledWith(
  31. '/projects/org-slug/project-slug/',
  32. expect.objectContaining({data: {isBookmarked: true}})
  33. );
  34. // Not yet updated in the project store
  35. expect(ProjectsStore.getBySlug(project.slug)?.isBookmarked).toBe(false);
  36. // Project store is updated
  37. await waitFor(() => {
  38. const updatedProject = ProjectsStore.getBySlug(project.slug);
  39. expect(updatedProject?.isBookmarked).toBe(true);
  40. });
  41. });
  42. it('can unstar', async function () {
  43. render(
  44. <BookmarkStar
  45. organization={TestStubs.Organization()}
  46. project={TestStubs.Project({isBookmarked: true})}
  47. />
  48. );
  49. const projectMock = MockApiClient.addMockResponse({
  50. url: '/projects/org-slug/project-slug/',
  51. method: 'PUT',
  52. body: TestStubs.Project({isBookmarked: false, platform: 'javascript'}),
  53. });
  54. expect(screen.getByRole('button', {pressed: true})).toBeInTheDocument();
  55. userEvent.click(screen.getByRole('button'));
  56. // Visually optimistically updated
  57. expect(screen.getByRole('button', {pressed: false})).toBeInTheDocument();
  58. expect(projectMock).toHaveBeenCalledWith(
  59. '/projects/org-slug/project-slug/',
  60. expect.objectContaining({data: {isBookmarked: false}})
  61. );
  62. // Not yet updated in the project store
  63. expect(ProjectsStore.getBySlug(project.slug)?.isBookmarked).toBe(false);
  64. // Project store is updated
  65. await waitFor(() => {
  66. const updatedProject = ProjectsStore.getBySlug(project.slug);
  67. expect(updatedProject?.isBookmarked).toBe(false);
  68. });
  69. });
  70. });