test(clipboard): Enhance test coverage for copyToClipboard function - #8327
test(clipboard): Enhance test coverage for copyToClipboard function#8327nishupr wants to merge 1 commit into
Conversation
Closes JhaSourav07#8323 utils/clipboard.test.ts only covered fallbackCopyToClipboard. This adds utils/copyToClipboard.test.ts with 12 tests covering: - Resolves when navigator.clipboard.writeText succeeds - Does not invoke fallback when modern API succeeds - Falls back to DOM execCommand when writeText throws - Throws Error('Clipboard copy failed') when both APIs fail - Uses fallback when navigator.clipboard is null - Uses fallback when navigator.clipboard is undefined - Throws when clipboard unavailable and fallback returns false - Error message exactness assertion - Correct text passed to writeText - Correct text set on fallback textarea - Empty string handled by modern API - Empty string handled by fallback
|
Someone is attempting to deploy a commit to the jhasourav07's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
This pull request significantly improves the test coverage for the |
📦 Next.js Bundle Size Report (Gzipped Sizes)✨ No significant bundle size changes detected. 📊 Summary of Totals
|
Aamod007
left a comment
There was a problem hiding this comment.
Nice test coverage for the clipboard utility — focuses on the actual copyToClipboard function behavior. CI failing, please fix.
Labels: level:beginner (1 file), quality:clean, type:testing, mentor:Aamod007
Aamod007
left a comment
There was a problem hiding this comment.
Re-reviewing after noting the only CI failure is Vercel authorization (not code-related). All other checks (Format · Lint · Typecheck · Test, Production Build, CodeQL) pass. Approving — the Vercel deploy auth is a repo-level config issue, not a code problem.
Summary
Closes #8323
utils/clipboard.tsexports two functions bututils/clipboard.test.tsonly testedfallbackCopyToClipboard. The primarycopyToClipboardfunction - the async one used throughout the app - had zero test coverage. This PR adds a dedicated test file covering all branches ofcopyToClipboard.What was missing
navigator.clipboard.writeTextsucceeds)writeTextthrows → DOMexecCommand)navigator.clipboardisnullorundefined'Clipboard copy failed'File Added
utils/copyToClipboard.test.ts- 12 testsnavigator.clipboard.writeTextsucceedsexecCommandwhenwriteTextthrowsError('Clipboard copy failed')when both APIs failnavigator.clipboardisnullnavigator.clipboardisundefinedfalsewriteText<textarea>Approach
navigator.clipboardis controlled viaObject.definePropertyonnavigator, consistent with how the existing tests mockdocument.execCommand. Each test sets up the clipboard state it needs and tears down viavi.restoreAllMocks()inafterEach.Checklist
utils/clipboard.test.tsmain