From 3b692bc8b6eec47d7dcb6663f8babea126db5e5b Mon Sep 17 00:00:00 2001 From: shaodahong Date: Tue, 6 Aug 2019 21:45:46 +0800 Subject: [PATCH] test: fix test fail --- components/_util/__tests__/scrollTo.test.js | 15 ++++----------- components/back-top/__tests__/index.test.js | 17 +++-------------- 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/components/_util/__tests__/scrollTo.test.js b/components/_util/__tests__/scrollTo.test.js index 9a9dff3320..fafe11d0b1 100644 --- a/components/_util/__tests__/scrollTo.test.js +++ b/components/_util/__tests__/scrollTo.test.js @@ -1,21 +1,14 @@ 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) => { window.scrollY = y; window.pageYOffset = y; }); scrollTo(1000); - jest.runAllTimers(); + await sleep(500); expect(window.pageYOffset).toBe(1000); scrollToSpy.mockRestore(); }); @@ -25,7 +18,7 @@ describe('Test ScrollTo function', () => { scrollTo(1000, { callback: cbMock, }); - jest.runAllTimers(); + await sleep(500); expect(cbMock).toHaveBeenCalledTimes(1); }); @@ -34,7 +27,7 @@ describe('Test ScrollTo function', () => { scrollTo(1000, { getContainer: () => div, }); - jest.runAllTimers(); + await sleep(500); expect(div.scrollTop).toBe(1000); }); }); diff --git a/components/back-top/__tests__/index.test.js b/components/back-top/__tests__/index.test.js index 4f2eca6d8c..de6e0ce55b 100644 --- a/components/back-top/__tests__/index.test.js +++ b/components/back-top/__tests__/index.test.js @@ -4,28 +4,17 @@ 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) => { - const w = window; - w.scrollY = y; - w.pageYOffset = y; + window.scrollY = y; + window.pageYOffset = y; }); 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(); + await sleep(500); expect(window.pageYOffset).toBe(0); scrollToSpy.mockRestore(); });