overview.polling.spec.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. import {OrganizationContext} from '../organizationContext';
  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 issuesRequest;
  22. let pollRequest;
  23. const {organization, project, router, routerContext} = initializeOrg({
  24. organization: {
  25. access: ['releases'],
  26. },
  27. });
  28. const savedSearch = TestStubs.Search({
  29. id: '789',
  30. query: 'is:unresolved',
  31. name: 'Unresolved Issues',
  32. projectId: project.id,
  33. });
  34. const group = TestStubs.Group({project});
  35. const defaultProps = {
  36. location: {query: {query: 'is:unresolved'}, search: 'query=is:unresolved'},
  37. params: {orgId: organization.slug},
  38. organization,
  39. };
  40. /* helpers */
  41. const renderComponent = async ({params, location, ...p} = {}) => {
  42. const newRouter = {
  43. ...router,
  44. params: {
  45. ...router.params,
  46. ...params,
  47. },
  48. location: {
  49. ...router.location,
  50. ...location,
  51. },
  52. };
  53. render(
  54. <OrganizationContext.Provider value={TestStubs.Organization()}>
  55. <IssueList {...newRouter} {...defaultProps} {...p} />
  56. </OrganizationContext.Provider>,
  57. {context: routerContext}
  58. );
  59. await Promise.resolve();
  60. jest.runAllTimers();
  61. };
  62. beforeEach(function () {
  63. // The tests fail because we have a "component update was not wrapped in act" error.
  64. // It should be safe to ignore this error, but we should remove the mock once we move to react testing library
  65. // eslint-disable-next-line no-console
  66. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  67. MockApiClient.clearMockResponses();
  68. MockApiClient.addMockResponse({
  69. url: '/organizations/org-slug/searches/',
  70. body: [savedSearch],
  71. });
  72. MockApiClient.addMockResponse({
  73. url: '/organizations/org-slug/recent-searches/',
  74. body: [],
  75. });
  76. MockApiClient.addMockResponse({
  77. url: '/organizations/org-slug/issues-count/',
  78. method: 'GET',
  79. body: [{}],
  80. });
  81. MockApiClient.addMockResponse({
  82. url: '/organizations/org-slug/processingissues/',
  83. method: 'GET',
  84. body: [
  85. {
  86. project: 'test-project',
  87. numIssues: 1,
  88. hasIssues: true,
  89. lastSeen: '2019-01-16T15:39:11.081Z',
  90. },
  91. ],
  92. });
  93. MockApiClient.addMockResponse({
  94. url: '/organizations/org-slug/tags/',
  95. method: 'GET',
  96. body: TestStubs.Tags(),
  97. });
  98. MockApiClient.addMockResponse({
  99. url: '/organizations/org-slug/users/',
  100. method: 'GET',
  101. body: [TestStubs.Member({projects: [project.slug]})],
  102. });
  103. MockApiClient.addMockResponse({
  104. url: '/organizations/org-slug/recent-searches/',
  105. method: 'GET',
  106. body: [],
  107. });
  108. MockApiClient.addMockResponse({
  109. url: '/organizations/org-slug/searches/',
  110. body: [savedSearch],
  111. });
  112. issuesRequest = MockApiClient.addMockResponse({
  113. url: '/organizations/org-slug/issues/',
  114. body: [group],
  115. headers: {
  116. Link: DEFAULT_LINKS_HEADER,
  117. },
  118. });
  119. MockApiClient.addMockResponse({
  120. url: '/organizations/org-slug/issues-stats/',
  121. body: [TestStubs.GroupStats()],
  122. });
  123. pollRequest = MockApiClient.addMockResponse({
  124. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  125. body: [],
  126. headers: {
  127. Link: DEFAULT_LINKS_HEADER,
  128. },
  129. });
  130. StreamGroup.mockClear();
  131. TagStore.init();
  132. });
  133. afterEach(function () {
  134. MockApiClient.clearMockResponses();
  135. });
  136. it('toggles polling for new issues', async function () {
  137. await renderComponent();
  138. expect(issuesRequest).toHaveBeenCalledWith(
  139. expect.anything(),
  140. expect.objectContaining({
  141. // Should be called with default query
  142. data: expect.stringContaining('is%3Aunresolved'),
  143. })
  144. );
  145. // Enable realtime updates
  146. userEvent.click(screen.getByRole('button', {name: 'Enable real-time updates'}));
  147. // Each poll request gets delayed by additional 3s, up to max of 60s
  148. jest.advanceTimersByTime(3001);
  149. expect(pollRequest).toHaveBeenCalledTimes(1);
  150. jest.advanceTimersByTime(6001);
  151. expect(pollRequest).toHaveBeenCalledTimes(2);
  152. // Pauses
  153. userEvent.click(screen.getByRole('button', {name: 'Pause real-time updates'}));
  154. jest.advanceTimersByTime(12001);
  155. expect(pollRequest).toHaveBeenCalledTimes(2);
  156. });
  157. it('stops polling for new issues when endpoint returns a 401', async function () {
  158. pollRequest = MockApiClient.addMockResponse({
  159. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  160. body: [],
  161. statusCode: 401,
  162. });
  163. await renderComponent();
  164. // Enable real time control
  165. userEvent.click(screen.getByRole('button', {name: 'Enable real-time updates'}));
  166. // Each poll request gets delayed by additional 3s, up to max of 60s
  167. jest.advanceTimersByTime(3001);
  168. expect(pollRequest).toHaveBeenCalledTimes(1);
  169. jest.advanceTimersByTime(9001);
  170. expect(pollRequest).toHaveBeenCalledTimes(1);
  171. });
  172. it('stops polling for new issues when endpoint returns a 403', async function () {
  173. pollRequest = MockApiClient.addMockResponse({
  174. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  175. body: [],
  176. statusCode: 403,
  177. });
  178. await renderComponent();
  179. // Enable real time control
  180. userEvent.click(screen.getByRole('button', {name: 'Enable real-time updates'}));
  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 404', async function () {
  188. pollRequest = MockApiClient.addMockResponse({
  189. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  190. body: [],
  191. statusCode: 404,
  192. });
  193. await renderComponent();
  194. // Enable real time control
  195. userEvent.click(screen.getByRole('button', {name: 'Enable real-time updates'}));
  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. });