overview.polling.spec.jsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import {textWithMarkupMatcher} from 'sentry-test/utils';
  4. import StreamGroup from 'sentry/components/stream/group';
  5. import TagStore from 'sentry/stores/tagStore';
  6. import IssueList from 'sentry/views/issueList/overview';
  7. jest.mock('sentry/views/issueList/filters', () => jest.fn(() => null));
  8. jest.mock('sentry/components/stream/group', () =>
  9. jest.fn(({id}) => <div data-test-id={id} />)
  10. );
  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. describe('IssueList -> Polling', function () {
  20. let issuesRequest;
  21. let pollRequest;
  22. beforeEach(() => {
  23. jest.useFakeTimers();
  24. });
  25. afterEach(() => {
  26. jest.useRealTimers();
  27. });
  28. const {organization, project, router, routerContext} = initializeOrg({
  29. organization: {
  30. access: ['releases'],
  31. },
  32. });
  33. const savedSearch = TestStubs.Search({
  34. id: '789',
  35. query: 'is:unresolved',
  36. name: 'Unresolved Issues',
  37. projectId: project.id,
  38. });
  39. const group = TestStubs.Group({project});
  40. const group2 = TestStubs.Group({project, id: 2});
  41. const defaultProps = {
  42. location: {query: {query: 'is:unresolved'}, search: 'query=is:unresolved'},
  43. params: {orgId: organization.slug},
  44. organization,
  45. };
  46. /* helpers */
  47. const renderComponent = async ({params, location, ...p} = {}) => {
  48. const newRouter = {
  49. ...router,
  50. params: {
  51. ...router.params,
  52. ...params,
  53. },
  54. location: {
  55. ...router.location,
  56. ...location,
  57. },
  58. };
  59. render(<IssueList {...newRouter} {...defaultProps} {...p} />, {
  60. context: routerContext,
  61. });
  62. await Promise.resolve();
  63. jest.runAllTimers();
  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. 'X-Hits': 1,
  121. },
  122. });
  123. MockApiClient.addMockResponse({
  124. url: '/organizations/org-slug/issues-stats/',
  125. body: [TestStubs.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. 'X-Hits': 1,
  133. },
  134. });
  135. StreamGroup.mockClear();
  136. TagStore.init();
  137. });
  138. afterEach(function () {
  139. MockApiClient.clearMockResponses();
  140. });
  141. it('toggles polling for new issues', async function () {
  142. await renderComponent();
  143. expect(issuesRequest).toHaveBeenCalledWith(
  144. expect.anything(),
  145. expect.objectContaining({
  146. // Should be called with default query
  147. data: expect.stringContaining('is%3Aunresolved'),
  148. })
  149. );
  150. // Enable realtime updates
  151. await userEvent.click(
  152. screen.getByRole('button', {name: 'Enable real-time updates'}),
  153. {delay: null}
  154. );
  155. // Each poll request gets delayed by additional 3s, up to max of 60s
  156. jest.advanceTimersByTime(3001);
  157. expect(pollRequest).toHaveBeenCalledTimes(1);
  158. jest.advanceTimersByTime(6001);
  159. expect(pollRequest).toHaveBeenCalledTimes(2);
  160. // Pauses
  161. await userEvent.click(screen.getByRole('button', {name: 'Pause real-time updates'}), {
  162. delay: null,
  163. });
  164. jest.advanceTimersByTime(12001);
  165. expect(pollRequest).toHaveBeenCalledTimes(2);
  166. });
  167. it('displays new group and pagination caption correctly', async function () {
  168. pollRequest = MockApiClient.addMockResponse({
  169. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  170. body: [group2],
  171. headers: {
  172. Link: DEFAULT_LINKS_HEADER,
  173. 'X-Hits': 2,
  174. },
  175. });
  176. await renderComponent();
  177. expect(screen.getByText(textWithMarkupMatcher('1-1 of 1'))).toBeInTheDocument();
  178. // Enable realtime updates
  179. await userEvent.click(
  180. screen.getByRole('button', {name: 'Enable real-time updates'}),
  181. {delay: null}
  182. );
  183. jest.advanceTimersByTime(3001);
  184. expect(pollRequest).toHaveBeenCalledTimes(1);
  185. // We mock out the stream group component and only render the ID as a testid
  186. await screen.findByTestId('2');
  187. expect(screen.getByText(textWithMarkupMatcher('1-2 of 2'))).toBeInTheDocument();
  188. });
  189. it('stops polling for new issues when endpoint returns a 401', async function () {
  190. pollRequest = MockApiClient.addMockResponse({
  191. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  192. body: [],
  193. statusCode: 401,
  194. });
  195. await renderComponent();
  196. // Enable real time control
  197. await userEvent.click(
  198. screen.getByRole('button', {name: 'Enable real-time updates'}),
  199. {delay: null}
  200. );
  201. // Each poll request gets delayed by additional 3s, up to max of 60s
  202. jest.advanceTimersByTime(3001);
  203. expect(pollRequest).toHaveBeenCalledTimes(1);
  204. jest.advanceTimersByTime(9001);
  205. expect(pollRequest).toHaveBeenCalledTimes(1);
  206. });
  207. it('stops polling for new issues when endpoint returns a 403', async function () {
  208. pollRequest = MockApiClient.addMockResponse({
  209. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  210. body: [],
  211. statusCode: 403,
  212. });
  213. await renderComponent();
  214. // Enable real time control
  215. await userEvent.click(
  216. screen.getByRole('button', {name: 'Enable real-time updates'}),
  217. {delay: null}
  218. );
  219. // Each poll request gets delayed by additional 3s, up to max of 60s
  220. jest.advanceTimersByTime(3001);
  221. expect(pollRequest).toHaveBeenCalledTimes(1);
  222. jest.advanceTimersByTime(9001);
  223. expect(pollRequest).toHaveBeenCalledTimes(1);
  224. });
  225. it('stops polling for new issues when endpoint returns a 404', async function () {
  226. pollRequest = MockApiClient.addMockResponse({
  227. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  228. body: [],
  229. statusCode: 404,
  230. });
  231. await renderComponent();
  232. // Enable real time control
  233. await userEvent.click(
  234. screen.getByRole('button', {name: 'Enable real-time updates'}),
  235. {delay: null}
  236. );
  237. // Each poll request gets delayed by additional 3s, up to max of 60s
  238. jest.advanceTimersByTime(3001);
  239. expect(pollRequest).toHaveBeenCalledTimes(1);
  240. jest.advanceTimersByTime(9001);
  241. expect(pollRequest).toHaveBeenCalledTimes(1);
  242. });
  243. });