commitRow.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {RouterContextFixture} from 'sentry-fixture/routerContextFixture';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import {textWithMarkupMatcher} from 'sentry-test/utils';
  4. import {openInviteMembersModal} from 'sentry/actionCreators/modal';
  5. import {CommitRow} from 'sentry/components/commitRow';
  6. import type {Commit, Repository} from 'sentry/types/integrations';
  7. import {RepositoryStatus} from 'sentry/types/integrations';
  8. import type {User} from 'sentry/types/user';
  9. jest.mock('sentry/components/hovercard', () => {
  10. return {
  11. Header: ({children}: {children: React.ReactNode}) => children,
  12. Body: ({children}: {children: React.ReactNode}) => children,
  13. Hovercard: ({body}) => {
  14. return body;
  15. },
  16. };
  17. });
  18. jest.mock('sentry/actionCreators/modal', () => {
  19. return {
  20. ...jest.requireActual('sentry/actionCreators/modal'),
  21. openInviteMembersModal: jest.fn(),
  22. };
  23. });
  24. const baseCommit: Commit = {
  25. dateCreated: '2020-11-30T18:46:31Z',
  26. id: 'f7f395d14b2fe29a4e253bf1d3094d61e6ad4434',
  27. message: 'ref(commitRow): refactor to fc\n',
  28. author: {
  29. id: '0',
  30. username: 'author',
  31. ip_address: '192.168.1.1',
  32. email: 'author@commit.com',
  33. name: 'Author',
  34. } as User,
  35. repository: {
  36. id: '1',
  37. integrationId: '2',
  38. name: 'getsentry/sentry',
  39. dateCreated: '2019-11-30T18:46:31Z',
  40. } as Repository,
  41. releases: [],
  42. };
  43. // static/app/components/hovercard.tsx
  44. describe('commitRow', () => {
  45. it('renders custom avatar', () => {
  46. render(<CommitRow commit={baseCommit} customAvatar="Custom Avatar" />);
  47. expect(screen.getByText(/Custom Avatar/)).toBeInTheDocument();
  48. });
  49. it('renders invite flow for non associated users', async () => {
  50. const commit: Commit = {
  51. ...baseCommit,
  52. author: {
  53. ...baseCommit.author,
  54. id: undefined as unknown as string,
  55. },
  56. } as Commit;
  57. render(<CommitRow commit={commit} />, {context: RouterContextFixture()});
  58. expect(
  59. screen.getByText(
  60. textWithMarkupMatcher(
  61. /The email author@commit.com is not a member of your organization./
  62. )
  63. )
  64. ).toBeInTheDocument();
  65. await userEvent.click(screen.getByText(/Invite/));
  66. // @ts-expect-error we are mocking this import
  67. expect(openInviteMembersModal.mock.calls[0][0].initialData[0].emails).toEqual(
  68. new Set(['author@commit.com'])
  69. );
  70. });
  71. it('renders commit info', () => {
  72. const commit: Commit = {
  73. ...baseCommit,
  74. author: {
  75. ...baseCommit.author,
  76. id: '0' as unknown as string,
  77. },
  78. } as Commit;
  79. render(<CommitRow commit={commit} />);
  80. expect(screen.getByText(/ref\(commitRow\): refactor to fc/)).toBeInTheDocument();
  81. });
  82. it('renders pull request', async () => {
  83. const commit: Commit = {
  84. ...baseCommit,
  85. pullRequest: {
  86. id: '9',
  87. title: 'cool pr',
  88. externalUrl: 'https://github.com/getsentry/sentry/pull/1',
  89. repository: {
  90. id: '14',
  91. name: 'example',
  92. url: '',
  93. provider: {
  94. id: 'unknown',
  95. name: 'Unknown Provider',
  96. },
  97. status: RepositoryStatus.ACTIVE,
  98. dateCreated: '2022-10-07T19:35:27.370422Z',
  99. integrationId: '14',
  100. externalSlug: 'org-slug',
  101. externalId: '1',
  102. },
  103. },
  104. };
  105. const handlePullRequestClick = jest.fn();
  106. render(<CommitRow commit={commit} onPullRequestClick={handlePullRequestClick} />);
  107. const pullRequestButton = screen.getByRole('button', {name: 'View Pull Request'});
  108. expect(pullRequestButton).toBeInTheDocument();
  109. expect(pullRequestButton).toHaveAttribute(
  110. 'href',
  111. 'https://github.com/getsentry/sentry/pull/1'
  112. );
  113. await userEvent.click(pullRequestButton);
  114. expect(handlePullRequestClick).toHaveBeenCalledTimes(1);
  115. });
  116. });