committers.spec.jsx 3.0 KB

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