withRepositories.spec.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {render, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import RepositoryStore from 'sentry/stores/repositoryStore';
  3. import withRepositories from 'sentry/utils/withRepositories';
  4. describe('withRepositories HoC', function () {
  5. const organization = TestStubs.Organization();
  6. const orgSlug = organization.slug;
  7. const repoUrl = `/organizations/${orgSlug}/repos/`;
  8. const api = new MockApiClient();
  9. const mockData = [{id: '1'}];
  10. beforeEach(() => {
  11. MockApiClient.clearMockResponses();
  12. MockApiClient.addMockResponse({
  13. url: repoUrl,
  14. body: mockData,
  15. });
  16. jest.restoreAllMocks();
  17. RepositoryStore.init?.();
  18. });
  19. it('adds repositories prop', async function () {
  20. const Component = jest.fn(() => null);
  21. const Container = withRepositories(Component);
  22. render(<Container api={api} organization={organization} />);
  23. await waitFor(() =>
  24. expect(Component).toHaveBeenCalledWith(
  25. expect.objectContaining({
  26. repositories: mockData,
  27. repositoriesLoading: false,
  28. repositoriesError: undefined,
  29. }),
  30. {}
  31. )
  32. );
  33. });
  34. it('prevents repeated calls', function () {
  35. const Component = () => null;
  36. const Container = withRepositories(Component);
  37. jest.spyOn(api, 'requestPromise');
  38. jest.spyOn(Container.prototype, 'fetchRepositories');
  39. // Mount and run component
  40. render(<Container api={api} organization={organization} />);
  41. // Mount and run duplicates
  42. render(<Container api={api} organization={organization} />);
  43. render(<Container api={api} organization={organization} />);
  44. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  45. expect(Container.prototype.fetchRepositories).toHaveBeenCalledTimes(3);
  46. });
  47. /**
  48. * Same as 'prevents repeated calls', but with the async fetch/checks
  49. * happening on same tick.
  50. *
  51. * Additionally, this test checks that withRepositories.fetchRepositories does
  52. * not check for (store.orgSlug !== orgSlug) as the short-circuit does not
  53. * change the value for orgSlug
  54. */
  55. it('prevents simultaneous calls', function () {
  56. const Component = () => null;
  57. const Container = withRepositories(Component);
  58. jest.spyOn(api, 'requestPromise');
  59. jest.spyOn(Container.prototype, 'componentDidMount');
  60. // Mount and run duplicates
  61. render(<Container api={api} organization={organization} />);
  62. render(<Container api={api} organization={organization} />);
  63. render(<Container api={api} organization={organization} />);
  64. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  65. expect(Container.prototype.componentDidMount).toHaveBeenCalledTimes(3);
  66. });
  67. });