commitRow.spec.tsx 3.5 KB

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