Skip to content

Commit 533a85f

Browse files
fix(frontend): surface compare share failures (JhaSourav07#3062)
Co-authored-by: Eshaan Agrawal <211106034+eshaanag@users.noreply.github.com>
2 parents b303d14 + 5ddc6c6 commit 533a85f

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

components/dashboard/DashboardClient.test.tsx

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,30 @@ describe('DashboardClient', () => {
217217
vi.clearAllMocks();
218218
});
219219

220+
const renderInCompareMode = async () => {
221+
const mockFetch = vi.fn().mockResolvedValue({
222+
ok: true,
223+
json: async () => mockSecondData,
224+
});
225+
vi.stubGlobal('fetch', mockFetch);
226+
227+
render(
228+
<DashboardClient initialData={mockInitialData} username="Shivangi1515" period={mockPeriod} />
229+
);
230+
231+
fireEvent.click(screen.getByText('Compare Profile'));
232+
fireEvent.change(screen.getByPlaceholderText('Enter GitHub Username'), {
233+
target: { value: 'JhaSourav07' },
234+
});
235+
fireEvent.click(screen.getByText('Compare'));
236+
237+
await waitFor(() => {
238+
expect(screen.getByText('Share Comparison')).toBeDefined();
239+
});
240+
241+
vi.clearAllMocks();
242+
};
243+
220244
it('renders standard single profile view by default', () => {
221245
render(
222246
<DashboardClient initialData={mockInitialData} username="Shivangi1515" period={mockPeriod} />
@@ -377,6 +401,76 @@ describe('DashboardClient', () => {
377401
expect(toast.success).not.toHaveBeenCalledWith('Link copied to clipboard!');
378402
});
379403

404+
it('copies the comparison link when native sharing is unavailable', async () => {
405+
const writeText = vi.fn().mockResolvedValue(undefined);
406+
Object.defineProperty(navigator, 'share', {
407+
value: undefined,
408+
configurable: true,
409+
});
410+
Object.defineProperty(navigator, 'clipboard', {
411+
value: { writeText },
412+
configurable: true,
413+
});
414+
415+
await renderInCompareMode();
416+
417+
fireEvent.click(screen.getByText('Share Comparison'));
418+
419+
await waitFor(() => {
420+
expect(writeText).toHaveBeenCalledWith(
421+
`${window.location.origin}/dashboard/Shivangi1515?compare=JhaSourav07`
422+
);
423+
expect(toast.success).toHaveBeenCalledWith('Comparison link copied!');
424+
});
425+
});
426+
427+
it('shows an error toast when comparison link copy fails', async () => {
428+
const writeText = vi.fn().mockRejectedValue(new Error('Permission denied'));
429+
Object.defineProperty(navigator, 'share', {
430+
value: undefined,
431+
configurable: true,
432+
});
433+
Object.defineProperty(navigator, 'clipboard', {
434+
value: { writeText },
435+
configurable: true,
436+
});
437+
438+
await renderInCompareMode();
439+
440+
fireEvent.click(screen.getByText('Share Comparison'));
441+
442+
await waitFor(() => {
443+
expect(writeText).toHaveBeenCalledWith(
444+
`${window.location.origin}/dashboard/Shivangi1515?compare=JhaSourav07`
445+
);
446+
expect(toast.error).toHaveBeenCalledWith('Failed to share comparison link');
447+
});
448+
expect(toast.success).not.toHaveBeenCalledWith('Comparison link copied!');
449+
});
450+
451+
it('does not show an error toast when native comparison sharing is cancelled', async () => {
452+
const share = vi
453+
.fn()
454+
.mockRejectedValue(Object.assign(new Error('AbortError'), { name: 'AbortError' }));
455+
Object.defineProperty(navigator, 'share', {
456+
value: share,
457+
configurable: true,
458+
});
459+
460+
await renderInCompareMode();
461+
462+
fireEvent.click(screen.getByText('Share Comparison'));
463+
464+
await waitFor(() => {
465+
expect(share).toHaveBeenCalledWith({
466+
title: 'Shivangi1515 vs JhaSourav07',
467+
text: 'Check out this GitHub profile comparison',
468+
url: `${window.location.origin}/dashboard/Shivangi1515?compare=JhaSourav07`,
469+
});
470+
});
471+
expect(toast.error).not.toHaveBeenCalledWith('Failed to share comparison link');
472+
});
473+
380474
// =========================================================================
381475
// ISSUE OBJECTIVE: Verify error is shown when comparing with same username
382476
// =========================================================================

components/dashboard/DashboardClient.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,12 @@ export default function DashboardClient({
472472
await navigator.clipboard.writeText(compareUrl);
473473
toast.success('Comparison link copied!');
474474
}
475-
} catch {
476-
// user cancelled share dialog
475+
} catch (error) {
476+
if (error instanceof Error && error.name === 'AbortError') {
477+
return;
478+
}
479+
480+
toast.error('Failed to share comparison link');
477481
}
478482
};
479483

0 commit comments

Comments
 (0)