overview.polling.spec.jsx 7.2 KB

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