diff options
author | Mathieu Suen <mathieu.suen@sonarsource.com> | 2022-02-21 11:50:32 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-07 20:02:55 +0000 |
commit | b90268c5cd663d677a4f0254c919e34cba67de5a (patch) | |
tree | 9cc743601a89f0eba9b3f1be6201a6de22483259 | |
parent | b355fc7b76a3e4949e25d7974c945d20545678fa (diff) | |
download | sonarqube-b90268c5cd663d677a4f0254c919e34cba67de5a.tar.gz sonarqube-b90268c5cd663d677a4f0254c919e34cba67de5a.zip |
[NO JIRA] Restore fake timer in UT
22 files changed, 156 insertions, 22 deletions
diff --git a/server/sonar-web/src/main/js/app/components/__tests__/AdminContainer-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/AdminContainer-test.tsx index c0ca09c8ee1..319896752c2 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/AdminContainer-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/AdminContainer-test.tsx @@ -22,6 +22,15 @@ import * as React from 'react'; import { mockAppState, mockLocation } from '../../../helpers/testMocks'; import { AdminContainer, AdminContainerProps } from '../AdminContainer'; +jest.mock('../../../api/plugins', () => ({ + getSettingsNavigation: jest.fn().mockResolvedValue({}), + getPendingPlugins: jest.fn().mockResolvedValue({}) +})); + +jest.mock('../../../api/system', () => ({ + getSystemStatus: jest.fn().mockResolvedValue({}) +})); + it('should render correctly', () => { const wrapper = shallowRender(); expect(wrapper).toMatchSnapshot(); diff --git a/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx index 30924992764..a0bbceabb68 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx @@ -88,9 +88,15 @@ jest.mock('../../utils/handleRequiredAuthorization', () => ({ const Inner = () => <div />; beforeEach(() => { + jest.useFakeTimers(); jest.clearAllMocks(); }); +afterEach(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); + it('changes component', () => { const wrapper = shallowRender(); wrapper.setState({ @@ -128,7 +134,7 @@ it('loads the project binding, if any', async () => { it("doesn't load branches portfolio", async () => { const wrapper = shallowRender({ location: mockLocation({ query: { id: 'portfolioKey' } }) }); - await new Promise(setImmediate); + await waitAndUpdate(wrapper); expect(getBranches).not.toBeCalled(); expect(getPullRequests).not.toBeCalled(); expect(getComponentData).toBeCalledWith({ component: 'portfolioKey', branch: undefined }); @@ -162,8 +168,8 @@ it('fetches status', async () => { component: {} }); - shallowRender(); - await new Promise(setImmediate); + const wrapper = shallowRender(); + await waitAndUpdate(wrapper); expect(getTasksForComponent).toBeCalledWith('portfolioKey'); }); @@ -200,7 +206,6 @@ it('filters correctly the pending tasks for a main branch', () => { }); it('reload component after task progress finished', async () => { - jest.useFakeTimers(); (getTasksForComponent as jest.Mock<any>) .mockResolvedValueOnce({ queue: [{ id: 'foo', status: TaskStatuses.InProgress, type: TaskTypes.ViewRefresh }] @@ -240,7 +245,6 @@ it('reload component after task progress finished', async () => { }); it('reloads component after task progress finished, and moves straight to current', async () => { - jest.useFakeTimers(); (getComponentData as jest.Mock<any>).mockResolvedValueOnce({ component: { key: 'bar' } }); @@ -290,7 +294,6 @@ it('only fully loads a non-empty component once', async () => { }); it('only fully reloads a non-empty component if there was previously some task in progress', async () => { - jest.useFakeTimers(); (getComponentData as jest.Mock<any>).mockResolvedValueOnce({ component: { key: 'bar', analysisDate: '2019-01-01' } }); diff --git a/server/sonar-web/src/main/js/app/components/__tests__/PageTracker-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/PageTracker-test.tsx index 5ea91b924db..5da29b31463 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/PageTracker-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/PageTracker-test.tsx @@ -34,8 +34,14 @@ jest.mock('../../../helpers/extensionsHandler', () => ({ })); jest.mock('../../../helpers/analytics', () => ({ gtm: jest.fn() })); +beforeAll(() => { + jest.useFakeTimers(); +}); -jest.useFakeTimers(); +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); beforeEach(() => { jest.clearAllTimers(); diff --git a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx index 8f57c123d6d..fb5c3defb5a 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx @@ -28,6 +28,11 @@ beforeEach(() => { jest.useFakeTimers(); }); +afterEach(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); + jest.mock('../../../../api/ce', () => ({ getIndexationStatus: jest.fn().mockResolvedValue({}) })); diff --git a/server/sonar-web/src/main/js/app/components/search/__tests__/SearchResult-test.tsx b/server/sonar-web/src/main/js/app/components/search/__tests__/SearchResult-test.tsx index edc5f8a200a..1a8509c28a5 100644 --- a/server/sonar-web/src/main/js/app/components/search/__tests__/SearchResult-test.tsx +++ b/server/sonar-web/src/main/js/app/components/search/__tests__/SearchResult-test.tsx @@ -21,7 +21,14 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import SearchResult from '../SearchResult'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); it('renders selected', () => { const wrapper = shallowRender(); diff --git a/server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts b/server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts index 12cc040e071..13c5f9f85b4 100644 --- a/server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts +++ b/server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts @@ -20,7 +20,14 @@ import getStore from '../getStore'; import throwGlobalError from '../throwGlobalError'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); it('should put the error message in the store', async () => { const response = new Response(); diff --git a/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssuesList-test.tsx b/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssuesList-test.tsx index fe5016bbaae..fa4aee4bce4 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssuesList-test.tsx +++ b/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssuesList-test.tsx @@ -23,9 +23,16 @@ import { mockIssue } from '../../../../helpers/testMocks'; import { waitAndUpdate } from '../../../../helpers/testUtils'; import IssuesList from '../IssuesList'; -it('should render correctly', async () => { +beforeAll(() => { jest.useFakeTimers(); +}); +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); + +it('should render correctly', async () => { const wrapper = shallowRender(); expect(wrapper).toMatchSnapshot(); jest.runAllTimers(); diff --git a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetGroupViewer-test.tsx b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetGroupViewer-test.tsx index 9e09842b368..cc815732c63 100644 --- a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetGroupViewer-test.tsx +++ b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/__tests__/ComponentSourceSnippetGroupViewer-test.tsx @@ -345,7 +345,14 @@ describe('getNodes', () => { }); describe('getHeight', () => { - jest.useFakeTimers(); + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); + }); const snippetGroup: SnippetGroup = { component: mockSourceViewerFile(), diff --git a/server/sonar-web/src/main/js/apps/maintenance/components/__tests__/App-test.tsx b/server/sonar-web/src/main/js/apps/maintenance/components/__tests__/App-test.tsx index e5ab3c2db29..abf658535db 100644 --- a/server/sonar-web/src/main/js/apps/maintenance/components/__tests__/App-test.tsx +++ b/server/sonar-web/src/main/js/apps/maintenance/components/__tests__/App-test.tsx @@ -28,7 +28,14 @@ jest.mock('../../../../api/system', () => ({ migrateDatabase: jest.fn() })); -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); const getMigrationStatus = require('../../../../api/system').getMigrationStatus as jest.Mock; const getSystemStatus = require('../../../../api/system').getSystemStatus as jest.Mock; diff --git a/server/sonar-web/src/main/js/apps/marketplace/__tests__/EditionBoxes-test.tsx b/server/sonar-web/src/main/js/apps/marketplace/__tests__/EditionBoxes-test.tsx index 119e45608fa..7bc48dbcaa8 100644 --- a/server/sonar-web/src/main/js/apps/marketplace/__tests__/EditionBoxes-test.tsx +++ b/server/sonar-web/src/main/js/apps/marketplace/__tests__/EditionBoxes-test.tsx @@ -22,6 +22,10 @@ import * as React from 'react'; import { EditionKey } from '../../../types/editions'; import EditionBoxes from '../EditionBoxes'; +jest.mock('../../../api/nav', () => ({ + getMarketplaceNavigation: jest.fn().mockResolvedValue({}) +})); + it('should display the available edition boxes correctly', () => { expect(getWrapper()).toMatchSnapshot(); }); diff --git a/server/sonar-web/src/main/js/apps/projectDump/__tests__/ProjectDumpApp-test.tsx b/server/sonar-web/src/main/js/apps/projectDump/__tests__/ProjectDumpApp-test.tsx index 1a3bda61f13..928cf3dd6ff 100644 --- a/server/sonar-web/src/main/js/apps/projectDump/__tests__/ProjectDumpApp-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectDump/__tests__/ProjectDumpApp-test.tsx @@ -36,9 +36,15 @@ jest.mock('../../../api/project-dump', () => ({ })); beforeEach(() => { + jest.useFakeTimers(); jest.clearAllMocks(); }); +afterEach(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); + it('should render correctly', async () => { (getActivity as jest.Mock) .mockResolvedValueOnce({ tasks: [mockDumpTask()] }) @@ -58,8 +64,6 @@ it('should render correctly', async () => { }); it('should poll for task status update', async () => { - jest.useFakeTimers(); - const wrapper = shallowRender(); await waitAndUpdate(wrapper); diff --git a/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx b/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx index 8c15a813335..f877ba5dfb4 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx @@ -36,6 +36,7 @@ beforeAll(() => { }); afterAll(() => { + jest.runOnlyPendingTimers(); jest.useRealTimers(); }); diff --git a/server/sonar-web/src/main/js/components/__tests__/lazyLoadComponent-test.tsx b/server/sonar-web/src/main/js/components/__tests__/lazyLoadComponent-test.tsx index d18211b0ef8..357fb135ddc 100644 --- a/server/sonar-web/src/main/js/components/__tests__/lazyLoadComponent-test.tsx +++ b/server/sonar-web/src/main/js/components/__tests__/lazyLoadComponent-test.tsx @@ -26,6 +26,12 @@ const factory = jest.fn().mockImplementation(() => import('../../components/cont beforeEach(() => { factory.mockClear(); + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); }); it('should lazy load and display the component', async () => { @@ -33,7 +39,9 @@ it('should lazy load and display the component', async () => { const wrapper = mount(<LazyComponent />); expect(wrapper).toMatchSnapshot(); expect(factory).toBeCalledTimes(1); + jest.runOnlyPendingTimers(); await waitAndUpdate(wrapper); + jest.runOnlyPendingTimers(); await waitAndUpdate(wrapper); expect(wrapper).toMatchSnapshot(); expect(factory).toBeCalledTimes(1); diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/EscKeydownHandler-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/EscKeydownHandler-test.tsx index cfb03de1aeb..dc773cb476b 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/EscKeydownHandler-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/EscKeydownHandler-test.tsx @@ -23,7 +23,14 @@ import { KeyboardCodes } from '../../../helpers/keycodes'; import { keydown } from '../../../helpers/testUtils'; import EscKeydownHandler from '../EscKeydownHandler'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); it('should render correctly', () => { expect(shallowRender()).toMatchSnapshot(); diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/OutsideClickHandler-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/OutsideClickHandler-test.tsx index b37bf017363..77ddcaee40e 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/OutsideClickHandler-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/OutsideClickHandler-test.tsx @@ -21,7 +21,14 @@ import { mount, shallow } from 'enzyme'; import * as React from 'react'; import OutsideClickHandler from '../OutsideClickHandler'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); it('should render correctly', () => { expect(shallowRender()).toMatchSnapshot(); diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/Tooltip-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/Tooltip-test.tsx index 9ca5bc08ebb..aa0450f088d 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/Tooltip-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/Tooltip-test.tsx @@ -21,7 +21,15 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import Tooltip, { TooltipInner, TooltipProps } from '../Tooltip'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); + jest.mock('react-dom', () => { const actual = jest.requireActual('react-dom'); return Object.assign({}, actual, { diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/clipboard-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/clipboard-test.tsx index bacedeb8b22..bf2c040aa21 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/clipboard-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/clipboard-test.tsx @@ -38,7 +38,14 @@ jest.mock( } ); -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); describe('ClipboardBase', () => { it('should display correctly', () => { diff --git a/server/sonar-web/src/main/js/components/docs/__tests__/DocMarkdownBlock-test.tsx b/server/sonar-web/src/main/js/components/docs/__tests__/DocMarkdownBlock-test.tsx index 83cf9862b83..176f99e936d 100644 --- a/server/sonar-web/src/main/js/components/docs/__tests__/DocMarkdownBlock-test.tsx +++ b/server/sonar-web/src/main/js/components/docs/__tests__/DocMarkdownBlock-test.tsx @@ -144,6 +144,8 @@ it('should correctly scroll to a specific heading if passed as a prop', () => { jest.runAllTimers(); expect(scrollToElement).toBeCalledWith(element, { bottomOffset: 720 }); + jest.runOnlyPendingTimers(); + jest.useRealTimers(); }); function shallowRender(props: Partial<DocMarkdownBlock['props']> = {}) { diff --git a/server/sonar-web/src/main/js/components/ui/__tests__/DeferredSpinner-test.tsx b/server/sonar-web/src/main/js/components/ui/__tests__/DeferredSpinner-test.tsx index 38f86f133b2..0b9f3f64f73 100644 --- a/server/sonar-web/src/main/js/components/ui/__tests__/DeferredSpinner-test.tsx +++ b/server/sonar-web/src/main/js/components/ui/__tests__/DeferredSpinner-test.tsx @@ -21,7 +21,14 @@ import { mount } from 'enzyme'; import * as React from 'react'; import DeferredSpinner from '../DeferredSpinner'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); it('renders spinner after timeout', () => { const spinner = mount(<DeferredSpinner />); diff --git a/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceRuleViewer-test.tsx b/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceRuleViewer-test.tsx index 40d76008fb0..b922f37d9e5 100644 --- a/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceRuleViewer-test.tsx +++ b/server/sonar-web/src/main/js/components/workspace/__tests__/WorkspaceRuleViewer-test.tsx @@ -21,7 +21,14 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import WorkspaceRuleViewer, { Props } from '../WorkspaceRuleViewer'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); it('should render', () => { expect(shallowRender()).toMatchSnapshot(); diff --git a/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts index be14c1bb3c6..69d20a41564 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts @@ -203,7 +203,14 @@ describe('post', () => { }); describe('requestTryAndRepeatUntil', () => { - jest.useFakeTimers(); + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); + }); beforeEach(() => { jest.clearAllTimers(); diff --git a/server/sonar-web/src/main/js/helpers/__tests__/scrolling-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/scrolling-test.ts index 01e76819f08..098d00ad5d9 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/scrolling-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/scrolling-test.ts @@ -19,7 +19,14 @@ */ import { scrollHorizontally, scrollToElement } from '../scrolling'; -jest.useFakeTimers(); +beforeAll(() => { + jest.useFakeTimers(); +}); + +afterAll(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); +}); describe('scrollToElement', () => { it('should scroll parent up to element', () => { |