repositories.spec.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {getRepositories} from 'sentry/actionCreators/repositories';
  2. import RepositoryActions from 'sentry/actions/repositoryActions';
  3. import RepositoryStore from 'sentry/stores/repositoryStore';
  4. describe('RepositoryActionCreator', function () {
  5. const orgSlug = 'myOrg';
  6. const repoUrl = `/organizations/${orgSlug}/repos/`;
  7. const api = new MockApiClient();
  8. const mockData = [{id: '1'}];
  9. let mockResponse;
  10. beforeEach(() => {
  11. MockApiClient.clearMockResponses();
  12. mockResponse = MockApiClient.addMockResponse({
  13. url: repoUrl,
  14. body: mockData,
  15. });
  16. RepositoryStore.resetRepositories();
  17. jest.restoreAllMocks();
  18. jest.spyOn(RepositoryActions, 'loadRepositories');
  19. jest.spyOn(RepositoryActions, 'loadRepositoriesSuccess');
  20. /**
  21. * XXX(leedongwei): We would want to ensure that Store methods are not
  22. * called to be 100% sure that the short-circuit is happening correctly.
  23. *
  24. * However, it seems like we cannot attach a listener to the method
  25. * See: https://github.com/reflux/refluxjs/issues/139#issuecomment-64495623
  26. */
  27. // jest.spyOn(RepositoryStore, 'loadRepositories');
  28. // jest.spyOn(RepositoryStore, 'loadRepositoriesSuccess');
  29. });
  30. /**
  31. * XXX(leedongwei): I wanted to separate the ticks and run tests to assert the
  32. * state change at every tick but it is incredibly flakey.
  33. */
  34. it('fetches a Repository and emits actions', async () => {
  35. getRepositories(api, {orgSlug}); // Fire Action.loadRepositories
  36. expect(RepositoryActions.loadRepositories).toHaveBeenCalledWith(orgSlug);
  37. expect(RepositoryActions.loadRepositoriesSuccess).not.toHaveBeenCalled();
  38. await tick(); // Run Store.loadRepositories and fire Action.loadRepositoriesSuccess
  39. await tick(); // Run Store.loadRepositoriesSuccess
  40. expect(mockResponse).toHaveBeenCalledWith(repoUrl, expect.anything());
  41. expect(RepositoryActions.loadRepositoriesSuccess).toHaveBeenCalledWith(mockData);
  42. expect(RepositoryStore.state.orgSlug).toEqual(orgSlug);
  43. expect(RepositoryStore.state.repositories).toEqual(mockData);
  44. expect(RepositoryStore.state.repositoriesLoading).toEqual(false);
  45. });
  46. it('short-circuits the JS event loop', () => {
  47. expect(RepositoryStore.state.repositoriesLoading).toEqual(undefined);
  48. getRepositories(api, {orgSlug}); // Fire Action.loadRepositories
  49. expect(RepositoryActions.loadRepositories).toHaveBeenCalled();
  50. // expect(RepositoryStore.loadRepositories).not.toHaveBeenCalled();
  51. expect(RepositoryStore.state.repositoriesLoading).toEqual(true); // Short-circuit
  52. });
  53. });