useCommitters.spec.tsx 3.0 KB

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