repositories.spec.jsx 2.5 KB

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