11/* eslint-disable @typescript-eslint/no-explicit-any */
22import { render , screen , fireEvent , waitFor } from '@testing-library/react' ;
33import { describe , expect , it , vi , beforeEach } from 'vitest' ;
4+ import { toast } from 'sonner' ;
45import DashboardClient from './DashboardClient' ;
56
67vi . mock ( 'next/navigation' , ( ) => ( {
@@ -15,6 +16,14 @@ vi.mock('next/navigation', () => ({
1516 } ) ,
1617} ) ) ;
1718
19+ vi . mock ( 'sonner' , ( ) => ( {
20+ toast : {
21+ success : vi . fn ( ) ,
22+ error : vi . fn ( ) ,
23+ info : vi . fn ( ) ,
24+ } ,
25+ } ) ) ;
26+
1827// Mock framer-motion to avoid animation issues in tests
1928vi . mock ( 'framer-motion' , ( ) => ( {
2029 motion : {
@@ -205,6 +214,7 @@ const mockPeriod = {
205214describe ( 'DashboardClient' , ( ) => {
206215 beforeEach ( ( ) => {
207216 vi . restoreAllMocks ( ) ;
217+ vi . clearAllMocks ( ) ;
208218 } ) ;
209219
210220 it ( 'renders standard single profile view by default' , ( ) => {
@@ -327,6 +337,46 @@ describe('DashboardClient', () => {
327337 const generateLink = screen . getByRole ( 'link' , { name : / g e n e r a t e y o u r o w n / i } ) ;
328338 expect ( generateLink . getAttribute ( 'href' ) ) . toBe ( '/' ) ;
329339 } ) ;
340+
341+ it ( 'shows a success toast after the dashboard link is copied' , async ( ) => {
342+ const writeText = vi . fn ( ) . mockResolvedValue ( undefined ) ;
343+ Object . defineProperty ( navigator , 'clipboard' , {
344+ value : { writeText } ,
345+ configurable : true ,
346+ } ) ;
347+
348+ render (
349+ < DashboardClient initialData = { mockInitialData } username = "Shivangi1515" period = { mockPeriod } />
350+ ) ;
351+
352+ fireEvent . click ( screen . getByRole ( 'button' , { name : / ^ s h a r e $ / i } ) ) ;
353+
354+ await waitFor ( ( ) => {
355+ expect ( writeText ) . toHaveBeenCalledWith ( window . location . href ) ;
356+ expect ( toast . success ) . toHaveBeenCalledWith ( 'Link copied to clipboard!' ) ;
357+ } ) ;
358+ } ) ;
359+
360+ it ( 'shows an error toast when the dashboard link copy fails' , async ( ) => {
361+ const writeText = vi . fn ( ) . mockRejectedValue ( new Error ( 'Permission denied' ) ) ;
362+ Object . defineProperty ( navigator , 'clipboard' , {
363+ value : { writeText } ,
364+ configurable : true ,
365+ } ) ;
366+
367+ render (
368+ < DashboardClient initialData = { mockInitialData } username = "Shivangi1515" period = { mockPeriod } />
369+ ) ;
370+
371+ fireEvent . click ( screen . getByRole ( 'button' , { name : / ^ s h a r e $ / i } ) ) ;
372+
373+ await waitFor ( ( ) => {
374+ expect ( writeText ) . toHaveBeenCalledWith ( window . location . href ) ;
375+ expect ( toast . error ) . toHaveBeenCalledWith ( 'Failed to copy dashboard link' ) ;
376+ } ) ;
377+ expect ( toast . success ) . not . toHaveBeenCalledWith ( 'Link copied to clipboard!' ) ;
378+ } ) ;
379+
330380 // =========================================================================
331381 // ISSUE OBJECTIVE: Verify error is shown when comparing with same username
332382 // =========================================================================
0 commit comments