withCommitters.spec.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import CommitterStore from 'sentry/stores/committerStore';
  3. import withCommitters from 'sentry/utils/withCommitters';
  4. describe('withCommitters HoC', function () {
  5. const organization = TestStubs.Organization();
  6. const project = TestStubs.Project();
  7. const event = TestStubs.Event();
  8. const group = TestStubs.Group({firstRelease: {}});
  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. beforeEach(() => {
  20. MockApiClient.clearMockResponses();
  21. MockApiClient.addMockResponse({
  22. url: endpoint,
  23. body: mockData,
  24. });
  25. jest.restoreAllMocks();
  26. CommitterStore.init();
  27. });
  28. it('adds committers prop', async () => {
  29. const Component = () => null;
  30. const Container = withCommitters(Component);
  31. const wrapper = mountWithTheme(
  32. <Container
  33. api={api}
  34. organization={organization}
  35. project={project}
  36. event={event}
  37. group={group}
  38. />
  39. );
  40. await tick(); // Run Store.load
  41. await tick(); // Run Store.loadSuccess
  42. wrapper.update(); // Re-render component with Store data
  43. const mountedComponent = wrapper.find(Component);
  44. expect(mountedComponent.prop('committers')).toEqual(mockData.committers);
  45. expect(mountedComponent.prop('committersLoading')).toEqual(undefined);
  46. expect(mountedComponent.prop('committersError')).toEqual(undefined);
  47. });
  48. it('prevents repeated calls', async () => {
  49. const Component = () => null;
  50. const Container = withCommitters(Component);
  51. jest.spyOn(api, 'requestPromise');
  52. jest.spyOn(Container.prototype, 'fetchCommitters');
  53. // Mount and run component
  54. mountWithTheme(
  55. <Container
  56. api={api}
  57. organization={organization}
  58. project={project}
  59. event={event}
  60. group={group}
  61. />
  62. );
  63. await tick();
  64. await tick();
  65. // Mount and run duplicates
  66. mountWithTheme(
  67. <Container
  68. api={api}
  69. organization={organization}
  70. project={project}
  71. event={event}
  72. group={group}
  73. />
  74. );
  75. await tick();
  76. mountWithTheme(
  77. <Container
  78. api={api}
  79. organization={organization}
  80. project={project}
  81. event={event}
  82. group={group}
  83. />
  84. );
  85. await tick();
  86. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  87. expect(Container.prototype.fetchCommitters).toHaveBeenCalledTimes(3);
  88. });
  89. /**
  90. * Same as 'prevents repeated calls', but with the async fetch/checks
  91. * happening on same tick.
  92. *
  93. * Additionally, this test checks that withCommitters.fetchCommitters does
  94. * not check for (store.orgSlug !== orgSlug) as the short-circuit does not
  95. * change the value for orgSlug
  96. */
  97. it('prevents simultaneous calls', async () => {
  98. const Component = () => null;
  99. const Container = withCommitters(Component);
  100. jest.spyOn(api, 'requestPromise');
  101. jest.spyOn(Container.prototype, 'fetchCommitters');
  102. // Mount and run duplicates
  103. mountWithTheme(
  104. <Container
  105. api={api}
  106. organization={organization}
  107. project={project}
  108. event={event}
  109. group={group}
  110. />
  111. );
  112. mountWithTheme(
  113. <Container
  114. api={api}
  115. organization={organization}
  116. project={project}
  117. event={event}
  118. group={group}
  119. />
  120. );
  121. mountWithTheme(
  122. <Container
  123. api={api}
  124. organization={organization}
  125. project={project}
  126. event={event}
  127. group={group}
  128. />
  129. );
  130. await tick();
  131. await tick();
  132. expect(api.requestPromise).toHaveBeenCalledTimes(1);
  133. expect(Container.prototype.fetchCommitters).toHaveBeenCalledTimes(3);
  134. });
  135. });