From 83022a4cff111a764bea4b74a939b1659393ed5b Mon Sep 17 00:00:00 2001 From: shaodahong Date: Thu, 8 Aug 2019 20:32:06 +0800 Subject: [PATCH] test: try mock Date.now to reduce test time --- components/_util/__tests__/scrollTo.test.js | 31 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/components/_util/__tests__/scrollTo.test.js b/components/_util/__tests__/scrollTo.test.js index fafe11d0b1..3e41a60ee9 100644 --- a/components/_util/__tests__/scrollTo.test.js +++ b/components/_util/__tests__/scrollTo.test.js @@ -1,15 +1,38 @@ import scrollTo from '../scrollTo'; -import { sleep } from '../../../tests/utils'; describe('Test ScrollTo function', () => { + let dateNowMock; + + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.useRealTimers(); + }); + + beforeEach(() => { + dateNowMock = jest + .spyOn(Date, 'now') + .mockImplementationOnce(() => 0) + .mockImplementationOnce(() => 1000); + }); + + afterEach(() => { + dateNowMock.mockRestore(); + }); + it('test scrollTo', async () => { const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => { window.scrollY = y; window.pageYOffset = y; }); + scrollTo(1000); - await sleep(500); + + jest.runAllTimers(); expect(window.pageYOffset).toBe(1000); + scrollToSpy.mockRestore(); }); @@ -18,7 +41,7 @@ describe('Test ScrollTo function', () => { scrollTo(1000, { callback: cbMock, }); - await sleep(500); + jest.runAllTimers(); expect(cbMock).toHaveBeenCalledTimes(1); }); @@ -27,7 +50,7 @@ describe('Test ScrollTo function', () => { scrollTo(1000, { getContainer: () => div, }); - await sleep(500); + jest.runAllTimers(); expect(div.scrollTop).toBe(1000); }); });