Skip to content

Commit e851523

Browse files
author
kali
committed
fix(frontend): wait for clipboard write before showing copied state
1 parent f0f56a8 commit e851523

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

app/page.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,26 @@ describe('LandingPage', () => {
274274
});
275275
});
276276

277+
it('does not show copied state when clipboard write fails', async () => {
278+
vi.mocked(navigator.clipboard.writeText).mockRejectedValueOnce(new Error('Permission denied'));
279+
280+
render(<LandingPage />);
281+
const input = screen.getByPlaceholderText('Enter GitHub Username') as HTMLInputElement;
282+
fireEvent.change(input, { target: { value: 'jhasourav07' } });
283+
284+
const copyButton = screen.getByText('Copy Link').closest('button');
285+
fireEvent.click(copyButton!);
286+
287+
await waitFor(() => {
288+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
289+
expect.stringContaining('/api/streak?user=jhasourav07')
290+
);
291+
});
292+
293+
expect(screen.queryByText('Copied')).toBeNull();
294+
expect(screen.queryByText('Your Monolith is Ready - Deploy It in 4 Steps')).toBeNull();
295+
});
296+
277297
it('disables Copy Link button when username is empty', () => {
278298
render(<LandingPage />);
279299

app/page.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,18 @@ export default function LandingPage() {
104104
badgeResult?.username === debouncedUsername && badgeResult?.status === 'loaded';
105105
const badgeError = badgeResult?.username === debouncedUsername && badgeResult?.status === 'error';
106106

107-
const copyToClipboard = () => {
107+
const copyToClipboard = async () => {
108108
if (trimmedUsername.length === 0) return;
109109

110+
try {
111+
await navigator.clipboard.writeText(markdown);
112+
} catch {
113+
setCopied(false);
114+
return;
115+
}
116+
110117
trackUser(trimmedUsername);
111118
addSearch(trimmedUsername);
112-
113-
navigator.clipboard.writeText(markdown);
114119
setCopied(true);
115120
setTimeout(() => {
116121
guideRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });

0 commit comments

Comments
 (0)