Skip to content

test(clipboard): Enhance test coverage for copyToClipboard function - #8327

Open
nishupr wants to merge 1 commit into
JhaSourav07:mainfrom
nishupr:feat/copy-to-clipboard-tests
Open

test(clipboard): Enhance test coverage for copyToClipboard function#8327
nishupr wants to merge 1 commit into
JhaSourav07:mainfrom
nishupr:feat/copy-to-clipboard-tests

Conversation

@nishupr

@nishupr nishupr commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #8323

utils/clipboard.ts exports two functions but utils/clipboard.test.ts only tested fallbackCopyToClipboard. The primary copyToClipboard function - the async one used throughout the app - had zero test coverage. This PR adds a dedicated test file covering all branches of copyToClipboard.

What was missing

  • No test for the happy path (navigator.clipboard.writeText succeeds)
  • No test for the fallback path (writeText throws → DOM execCommand)
  • No test for double failure (modern API + fallback both fail → throws)
  • No test for when navigator.clipboard is null or undefined
  • No test verifying the exact error message 'Clipboard copy failed'
  • No test verifying the correct text is passed through each path
  • No test for empty string edge cases

File Added

utils/copyToClipboard.test.ts - 12 tests

# Test
1 Resolves when navigator.clipboard.writeText succeeds
2 Does not invoke fallback when modern API succeeds
3 Falls back to DOM execCommand when writeText throws
4 Throws Error('Clipboard copy failed') when both APIs fail
5 Uses fallback when navigator.clipboard is null
6 Uses fallback when navigator.clipboard is undefined
7 Throws when clipboard unavailable and fallback returns false
8 Error message exact match assertion
9 Correct text string passed to writeText
10 Correct text set on fallback <textarea>
11 Empty string handled correctly by modern API
12 Empty string handled correctly by fallback

Approach

navigator.clipboard is controlled via Object.defineProperty on navigator, consistent with how the existing tests mock document.execCommand. Each test sets up the clipboard state it needs and tears down via vi.restoreAllMocks() in afterEach.

Checklist

  • All 12 tests pass locally
  • No changes to production code
  • Follows patterns in utils/clipboard.test.ts
  • Branch created off main

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
@vercel

vercel Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

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.

@retenta-bot

retenta-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

This pull request significantly improves the test coverage for the copyToClipboard function, addressing previously untested scenarios. It aligns well with our past decisions to implement a unified clipboard utility with fallback support, ensuring robustness in clipboard operations across different environments. Great job on this! Looking forward to seeing it merged!

@retenta-bot retenta-bot Bot changed the title test(clipboard): add full test coverage for copyToClipboard test(clipboard): Enhance test coverage for copyToClipboard function Jul 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Next.js Bundle Size Report (Gzipped Sizes)

✨ No significant bundle size changes detected.

📊 Summary of Totals

Category PR Size Base Size Difference
Total JS 4052.50 KB 4052.50 KB 0 B
Total CSS 338.94 KB 338.94 KB 0 B

@Aamod007 Aamod007 added level:beginner Small changes Usually isolated fixes or simple UI/text updates. quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. mentor:Aamod007 type:testing Adding, updating, or fixing tests labels Jul 27, 2026

@Aamod007 Aamod007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Aamod007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:beginner Small changes Usually isolated fixes or simple UI/text updates. mentor:Aamod007 quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. type:testing Adding, updating, or fixing tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add missing test coverage for copyToClipboard in utils/clipboard.ts

2 participants