overview.polling.spec.jsx 7.9 KB

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