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, router} = 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. router,
  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. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  65. MockApiClient.clearMockResponses();
  66. MockApiClient.addMockResponse({
  67. url: '/organizations/org-slug/searches/',
  68. body: [savedSearch],
  69. });
  70. MockApiClient.addMockResponse({
  71. url: '/organizations/org-slug/recent-searches/',
  72. body: [],
  73. });
  74. MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/issues-count/',
  76. method: 'GET',
  77. body: [{}],
  78. });
  79. MockApiClient.addMockResponse({
  80. url: '/organizations/org-slug/processingissues/',
  81. method: 'GET',
  82. body: [
  83. {
  84. project: 'test-project',
  85. numIssues: 1,
  86. hasIssues: true,
  87. lastSeen: '2019-01-16T15:39:11.081Z',
  88. },
  89. ],
  90. });
  91. MockApiClient.addMockResponse({
  92. url: '/organizations/org-slug/tags/',
  93. method: 'GET',
  94. body: TagsFixture(),
  95. });
  96. MockApiClient.addMockResponse({
  97. url: '/organizations/org-slug/users/',
  98. method: 'GET',
  99. body: [MemberFixture({projects: [project.slug]})],
  100. });
  101. MockApiClient.addMockResponse({
  102. url: '/organizations/org-slug/recent-searches/',
  103. method: 'GET',
  104. body: [],
  105. });
  106. MockApiClient.addMockResponse({
  107. url: '/organizations/org-slug/searches/',
  108. body: [savedSearch],
  109. });
  110. issuesRequest = MockApiClient.addMockResponse({
  111. url: '/organizations/org-slug/issues/',
  112. body: [group],
  113. headers: {
  114. Link: DEFAULT_LINKS_HEADER,
  115. 'X-Hits': '1',
  116. },
  117. });
  118. MockApiClient.addMockResponse({
  119. url: '/organizations/org-slug/issues-stats/',
  120. body: [GroupStatsFixture()],
  121. });
  122. pollRequest = MockApiClient.addMockResponse({
  123. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  124. body: [],
  125. headers: {
  126. Link: DEFAULT_LINKS_HEADER,
  127. 'X-Hits': '1',
  128. },
  129. });
  130. jest.mocked(StreamGroup).mockClear();
  131. TagStore.init();
  132. });
  133. it('toggles polling for new issues', async function () {
  134. await renderComponent();
  135. await waitFor(() => {
  136. expect(issuesRequest).toHaveBeenCalledWith(
  137. expect.anything(),
  138. expect.objectContaining({
  139. // Should be called with default query
  140. data: expect.stringContaining('is%3Aunresolved'),
  141. })
  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(
  172. await screen.findByText(textWithMarkupMatcher('1-1 of 1'))
  173. ).toBeInTheDocument();
  174. // Enable realtime updates
  175. await userEvent.click(
  176. screen.getByRole('button', {name: 'Enable real-time updates'}),
  177. {delay: null}
  178. );
  179. jest.advanceTimersByTime(3001);
  180. expect(pollRequest).toHaveBeenCalledTimes(1);
  181. // We mock out the stream group component and only render the ID as a testid
  182. await screen.findByTestId('2');
  183. expect(screen.getByText(textWithMarkupMatcher('1-2 of 2'))).toBeInTheDocument();
  184. });
  185. it('stops polling for new issues when endpoint returns a 401', async function () {
  186. pollRequest = MockApiClient.addMockResponse({
  187. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  188. body: [],
  189. statusCode: 401,
  190. });
  191. await renderComponent();
  192. // Enable real time control
  193. await userEvent.click(
  194. await screen.findByRole('button', {name: 'Enable real-time updates'}),
  195. {delay: null}
  196. );
  197. // Each poll request gets delayed by additional 3s, up to max of 60s
  198. jest.advanceTimersByTime(3001);
  199. expect(pollRequest).toHaveBeenCalledTimes(1);
  200. jest.advanceTimersByTime(9001);
  201. expect(pollRequest).toHaveBeenCalledTimes(1);
  202. });
  203. it('stops polling for new issues when endpoint returns a 403', async function () {
  204. pollRequest = MockApiClient.addMockResponse({
  205. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  206. body: [],
  207. statusCode: 403,
  208. });
  209. await renderComponent();
  210. // Enable real time control
  211. await userEvent.click(
  212. await screen.findByRole('button', {name: 'Enable real-time updates'}),
  213. {delay: null}
  214. );
  215. // Each poll request gets delayed by additional 3s, up to max of 60s
  216. jest.advanceTimersByTime(3001);
  217. expect(pollRequest).toHaveBeenCalledTimes(1);
  218. jest.advanceTimersByTime(9001);
  219. expect(pollRequest).toHaveBeenCalledTimes(1);
  220. });
  221. it('stops polling for new issues when endpoint returns a 404', async function () {
  222. pollRequest = MockApiClient.addMockResponse({
  223. url: `/api/0/organizations/org-slug/issues/?cursor=${PREVIOUS_PAGE_CURSOR}:0:1`,
  224. body: [],
  225. statusCode: 404,
  226. });
  227. await renderComponent();
  228. // Enable real time control
  229. await userEvent.click(
  230. await screen.findByRole('button', {name: 'Enable real-time updates'}),
  231. {delay: null}
  232. );
  233. // Each poll request gets delayed by additional 3s, up to max of 60s
  234. jest.advanceTimersByTime(3001);
  235. expect(pollRequest).toHaveBeenCalledTimes(1);
  236. jest.advanceTimersByTime(9001);
  237. expect(pollRequest).toHaveBeenCalledTimes(1);
  238. });
  239. });