diff --git a/components/_util/__tests__/scrollTo.test.js b/components/_util/__tests__/scrollTo.test.js index 4722196cd9..9a9dff3320 100644 --- a/components/_util/__tests__/scrollTo.test.js +++ b/components/_util/__tests__/scrollTo.test.js @@ -1,15 +1,21 @@ import scrollTo from '../scrollTo'; -import { sleep } from '../../../tests/utils'; describe('Test ScrollTo function', () => { + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.useRealTimers(); + }); + it('test scrollTo', async () => { const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => { - const w = window; - w.scrollY = y; - w.pageYOffset = y; + window.scrollY = y; + window.pageYOffset = y; }); scrollTo(1000); - await sleep(1000); + jest.runAllTimers(); expect(window.pageYOffset).toBe(1000); scrollToSpy.mockRestore(); }); @@ -19,7 +25,7 @@ describe('Test ScrollTo function', () => { scrollTo(1000, { callback: cbMock, }); - await sleep(1000); + jest.runAllTimers(); expect(cbMock).toHaveBeenCalledTimes(1); }); @@ -28,7 +34,7 @@ describe('Test ScrollTo function', () => { scrollTo(1000, { getContainer: () => div, }); - await sleep(1000); + jest.runAllTimers(); expect(div.scrollTop).toBe(1000); }); }); diff --git a/components/back-top/__tests__/index.test.js b/components/back-top/__tests__/index.test.js index c79229bf5f..acabb8c40b 100644 --- a/components/back-top/__tests__/index.test.js +++ b/components/back-top/__tests__/index.test.js @@ -1,9 +1,15 @@ import React from 'react'; import { mount } from 'enzyme'; -import { sleep } from '../../../tests/utils'; import BackTop from '..'; describe('BackTop', () => { + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.useRealTimers(); + }); it('should scroll to top after click it', async () => { const wrapper = mount(); const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => { @@ -14,9 +20,9 @@ describe('BackTop', () => { window.scrollTo(0, 400); // trigger scroll manually wrapper.instance().handleScroll(); - await sleep(); + jest.runAllTimers(); wrapper.find('.ant-back-top').simulate('click'); - await sleep(1000); + jest.runAllTimers(); expect(window.pageYOffset).toBe(0); scrollToSpy.mockRestore(); });