committers.spec.jsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {getCommitters} from 'sentry/actionCreators/committers';
  2. import CommitterActions from 'sentry/actions/committerActions';
  3. import CommitterStore, {getCommitterStoreKey} from 'sentry/stores/committerStore';
  4. describe('CommitterActionCreator', function () {
  5. const organization = TestStubs.Organization();
  6. const project = TestStubs.Project();
  7. const event = TestStubs.Event();
  8. const storeKey = getCommitterStoreKey(organization.slug, project.slug, event.id);
  9. const endpoint = `/projects/${organization.slug}/${project.slug}/events/${event.id}/committers/`;
  10. const api = new MockApiClient();
  11. const mockData = {
  12. committers: [
  13. {
  14. author: TestStubs.CommitAuthor(),
  15. commits: [TestStubs.Commit()],
  16. },
  17. ],
  18. };
  19. let mockResponse;
  20. beforeEach(() => {
  21. MockApiClient.clearMockResponses();
  22. mockResponse = MockApiClient.addMockResponse({
  23. url: endpoint,
  24. body: mockData,
  25. });
  26. CommitterStore.init();
  27. jest.restoreAllMocks();
  28. jest.spyOn(CommitterActions, 'load');
  29. jest.spyOn(CommitterActions, 'loadSuccess');
  30. /**
  31. * XXX(leedongwei): We would want to ensure that Store methods are not
  32. * called to be 100% sure that the short-circuit is happening correctly.
  33. *
  34. * However, it seems like we cannot attach a listener to the method
  35. * See: https://github.com/reflux/refluxjs/issues/139#issuecomment-64495623
  36. */
  37. // jest.spyOn(CommitterStore, 'load');
  38. // jest.spyOn(CommitterStore, 'loadSuccess');
  39. });
  40. /**
  41. * XXX(leedongwei): I wanted to separate the ticks and run tests to assert the
  42. * state change at every tick but it is incredibly flakey.
  43. */
  44. it('fetches a Committer and emits actions', async () => {
  45. getCommitters(api, {
  46. orgSlug: organization.slug,
  47. projectSlug: project.slug,
  48. eventId: event.id,
  49. }); // Fire Action.load
  50. expect(CommitterActions.load).toHaveBeenCalledWith(
  51. organization.slug,
  52. project.slug,
  53. event.id
  54. );
  55. expect(CommitterActions.loadSuccess).not.toHaveBeenCalled();
  56. await tick(); // Run Store.load and fire Action.loadSuccess
  57. await tick(); // Run Store.loadSuccess
  58. expect(mockResponse).toHaveBeenCalledWith(endpoint, expect.anything());
  59. expect(CommitterActions.loadSuccess).toHaveBeenCalledWith(
  60. organization.slug,
  61. project.slug,
  62. event.id,
  63. mockData.committers
  64. );
  65. expect(CommitterStore.state).toEqual({
  66. [storeKey]: {
  67. committers: mockData.committers,
  68. committersLoading: false,
  69. committersError: undefined,
  70. },
  71. });
  72. });
  73. it('short-circuits the JS event loop', () => {
  74. expect(CommitterStore.state.committersLoading).toEqual(undefined);
  75. getCommitters(api, {
  76. orgSlug: organization.slug,
  77. projectSlug: project.slug,
  78. eventId: event.id,
  79. }); // Fire Action.load
  80. expect(CommitterActions.load).toHaveBeenCalled();
  81. // expect(CommitterStore.load).not.toHaveBeenCalled();
  82. expect(CommitterStore.state[storeKey].committersLoading).toEqual(true); // Short-circuit
  83. });
  84. });