bookmarkStar.spec.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import BookmarkStar from 'sentry/components/projects/bookmarkStar';
  3. describe('BookmarkStar', function () {
  4. let projectMock: jest.Mock;
  5. beforeEach(function () {
  6. projectMock = MockApiClient.addMockResponse({
  7. url: '/projects/org-slug/project-slug/',
  8. method: 'PUT',
  9. body: TestStubs.Project({isBookmarked: false, platform: 'javascript'}),
  10. });
  11. });
  12. afterEach(function () {
  13. MockApiClient.clearMockResponses();
  14. });
  15. it('renders', function () {
  16. const {container} = render(
  17. <BookmarkStar
  18. organization={TestStubs.Organization()}
  19. project={TestStubs.Project()}
  20. />
  21. );
  22. expect(container).toSnapshot();
  23. });
  24. it('can star', function () {
  25. render(
  26. <BookmarkStar
  27. organization={TestStubs.Organization()}
  28. project={TestStubs.Project()}
  29. />
  30. );
  31. expect(screen.getByRole('button', {pressed: false})).toBeInTheDocument();
  32. userEvent.click(screen.getByRole('button'));
  33. expect(projectMock).toHaveBeenCalledWith(
  34. '/projects/org-slug/project-slug/',
  35. expect.objectContaining({data: {isBookmarked: true}})
  36. );
  37. });
  38. it('can unstar', function () {
  39. render(
  40. <BookmarkStar
  41. organization={TestStubs.Organization()}
  42. project={TestStubs.Project({
  43. isBookmarked: true,
  44. })}
  45. />
  46. );
  47. expect(screen.getByRole('button', {pressed: true})).toBeInTheDocument();
  48. userEvent.click(screen.getByRole('button'));
  49. expect(projectMock).toHaveBeenCalledWith(
  50. '/projects/org-slug/project-slug/',
  51. expect.objectContaining({data: {isBookmarked: false}})
  52. );
  53. });
  54. it('takes a manual isBookmarked prop', async function () {
  55. render(
  56. <BookmarkStar
  57. organization={TestStubs.Organization()}
  58. project={TestStubs.Project()}
  59. isBookmarked
  60. />
  61. );
  62. expect(screen.getByRole('button', {pressed: true})).toBeInTheDocument();
  63. userEvent.click(screen.getByRole('button'));
  64. // State if isBookmarked is maintained via the prop
  65. expect(await screen.findByRole('button', {pressed: true})).toBeInTheDocument();
  66. });
  67. });