diff --git a/apps/site/hooks/react-client/__tests__/useCopyToClipboard.test.jsx b/apps/site/hooks/react-client/__tests__/useCopyToClipboard.test.jsx index 5bb64c54c7592..89fae6fe49b2e 100644 --- a/apps/site/hooks/react-client/__tests__/useCopyToClipboard.test.jsx +++ b/apps/site/hooks/react-client/__tests__/useCopyToClipboard.test.jsx @@ -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'; @@ -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 ( @@ -21,6 +21,10 @@ await describe('useCopyToClipboard', async () => { ); }; + }); + + await it('should call clipboard API with `test` once', async t => { + t.mock.method(navigator.clipboard, 'writeText', () => Promise.resolve()); render(); @@ -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(); + 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(); + const button = screen.getByRole('button'); + fireEvent.click(button); + + assert.equal(navigator.clipboard.writeText.mock.callCount(), 0); + }); }); diff --git a/codecov.yml b/codecov.yml index 35cde5cd5e854..401d836ecb2ef 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,4 +1,6 @@ coverage: status: - project: off patch: off + project: + default: + target: 70%