overview.polling.spec.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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: jest.Mock;
  21. let pollRequest: jest.Mock;
  22. beforeEach(() => {
  23. jest.useFakeTimers();
  24. });
  25. afterEach(() => {
  26. jest.useRealTimers();
  27. });
  28. const {organization, project, routerProps, routerContext} = initializeOrg({
  29. organization: {
  30. access: ['project: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: TestStubs.location({
  43. query: {query: 'is:unresolved'},
  44. search: 'query=is:unresolved',
  45. }),
  46. params: {},
  47. organization,
  48. };
  49. /* helpers */
  50. const renderComponent = async () => {
  51. render(<IssueList {...routerProps} {...defaultProps} />, {
  52. context: routerContext,
  53. });
  54. await Promise.resolve();
  55. jest.runAllTimers();
  56. };
  57. beforeEach(function () {
  58. // The tests fail because we have a "component update was not wrapped in act" error.
  59. // It should be safe to ignore this error, but we should remove the mock once we move to react testing library
  60. // eslint-disable-next-line no-console
  61. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  62. MockApiClient.clearMockResponses();
  63. MockApiClient.addMockResponse({
  64. url: '/organizations/org-slug/searches/',
  65. body: [savedSearch],
  66. });
  67. MockApiClient.addMockResponse({
  68. url: '/organizations/org-slug/recent-searches/',
  69. body: [],
  70. });
  71. MockApiClient.addMockResponse({
  72. url: '/organizations/org-slug/issues-count/',
  73. method: 'GET',
  74. body: [{}],
  75. });
  76. MockApiClient.addMockResponse({
  77. url: '/organizations/org-slug/processingissues/',
  78. method: 'GET',
  79. body: [
  80. {
  81. project: 'test-project',
  82. numIssues: 1,
  83. hasIssues: true,
  84. lastSeen: '2019-01-16T15:39:11.081Z',
  85. },
  86. ],
  87. });
  88. MockApiClient.addMockResponse({
  89. url: '/organizations/org-slug/tags/',
  90. method: 'GET',
  91. body: TestStubs.Tags(),
  92. });
  93. MockApiClient.addMockResponse({
  94. url: '/organizations/org-slug/users/',
  95. method: 'GET',
  96. body: [TestStubs.Member({projects: [project.slug]})],
  97. });
  98. MockApiClient.addMockResponse({
  99. url: '/organizations/org-slug/recent-searches/',
  100. method: 'GET',
  101. body: [],
  102. });
  103. MockApiClient.addMockResponse({
  104. url: '/organizations/org-slug/searches/',
  105. body: [savedSearch],
  106. });
  107. issuesRequest = MockApiClient.addMockResponse({
  108. url: '/organizations/org-slug/issues/',
  109. body: [group],
  110. headers: {
  111. Link: DEFAULT_LINKS_HEADER,
  112. 'X-Hits': '1',
  113. },
  114. });
  115. MockApiClient.addMockResponse({
  116. url: '/organizations/org-slug/issues-stats/',
  117. body: [TestStubs.GroupStats()],
  118. });
  119. pollRequest = MockApiClient.addMockResponse({
  120. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  121. body: [],
  122. headers: {
  123. Link: DEFAULT_LINKS_HEADER,
  124. 'X-Hits': '1',
  125. },
  126. });
  127. jest.mocked(StreamGroup).mockClear();
  128. TagStore.init();
  129. });
  130. afterEach(function () {
  131. MockApiClient.clearMockResponses();
  132. });
  133. it('toggles polling for new issues', async function () {
  134. await renderComponent();
  135. expect(issuesRequest).toHaveBeenCalledWith(
  136. expect.anything(),
  137. expect.objectContaining({
  138. // Should be called with default query
  139. data: expect.stringContaining('is%3Aunresolved'),
  140. })
  141. );
  142. // Enable realtime updates
  143. await userEvent.click(
  144. screen.getByRole('button', {name: 'Enable real-time updates'}),
  145. {delay: null}
  146. );
  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. await userEvent.click(screen.getByRole('button', {name: 'Pause real-time updates'}), {
  154. delay: null,
  155. });
  156. jest.advanceTimersByTime(12001);
  157. expect(pollRequest).toHaveBeenCalledTimes(2);
  158. });
  159. it('displays new group and pagination caption correctly', async function () {
  160. pollRequest = MockApiClient.addMockResponse({
  161. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  162. body: [group2],
  163. headers: {
  164. Link: DEFAULT_LINKS_HEADER,
  165. 'X-Hits': '2',
  166. },
  167. });
  168. await renderComponent();
  169. expect(screen.getByText(textWithMarkupMatcher('1-1 of 1'))).toBeInTheDocument();
  170. // Enable realtime updates
  171. await userEvent.click(
  172. screen.getByRole('button', {name: 'Enable real-time updates'}),
  173. {delay: null}
  174. );
  175. jest.advanceTimersByTime(3001);
  176. expect(pollRequest).toHaveBeenCalledTimes(1);
  177. // We mock out the stream group component and only render the ID as a testid
  178. await screen.findByTestId('2');
  179. expect(screen.getByText(textWithMarkupMatcher('1-2 of 2'))).toBeInTheDocument();
  180. });
  181. it('stops polling for new issues when endpoint returns a 401', async function () {
  182. pollRequest = MockApiClient.addMockResponse({
  183. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  184. body: [],
  185. statusCode: 401,
  186. });
  187. await renderComponent();
  188. // Enable real time control
  189. await userEvent.click(
  190. screen.getByRole('button', {name: 'Enable real-time updates'}),
  191. {delay: null}
  192. );
  193. // Each poll request gets delayed by additional 3s, up to max of 60s
  194. jest.advanceTimersByTime(3001);
  195. expect(pollRequest).toHaveBeenCalledTimes(1);
  196. jest.advanceTimersByTime(9001);
  197. expect(pollRequest).toHaveBeenCalledTimes(1);
  198. });
  199. it('stops polling for new issues when endpoint returns a 403', async function () {
  200. pollRequest = MockApiClient.addMockResponse({
  201. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  202. body: [],
  203. statusCode: 403,
  204. });
  205. await renderComponent();
  206. // Enable real time control
  207. await userEvent.click(
  208. screen.getByRole('button', {name: 'Enable real-time updates'}),
  209. {delay: null}
  210. );
  211. // Each poll request gets delayed by additional 3s, up to max of 60s
  212. jest.advanceTimersByTime(3001);
  213. expect(pollRequest).toHaveBeenCalledTimes(1);
  214. jest.advanceTimersByTime(9001);
  215. expect(pollRequest).toHaveBeenCalledTimes(1);
  216. });
  217. it('stops polling for new issues when endpoint returns a 404', async function () {
  218. pollRequest = MockApiClient.addMockResponse({
  219. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  220. body: [],
  221. statusCode: 404,
  222. });
  223. await renderComponent();
  224. // Enable real time control
  225. await userEvent.click(
  226. screen.getByRole('button', {name: 'Enable real-time updates'}),
  227. {delay: null}
  228. );
  229. // Each poll request gets delayed by additional 3s, up to max of 60s
  230. jest.advanceTimersByTime(3001);
  231. expect(pollRequest).toHaveBeenCalledTimes(1);
  232. jest.advanceTimersByTime(9001);
  233. expect(pollRequest).toHaveBeenCalledTimes(1);
  234. });
  235. });