withRepositories.spec.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. function Component() {
  36. return null;
  37. }
  38. const Container = withRepositories(Component);
  39. jest.spyOn(api, 'requestPromise');
  40. jest.spyOn(Container.prototype, 'fetchRepositories');
  41. // Mount and run component
  42. render(<Container api={api} organization={organization} />);
  43. // Mount and run duplicates
  44. render(<Container api={api} organization={organization} />);
  45. render(<Container api={api} organization={organization} />);
  46. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  47. expect(Container.prototype.fetchRepositories).toHaveBeenCalledTimes(3);
  48. });
  49. /**
  50. * Same as 'prevents repeated calls', but with the async fetch/checks
  51. * happening on same tick.
  52. *
  53. * Additionally, this test checks that withRepositories.fetchRepositories does
  54. * not check for (store.orgSlug !== orgSlug) as the short-circuit does not
  55. * change the value for orgSlug
  56. */
  57. it('prevents simultaneous calls', function () {
  58. function Component() {
  59. return null;
  60. }
  61. const Container = withRepositories(Component);
  62. jest.spyOn(api, 'requestPromise');
  63. jest.spyOn(Container.prototype, 'componentDidMount');
  64. // Mount and run duplicates
  65. render(<Container api={api} organization={organization} />);
  66. render(<Container api={api} organization={organization} />);
  67. render(<Container api={api} organization={organization} />);
  68. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  69. expect(Container.prototype.componentDidMount).toHaveBeenCalledTimes(3);
  70. });
  71. });