overview.polling.spec.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import {GroupFixture} from 'sentry-fixture/group';
  2. import {GroupStatsFixture} from 'sentry-fixture/groupStats';
  3. import {LocationFixture} from 'sentry-fixture/locationFixture';
  4. import {MemberFixture} from 'sentry-fixture/member';
  5. import {SearchFixture} from 'sentry-fixture/search';
  6. import {TagsFixture} from 'sentry-fixture/tags';
  7. import {initializeOrg} from 'sentry-test/initializeOrg';
  8. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  9. import {textWithMarkupMatcher} from 'sentry-test/utils';
  10. import StreamGroup from 'sentry/components/stream/group';
  11. import TagStore from 'sentry/stores/tagStore';
  12. import IssueList from 'sentry/views/issueList/overview';
  13. jest.mock('sentry/views/issueList/filters', () => jest.fn(() => null));
  14. jest.mock('sentry/components/stream/group', () =>
  15. jest.fn(({id}) => <div data-test-id={id} />)
  16. );
  17. jest.mock('js-cookie', () => ({
  18. get: jest.fn(),
  19. set: jest.fn(),
  20. }));
  21. const PREVIOUS_PAGE_CURSOR = '1443575731';
  22. const DEFAULT_LINKS_HEADER =
  23. `<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", ` +
  24. '<http://127.0.0.1:8000/api/0/organizations/org-slug/issues/?cursor=1443575000:0:0>; rel="next"; results="true"; cursor="1443575000:0:0"';
  25. describe('IssueList -> Polling', function () {
  26. let issuesRequest: jest.Mock;
  27. let pollRequest: jest.Mock;
  28. afterEach(() => {
  29. jest.useRealTimers();
  30. MockApiClient.clearMockResponses();
  31. });
  32. const {organization, project, routerProps, routerContext} = initializeOrg({
  33. organization: {
  34. access: ['project:releases'],
  35. },
  36. });
  37. const savedSearch = SearchFixture({
  38. id: '789',
  39. query: 'is:unresolved',
  40. name: 'Unresolved Issues',
  41. });
  42. const group = GroupFixture({project});
  43. const group2 = GroupFixture({project, id: '2'});
  44. const defaultProps = {
  45. location: LocationFixture({
  46. query: {query: 'is:unresolved'},
  47. search: 'query=is:unresolved',
  48. }),
  49. params: {},
  50. organization,
  51. };
  52. /* helpers */
  53. const renderComponent = async () => {
  54. render(<IssueList {...routerProps} {...defaultProps} />, {
  55. context: routerContext,
  56. });
  57. await Promise.resolve();
  58. jest.runAllTimers();
  59. };
  60. beforeEach(function () {
  61. jest.useFakeTimers();
  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: TagsFixture(),
  96. });
  97. MockApiClient.addMockResponse({
  98. url: '/organizations/org-slug/users/',
  99. method: 'GET',
  100. body: [MemberFixture({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. 'X-Hits': '1',
  117. },
  118. });
  119. MockApiClient.addMockResponse({
  120. url: '/organizations/org-slug/issues-stats/',
  121. body: [GroupStatsFixture()],
  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. 'X-Hits': '1',
  129. },
  130. });
  131. jest.mocked(StreamGroup).mockClear();
  132. TagStore.init();
  133. });
  134. it('toggles polling for new issues', async function () {
  135. await renderComponent();
  136. await waitFor(() => {
  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. });
  145. // Enable realtime updates
  146. await userEvent.click(
  147. screen.getByRole('button', {name: 'Enable real-time updates'}),
  148. {delay: null}
  149. );
  150. // Each poll request gets delayed by additional 3s, up to max of 60s
  151. jest.advanceTimersByTime(3001);
  152. expect(pollRequest).toHaveBeenCalledTimes(1);
  153. jest.advanceTimersByTime(6001);
  154. expect(pollRequest).toHaveBeenCalledTimes(2);
  155. // Pauses
  156. await userEvent.click(screen.getByRole('button', {name: 'Pause real-time updates'}), {
  157. delay: null,
  158. });
  159. jest.advanceTimersByTime(12001);
  160. expect(pollRequest).toHaveBeenCalledTimes(2);
  161. });
  162. it('displays new group and pagination caption correctly', async function () {
  163. pollRequest = MockApiClient.addMockResponse({
  164. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  165. body: [group2],
  166. headers: {
  167. Link: DEFAULT_LINKS_HEADER,
  168. 'X-Hits': '2',
  169. },
  170. });
  171. await renderComponent();
  172. expect(
  173. await screen.findByText(textWithMarkupMatcher('1-1 of 1'))
  174. ).toBeInTheDocument();
  175. // Enable realtime updates
  176. await userEvent.click(
  177. screen.getByRole('button', {name: 'Enable real-time updates'}),
  178. {delay: null}
  179. );
  180. jest.advanceTimersByTime(3001);
  181. expect(pollRequest).toHaveBeenCalledTimes(1);
  182. // We mock out the stream group component and only render the ID as a testid
  183. await screen.findByTestId('2');
  184. expect(screen.getByText(textWithMarkupMatcher('1-2 of 2'))).toBeInTheDocument();
  185. });
  186. it('stops polling for new issues when endpoint returns a 401', async function () {
  187. pollRequest = MockApiClient.addMockResponse({
  188. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  189. body: [],
  190. statusCode: 401,
  191. });
  192. await renderComponent();
  193. // Enable real time control
  194. await userEvent.click(
  195. await screen.findByRole('button', {name: 'Enable real-time updates'}),
  196. {delay: null}
  197. );
  198. // Each poll request gets delayed by additional 3s, up to max of 60s
  199. jest.advanceTimersByTime(3001);
  200. expect(pollRequest).toHaveBeenCalledTimes(1);
  201. jest.advanceTimersByTime(9001);
  202. expect(pollRequest).toHaveBeenCalledTimes(1);
  203. });
  204. it('stops polling for new issues when endpoint returns a 403', async function () {
  205. pollRequest = MockApiClient.addMockResponse({
  206. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  207. body: [],
  208. statusCode: 403,
  209. });
  210. await renderComponent();
  211. // Enable real time control
  212. await userEvent.click(
  213. await screen.findByRole('button', {name: 'Enable real-time updates'}),
  214. {delay: null}
  215. );
  216. // Each poll request gets delayed by additional 3s, up to max of 60s
  217. jest.advanceTimersByTime(3001);
  218. expect(pollRequest).toHaveBeenCalledTimes(1);
  219. jest.advanceTimersByTime(9001);
  220. expect(pollRequest).toHaveBeenCalledTimes(1);
  221. });
  222. it('stops polling for new issues when endpoint returns a 404', async function () {
  223. pollRequest = MockApiClient.addMockResponse({
  224. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  225. body: [],
  226. statusCode: 404,
  227. });
  228. await renderComponent();
  229. // Enable real time control
  230. await userEvent.click(
  231. await screen.findByRole('button', {name: 'Enable real-time updates'}),
  232. {delay: null}
  233. );
  234. // Each poll request gets delayed by additional 3s, up to max of 60s
  235. jest.advanceTimersByTime(3001);
  236. expect(pollRequest).toHaveBeenCalledTimes(1);
  237. jest.advanceTimersByTime(9001);
  238. expect(pollRequest).toHaveBeenCalledTimes(1);
  239. });
  240. });