withRepositories.spec.tsx 2.7 KB

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