From 96ccdfedf108f792813d63b19b5d0f26ed1e41b3 Mon Sep 17 00:00:00 2001 From: yykoypj <601924094@qq.com> Date: Fri, 17 Jun 2022 15:07:32 +0800 Subject: [PATCH] test: change to fakeTimer when error happens sometimes (#36102) --- components/typography/__tests__/copy.test.tsx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/components/typography/__tests__/copy.test.tsx b/components/typography/__tests__/copy.test.tsx index 529e721760..ef5afb9d43 100644 --- a/components/typography/__tests__/copy.test.tsx +++ b/components/typography/__tests__/copy.test.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { LikeOutlined, SmileOutlined } from '@ant-design/icons'; import * as copyObj from 'copy-to-clipboard'; +import { act } from '@testing-library/react'; import { fireEvent, render, waitFor } from '../../../tests/utils'; import Base from '../Base'; @@ -233,7 +234,8 @@ describe('Typography copy', () => { fireEvent.click(wrapper.querySelectorAll('.ant-typography-copy')[0]); }); - it('copy to clipboard', done => { + it('copy to clipboard', () => { + jest.useFakeTimers(); const spy = jest.spyOn(copyObj, 'default'); const originText = 'origin text.'; const nextText = 'next text.'; @@ -243,7 +245,7 @@ describe('Typography copy', () => { setTimeout(() => { setDynamicText(nextText); }, 500); - }); + }, []); return ( {dynamicText} @@ -254,12 +256,13 @@ describe('Typography copy', () => { const copyBtn = wrapper.querySelectorAll('.ant-typography-copy')[0]; fireEvent.click(copyBtn); expect(spy.mock.calls[0][0]).toEqual(originText); - setTimeout(() => { - spy.mockReset(); - fireEvent.click(copyBtn); - expect(spy.mock.calls[0][0]).toEqual(nextText); - done(); - }, 500); + act(() => { + jest.runAllTimers(); + }); + spy.mockReset(); + fireEvent.click(copyBtn); + expect(spy.mock.calls[0][0]).toEqual(nextText); + jest.useRealTimers(); }); }); });