overview.polling.spec.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import {GroupStats} from 'sentry-fixture/groupStats';
  2. import {Search} from 'sentry-fixture/search';
  3. import {Tags} from 'sentry-fixture/tags';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  6. import {textWithMarkupMatcher} from 'sentry-test/utils';
  7. import StreamGroup from 'sentry/components/stream/group';
  8. import TagStore from 'sentry/stores/tagStore';
  9. import IssueList from 'sentry/views/issueList/overview';
  10. jest.mock('sentry/views/issueList/filters', () => jest.fn(() => null));
  11. jest.mock('sentry/components/stream/group', () =>
  12. jest.fn(({id}) => <div data-test-id={id} />)
  13. );
  14. jest.mock('js-cookie', () => ({
  15. get: jest.fn(),
  16. set: jest.fn(),
  17. }));
  18. const PREVIOUS_PAGE_CURSOR = '1443575731';
  19. const DEFAULT_LINKS_HEADER =
  20. `<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", ` +
  21. '<http://127.0.0.1:8000/api/0/organizations/org-slug/issues/?cursor=1443575000:0:0>; rel="next"; results="true"; cursor="1443575000:0:0"';
  22. describe('IssueList -> Polling', function () {
  23. let issuesRequest: jest.Mock;
  24. let pollRequest: jest.Mock;
  25. beforeEach(() => {
  26. jest.useFakeTimers();
  27. });
  28. afterEach(() => {
  29. jest.useRealTimers();
  30. });
  31. const {organization, project, routerProps, routerContext} = initializeOrg({
  32. organization: {
  33. access: ['project:releases'],
  34. },
  35. });
  36. const savedSearch = Search({
  37. id: '789',
  38. query: 'is:unresolved',
  39. name: 'Unresolved Issues',
  40. });
  41. const group = TestStubs.Group({project});
  42. const group2 = TestStubs.Group({project, id: 2});
  43. const defaultProps = {
  44. location: TestStubs.location({
  45. query: {query: 'is:unresolved'},
  46. search: 'query=is:unresolved',
  47. }),
  48. params: {},
  49. organization,
  50. };
  51. /* helpers */
  52. const renderComponent = async () => {
  53. render(<IssueList {...routerProps} {...defaultProps} />, {
  54. context: routerContext,
  55. });
  56. await Promise.resolve();
  57. jest.runAllTimers();
  58. };
  59. beforeEach(function () {
  60. // The tests fail because we have a "component update was not wrapped in act" error.
  61. // It should be safe to ignore this error, but we should remove the mock once we move to react testing library
  62. // eslint-disable-next-line no-console
  63. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  64. MockApiClient.clearMockResponses();
  65. MockApiClient.addMockResponse({
  66. url: '/organizations/org-slug/searches/',
  67. body: [savedSearch],
  68. });
  69. MockApiClient.addMockResponse({
  70. url: '/organizations/org-slug/recent-searches/',
  71. body: [],
  72. });
  73. MockApiClient.addMockResponse({
  74. url: '/organizations/org-slug/issues-count/',
  75. method: 'GET',
  76. body: [{}],
  77. });
  78. MockApiClient.addMockResponse({
  79. url: '/organizations/org-slug/processingissues/',
  80. method: 'GET',
  81. body: [
  82. {
  83. project: 'test-project',
  84. numIssues: 1,
  85. hasIssues: true,
  86. lastSeen: '2019-01-16T15:39:11.081Z',
  87. },
  88. ],
  89. });
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/tags/',
  92. method: 'GET',
  93. body: Tags(),
  94. });
  95. MockApiClient.addMockResponse({
  96. url: '/organizations/org-slug/users/',
  97. method: 'GET',
  98. body: [TestStubs.Member({projects: [project.slug]})],
  99. });
  100. MockApiClient.addMockResponse({
  101. url: '/organizations/org-slug/recent-searches/',
  102. method: 'GET',
  103. body: [],
  104. });
  105. MockApiClient.addMockResponse({
  106. url: '/organizations/org-slug/searches/',
  107. body: [savedSearch],
  108. });
  109. issuesRequest = MockApiClient.addMockResponse({
  110. url: '/organizations/org-slug/issues/',
  111. body: [group],
  112. headers: {
  113. Link: DEFAULT_LINKS_HEADER,
  114. 'X-Hits': '1',
  115. },
  116. });
  117. MockApiClient.addMockResponse({
  118. url: '/organizations/org-slug/issues-stats/',
  119. body: [GroupStats()],
  120. });
  121. pollRequest = MockApiClient.addMockResponse({
  122. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  123. body: [],
  124. headers: {
  125. Link: DEFAULT_LINKS_HEADER,
  126. 'X-Hits': '1',
  127. },
  128. });
  129. jest.mocked(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('displays new group and pagination caption correctly', async function () {
  162. pollRequest = MockApiClient.addMockResponse({
  163. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  164. body: [group2],
  165. headers: {
  166. Link: DEFAULT_LINKS_HEADER,
  167. 'X-Hits': '2',
  168. },
  169. });
  170. await renderComponent();
  171. expect(screen.getByText(textWithMarkupMatcher('1-1 of 1'))).toBeInTheDocument();
  172. // Enable realtime updates
  173. await userEvent.click(
  174. screen.getByRole('button', {name: 'Enable real-time updates'}),
  175. {delay: null}
  176. );
  177. jest.advanceTimersByTime(3001);
  178. expect(pollRequest).toHaveBeenCalledTimes(1);
  179. // We mock out the stream group component and only render the ID as a testid
  180. await screen.findByTestId('2');
  181. expect(screen.getByText(textWithMarkupMatcher('1-2 of 2'))).toBeInTheDocument();
  182. });
  183. it('stops polling for new issues when endpoint returns a 401', async function () {
  184. pollRequest = MockApiClient.addMockResponse({
  185. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  186. body: [],
  187. statusCode: 401,
  188. });
  189. await renderComponent();
  190. // Enable real time control
  191. await userEvent.click(
  192. screen.getByRole('button', {name: 'Enable real-time updates'}),
  193. {delay: null}
  194. );
  195. // Each poll request gets delayed by additional 3s, up to max of 60s
  196. jest.advanceTimersByTime(3001);
  197. expect(pollRequest).toHaveBeenCalledTimes(1);
  198. jest.advanceTimersByTime(9001);
  199. expect(pollRequest).toHaveBeenCalledTimes(1);
  200. });
  201. it('stops polling for new issues when endpoint returns a 403', async function () {
  202. pollRequest = MockApiClient.addMockResponse({
  203. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  204. body: [],
  205. statusCode: 403,
  206. });
  207. await renderComponent();
  208. // Enable real time control
  209. await userEvent.click(
  210. screen.getByRole('button', {name: 'Enable real-time updates'}),
  211. {delay: null}
  212. );
  213. // Each poll request gets delayed by additional 3s, up to max of 60s
  214. jest.advanceTimersByTime(3001);
  215. expect(pollRequest).toHaveBeenCalledTimes(1);
  216. jest.advanceTimersByTime(9001);
  217. expect(pollRequest).toHaveBeenCalledTimes(1);
  218. });
  219. it('stops polling for new issues when endpoint returns a 404', async function () {
  220. pollRequest = MockApiClient.addMockResponse({
  221. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  222. body: [],
  223. statusCode: 404,
  224. });
  225. await renderComponent();
  226. // Enable real time control
  227. await userEvent.click(
  228. screen.getByRole('button', {name: 'Enable real-time updates'}),
  229. {delay: null}
  230. );
  231. // Each poll request gets delayed by additional 3s, up to max of 60s
  232. jest.advanceTimersByTime(3001);
  233. expect(pollRequest).toHaveBeenCalledTimes(1);
  234. jest.advanceTimersByTime(9001);
  235. expect(pollRequest).toHaveBeenCalledTimes(1);
  236. });
  237. });