useCommitters.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {Commit} from 'fixtures/js-stubs/commit';
  2. import {CommitAuthor} from 'fixtures/js-stubs/commitAuthor';
  3. import {Event} from 'fixtures/js-stubs/event';
  4. import {Organization} from 'fixtures/js-stubs/organization';
  5. import {Project} from 'fixtures/js-stubs/project';
  6. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  7. import CommitterStore from 'sentry/stores/committerStore';
  8. import useCommitters from 'sentry/utils/useCommitters';
  9. import {OrganizationContext} from 'sentry/views/organizationContext';
  10. describe('useCommitters hook', function () {
  11. const organization = Organization();
  12. const wrapper = ({children}: {children?: React.ReactNode}) => (
  13. <OrganizationContext.Provider value={organization}>
  14. {children}
  15. </OrganizationContext.Provider>
  16. );
  17. const project = Project();
  18. const event = Event();
  19. let mockApiEndpoint: ReturnType<typeof MockApiClient.addMockResponse>;
  20. const endpoint = `/projects/${organization.slug}/${project.slug}/events/${event.id}/committers/`;
  21. const mockData = {
  22. committers: [
  23. {
  24. author: CommitAuthor(),
  25. commits: [Commit()],
  26. },
  27. ],
  28. };
  29. beforeEach(() => {
  30. mockApiEndpoint = MockApiClient.addMockResponse({
  31. url: endpoint,
  32. body: mockData,
  33. });
  34. CommitterStore.init();
  35. });
  36. afterEach(() => {
  37. MockApiClient.clearMockResponses();
  38. jest.clearAllMocks();
  39. });
  40. it('returns committers', async () => {
  41. const {result, waitFor} = reactHooks.renderHook(useCommitters, {
  42. initialProps: {eventId: event.id, projectSlug: project.slug},
  43. wrapper,
  44. });
  45. await waitFor(() => expect(result.current.committers).toEqual(mockData.committers));
  46. expect(result.current.fetching).toBe(false);
  47. expect(mockApiEndpoint).toHaveBeenCalledTimes(1);
  48. });
  49. it('prevents repeated calls', async () => {
  50. const {result, waitFor} = reactHooks.renderHook(useCommitters, {
  51. initialProps: {eventId: event.id, projectSlug: project.slug},
  52. wrapper,
  53. });
  54. await waitFor(() => expect(result.current.committers).toEqual(mockData.committers));
  55. reactHooks.renderHook(useCommitters, {
  56. initialProps: {eventId: event.id, projectSlug: project.slug},
  57. wrapper,
  58. });
  59. reactHooks.renderHook(useCommitters, {
  60. initialProps: {eventId: event.id, projectSlug: project.slug},
  61. wrapper,
  62. });
  63. expect(mockApiEndpoint).toHaveBeenCalledTimes(1);
  64. });
  65. /**
  66. * Same as 'prevents repeated calls', but with the async fetch/checks
  67. * happening on same tick.
  68. *
  69. * Additionally, this test checks that withCommitters.fetchCommitters does
  70. * not check for (store.orgSlug !== orgSlug) as the short-circuit does not
  71. * change the value for orgSlug
  72. */
  73. it('prevents simultaneous calls', async () => {
  74. // Mount and run duplicates
  75. reactHooks.renderHook(useCommitters, {
  76. initialProps: {eventId: event.id, projectSlug: project.slug},
  77. wrapper,
  78. });
  79. const {result, waitFor} = reactHooks.renderHook(useCommitters, {
  80. initialProps: {eventId: event.id, projectSlug: project.slug},
  81. wrapper,
  82. });
  83. await waitFor(() => expect(result.current.committers).toEqual(mockData.committers));
  84. expect(mockApiEndpoint).toHaveBeenCalledTimes(1);
  85. });
  86. });