withRepositories.spec.jsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. afterEach(() => {
  20. RepositoryStore.teardown();
  21. });
  22. it('adds repositories prop', async () => {
  23. const Component = () => null;
  24. const Container = withRepositories(Component);
  25. const wrapper = mountWithTheme(<Container api={api} organization={organization} />);
  26. await tick(); // Run Store.loadRepositories
  27. await tick(); // Run Store.loadRepositoriesSuccess
  28. wrapper.update(); // Re-render component with Store data
  29. const mountedComponent = wrapper.find(Component);
  30. expect(mountedComponent.prop('repositories')).toEqual(mockData);
  31. expect(mountedComponent.prop('repositoriesLoading')).toEqual(false);
  32. expect(mountedComponent.prop('repositoriesError')).toEqual(undefined);
  33. });
  34. it('prevents repeated calls', async () => {
  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. mountWithTheme(<Container api={api} organization={organization} />);
  41. await tick();
  42. await tick();
  43. // Mount and run duplicates
  44. mountWithTheme(<Container api={api} organization={organization} />);
  45. await tick();
  46. mountWithTheme(<Container api={api} organization={organization} />);
  47. await tick();
  48. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  49. expect(Container.prototype.fetchRepositories).toHaveBeenCalledTimes(3);
  50. });
  51. /**
  52. * Same as 'prevents repeated calls', but with the async fetch/checks
  53. * happening on same tick.
  54. *
  55. * Additionally, this test checks that withRepositories.fetchRepositories does
  56. * not check for (store.orgSlug !== orgSlug) as the short-circuit does not
  57. * change the value for orgSlug
  58. */
  59. it('prevents simultaneous calls', async () => {
  60. const Component = () => null;
  61. const Container = withRepositories(Component);
  62. jest.spyOn(api, 'requestPromise');
  63. jest.spyOn(Container.prototype, 'componentDidMount');
  64. // Mount and run duplicates
  65. mountWithTheme(<Container api={api} organization={organization} />);
  66. mountWithTheme(<Container api={api} organization={organization} />);
  67. mountWithTheme(<Container api={api} organization={organization} />);
  68. await tick();
  69. await tick();
  70. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  71. expect(Container.prototype.componentDidMount).toHaveBeenCalledTimes(3);
  72. });
  73. });