overview.polling.spec.tsx 8.6 KB

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