Skip to content

Commit

Permalink
Merge pull request #129 from JohnsonMao/fix/test-act-error-log
Browse files Browse the repository at this point in the history
✅ fixed untracked state changes in test components
  • Loading branch information
JohnsonMao authored Jan 13, 2024
2 parents fe5c449 + e3d1a97 commit 25ebd01
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tests/components/codeBox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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 }
);
});
});

0 comments on commit 25ebd01

Please sign in to comment.