Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from 'node:test';
import { describe, it, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import { setTimeout } from 'node:timers/promises';

Expand All @@ -9,10 +9,10 @@ import useCopyToClipboard from '#site/hooks/react-client/useCopyToClipboard';
navigator.clipboard = { writeText: () => {} };

await describe('useCopyToClipboard', async () => {
await it('should call clipboard API with `test` once', async t => {
t.mock.method(navigator.clipboard, 'writeText', () => Promise.resolve());
let TestComponent;

const TestComponent = ({ textToCopy }) => {
beforeEach(() => {
TestComponent = ({ textToCopy }) => {
const [copied, copyText] = useCopyToClipboard();

return (
Expand All @@ -21,6 +21,10 @@ await describe('useCopyToClipboard', async () => {
</button>
);
};
});

await it('should call clipboard API with `test` once', async t => {
t.mock.method(navigator.clipboard, 'writeText', () => Promise.resolve());

render(<TestComponent textToCopy="test" />);

Expand All @@ -40,4 +44,29 @@ await describe('useCopyToClipboard', async () => {
'test',
]);
});

await it('should handle clipboard write text failure', async t => {
t.mock.method(navigator.clipboard, 'writeText', () => Promise.reject(new Error("fail")));

render(<TestComponent textToCopy="fail" />);
const button = screen.getByRole('button');

fireEvent.click(button);
assert.ok((await screen.findByText(/copy/i)));

assert.equal(navigator.clipboard.writeText.mock.callCount(), 1);
assert.deepEqual(navigator.clipboard.writeText.mock.calls[0].arguments, [
'fail',
]);
});

await it('should not call clipboard API when text is undefined', async t => {
t.mock.method(navigator.clipboard, 'writeText', () => Promise.resolve());

render(<TestComponent textToCopy={undefined} />);
const button = screen.getByRole('button');
fireEvent.click(button);

assert.equal(navigator.clipboard.writeText.mock.callCount(), 0);
});
});
4 changes: 3 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
coverage:
status:
project: off
patch: off
project:
default:
target: 70%