From e3d1a976df22a6c915cd6a7ecd53baa5bb806316 Mon Sep 17 00:00:00 2001 From: Johnson Mao Date: Wed, 10 Jan 2024 21:56:56 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20fixed=20untracked=20state=20changes?= =?UTF-8?q?=20in=20test=20components?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/components/codeBox.test.tsx | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/components/codeBox.test.tsx b/tests/components/codeBox.test.tsx index 5b1e5da..a6b612a 100644 --- a/tests/components/codeBox.test.tsx +++ b/tests/components/codeBox.test.tsx @@ -4,9 +4,14 @@ import userEvent from '@testing-library/user-event'; import CodeBox from '@/components/CodeBox'; const mockCopy = jest.fn(); +let resolve: (value: unknown) => void; jest.mock('@/utils/clipboard', () => ({ - copyToClipboard: async (props: string) => mockCopy(props), + copyToClipboard: (props: string) => + new Promise((_resolve) => { + resolve = _resolve; + mockCopy(props); + }), })); describe('CodeBox component', () => { @@ -28,16 +33,18 @@ describe('CodeBox component', () => { const user = userEvent.setup(); expect(screen.queryByText('複製成功!')).toHaveClass('opacity-0'); - await act(() => user.click(copyButton)); - await act(() => user.click(copyButton)); + await act(async () => { + await user.click(copyButton); + resolve(undefined); + }); expect(screen.queryByText('複製成功!')).not.toHaveClass('opacity-0'); - expect(mockCopy).toHaveBeenCalledTimes(2); + expect(mockCopy).toHaveBeenCalledTimes(1); expect(mockCopy).toHaveBeenCalledWith(code); await waitFor( () => { expect(screen.queryByText('複製成功!')).toHaveClass('opacity-0'); }, - { timeout: 2000 } + { timeout: 1500 } ); }); });