overview.polling.spec.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {act} from 'sentry-test/reactTestingLibrary';
  4. import StreamGroup from 'sentry/components/stream/group';
  5. import TagStore from 'sentry/stores/tagStore';
  6. import IssueList from 'sentry/views/issueList/overview';
  7. import {OrganizationContext} from '../organizationContext';
  8. // Mock <IssueListSidebar> (need <IssueListActions> to toggling real time polling)
  9. jest.mock('sentry/views/issueList/sidebar', () => jest.fn(() => null));
  10. jest.mock('sentry/views/issueList/filters', () => jest.fn(() => null));
  11. jest.mock('sentry/components/stream/group', () => jest.fn(() => null));
  12. jest.mock('js-cookie', () => ({
  13. get: jest.fn(),
  14. set: jest.fn(),
  15. }));
  16. const PREVIOUS_PAGE_CURSOR = '1443575731';
  17. const DEFAULT_LINKS_HEADER =
  18. `<http://127.0.0.1:8000/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1>; rel="previous"; results="false"; cursor="${PREVIOUS_PAGE_CURSOR}:0:1", ` +
  19. '<http://127.0.0.1:8000/api/0/organizations/org-slug/issues/?cursor=1443575000:0:0>; rel="next"; results="true"; cursor="1443575000:0:0"';
  20. jest.useFakeTimers();
  21. describe('IssueList -> Polling', function () {
  22. let wrapper;
  23. let issuesRequest;
  24. let pollRequest;
  25. const {organization, project, router, routerContext} = initializeOrg({
  26. organization: {
  27. access: ['releases'],
  28. },
  29. });
  30. const savedSearch = TestStubs.Search({
  31. id: '789',
  32. query: 'is:unresolved',
  33. name: 'Unresolved Issues',
  34. projectId: project.id,
  35. });
  36. const group = TestStubs.Group({project});
  37. const defaultProps = {
  38. location: {query: {query: 'is:unresolved'}, search: 'query=is:unresolved'},
  39. params: {orgId: organization.slug},
  40. organization,
  41. };
  42. /* helpers */
  43. const createWrapper = async ({params, location, ...p} = {}) => {
  44. const newRouter = {
  45. ...router,
  46. params: {
  47. ...router.params,
  48. ...params,
  49. },
  50. location: {
  51. ...router.location,
  52. ...location,
  53. },
  54. };
  55. wrapper = mountWithTheme(
  56. <OrganizationContext.Provider value={TestStubs.Organization()}>
  57. <IssueList {...newRouter} {...defaultProps} {...p} />
  58. </OrganizationContext.Provider>,
  59. routerContext
  60. );
  61. await act(async () => {
  62. await Promise.resolve();
  63. jest.runAllTimers();
  64. });
  65. wrapper.update();
  66. return wrapper;
  67. };
  68. beforeEach(function () {
  69. // The tests fail because we have a "component update was not wrapped in act" error.
  70. // It should be safe to ignore this error, but we should remove the mock once we move to react testing library
  71. // eslint-disable-next-line no-console
  72. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  73. MockApiClient.clearMockResponses();
  74. MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/searches/',
  76. body: [savedSearch],
  77. });
  78. MockApiClient.addMockResponse({
  79. url: '/organizations/org-slug/recent-searches/',
  80. body: [],
  81. });
  82. MockApiClient.addMockResponse({
  83. url: '/organizations/org-slug/issues-count/',
  84. method: 'GET',
  85. body: [{}],
  86. });
  87. MockApiClient.addMockResponse({
  88. url: '/organizations/org-slug/processingissues/',
  89. method: 'GET',
  90. body: [
  91. {
  92. project: 'test-project',
  93. numIssues: 1,
  94. hasIssues: true,
  95. lastSeen: '2019-01-16T15:39:11.081Z',
  96. },
  97. ],
  98. });
  99. MockApiClient.addMockResponse({
  100. url: '/organizations/org-slug/tags/',
  101. method: 'GET',
  102. body: TestStubs.Tags(),
  103. });
  104. MockApiClient.addMockResponse({
  105. url: '/organizations/org-slug/users/',
  106. method: 'GET',
  107. body: [TestStubs.Member({projects: [project.slug]})],
  108. });
  109. MockApiClient.addMockResponse({
  110. url: '/organizations/org-slug/recent-searches/',
  111. method: 'GET',
  112. body: [],
  113. });
  114. MockApiClient.addMockResponse({
  115. url: '/organizations/org-slug/searches/',
  116. body: [savedSearch],
  117. });
  118. issuesRequest = MockApiClient.addMockResponse({
  119. url: '/organizations/org-slug/issues/',
  120. body: [group],
  121. headers: {
  122. Link: DEFAULT_LINKS_HEADER,
  123. },
  124. });
  125. const groupStats = TestStubs.GroupStats();
  126. MockApiClient.addMockResponse({
  127. url: '/organizations/org-slug/issues-stats/',
  128. body: [groupStats],
  129. });
  130. pollRequest = MockApiClient.addMockResponse({
  131. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  132. body: [],
  133. headers: {
  134. Link: DEFAULT_LINKS_HEADER,
  135. },
  136. });
  137. StreamGroup.mockClear();
  138. TagStore.init();
  139. });
  140. afterEach(function () {
  141. MockApiClient.clearMockResponses();
  142. if (wrapper) {
  143. wrapper.unmount();
  144. }
  145. wrapper = null;
  146. });
  147. it('toggles polling for new issues', async function () {
  148. await createWrapper();
  149. expect(issuesRequest).toHaveBeenCalledWith(
  150. expect.anything(),
  151. expect.objectContaining({
  152. // Should be called with default query
  153. data: expect.stringContaining('is%3Aunresolved'),
  154. })
  155. );
  156. // Enable real time control
  157. expect(wrapper.find('button[data-test-id="real-time"] IconPlay')).toHaveLength(1);
  158. wrapper.find('button[data-test-id="real-time"]').simulate('click');
  159. expect(wrapper.find('button[data-test-id="real-time"] IconPlay')).toHaveLength(0);
  160. // Each poll request gets delayed by additional 3s, up to max of 60s
  161. jest.advanceTimersByTime(3001);
  162. expect(pollRequest).toHaveBeenCalledTimes(1);
  163. jest.advanceTimersByTime(6001);
  164. expect(pollRequest).toHaveBeenCalledTimes(2);
  165. // Pauses
  166. wrapper.find('button[data-test-id="real-time"]').simulate('click');
  167. expect(wrapper.find('button[data-test-id="real-time"] IconPlay')).toHaveLength(1);
  168. jest.advanceTimersByTime(12001);
  169. expect(pollRequest).toHaveBeenCalledTimes(2);
  170. });
  171. it('stops polling for new issues when endpoint returns a 401', async function () {
  172. pollRequest = MockApiClient.addMockResponse({
  173. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  174. body: [],
  175. statusCode: 401,
  176. });
  177. await createWrapper();
  178. // Enable real time control
  179. wrapper.find('button[data-test-id="real-time"]').simulate('click');
  180. expect(wrapper.find('button[data-test-id="real-time"] IconPlay')).toHaveLength(0);
  181. // Each poll request gets delayed by additional 3s, up to max of 60s
  182. jest.advanceTimersByTime(3001);
  183. expect(pollRequest).toHaveBeenCalledTimes(1);
  184. jest.advanceTimersByTime(9001);
  185. expect(pollRequest).toHaveBeenCalledTimes(1);
  186. });
  187. it('stops polling for new issues when endpoint returns a 403', async function () {
  188. pollRequest = MockApiClient.addMockResponse({
  189. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  190. body: [],
  191. statusCode: 403,
  192. });
  193. await createWrapper();
  194. // Enable real time control
  195. expect(wrapper.find('button[data-test-id="real-time"] IconPlay')).toHaveLength(1);
  196. wrapper.find('button[data-test-id="real-time"]').simulate('click');
  197. expect(wrapper.find('button[data-test-id="real-time"] IconPlay')).toHaveLength(0);
  198. // Each poll request gets delayed by additional 3s, up to max of 60s
  199. jest.advanceTimersByTime(3001);
  200. expect(pollRequest).toHaveBeenCalledTimes(1);
  201. jest.advanceTimersByTime(9001);
  202. expect(pollRequest).toHaveBeenCalledTimes(1);
  203. });
  204. it('stops polling for new issues when endpoint returns a 404', async function () {
  205. pollRequest = MockApiClient.addMockResponse({
  206. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  207. body: [],
  208. statusCode: 404,
  209. });
  210. await createWrapper();
  211. // Enable real time control
  212. wrapper.find('button[data-test-id="real-time"]').simulate('click');
  213. expect(wrapper.find('button[data-test-id="real-time"] IconPlay')).toHaveLength(0);
  214. // Each poll request gets delayed by additional 3s, up to max of 60s
  215. jest.advanceTimersByTime(3001);
  216. expect(pollRequest).toHaveBeenCalledTimes(1);
  217. jest.advanceTimersByTime(9001);
  218. expect(pollRequest).toHaveBeenCalledTimes(1);
  219. });
  220. });