withRepositories.spec.jsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  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 () => {
  20. const Component = () => null;
  21. const Container = withRepositories(Component);
  22. const wrapper = mountWithTheme(<Container api={api} organization={organization} />);
  23. await tick(); // Run Store.loadRepositories
  24. await tick(); // Run Store.loadRepositoriesSuccess
  25. wrapper.update(); // Re-render component with Store data
  26. const mountedComponent = wrapper.find(Component);
  27. expect(mountedComponent.prop('repositories')).toEqual(mockData);
  28. expect(mountedComponent.prop('repositoriesLoading')).toEqual(false);
  29. expect(mountedComponent.prop('repositoriesError')).toEqual(undefined);
  30. });
  31. it('prevents repeated calls', async () => {
  32. const Component = () => null;
  33. const Container = withRepositories(Component);
  34. jest.spyOn(api, 'requestPromise');
  35. jest.spyOn(Container.prototype, 'fetchRepositories');
  36. // Mount and run component
  37. mountWithTheme(<Container api={api} organization={organization} />);
  38. await tick();
  39. await tick();
  40. // Mount and run duplicates
  41. mountWithTheme(<Container api={api} organization={organization} />);
  42. await tick();
  43. mountWithTheme(<Container api={api} organization={organization} />);
  44. await tick();
  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', async () => {
  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. mountWithTheme(<Container api={api} organization={organization} />);
  63. mountWithTheme(<Container api={api} organization={organization} />);
  64. mountWithTheme(<Container api={api} organization={organization} />);
  65. await tick();
  66. await tick();
  67. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  68. expect(Container.prototype.componentDidMount).toHaveBeenCalledTimes(3);
  69. });
  70. });