@@ -18,6 +18,13 @@ vi.mock("@components/UseAuth", () => ({
1818 default : vi . fn ( ) ,
1919} ) ) ;
2020
21+ vi . mock ( "next/image" , ( ) => ( {
22+ default : ( props : any ) => {
23+ // eslint-disable-next-line jsx-a11y/alt-text, @next/next/no-img-element
24+ return < img { ...props } /> ;
25+ } ,
26+ } ) ) ;
27+
2128vi . mock (
2229 "@services/auth.service" ,
2330 async (
@@ -30,6 +37,14 @@ vi.mock(
3037 default : {
3138 ...actual . default ,
3239 logout : vi . fn ( ) ,
40+ getUser : vi . fn ( ) . mockImplementation ( ( ) => {
41+ return {
42+ id : 1 ,
43+ username : "test" ,
44+ refreshToken : "test" ,
45+ profileImage : "" ,
46+ } ;
47+ } ) ,
3348 AuthStatus : {
3449 Unauthorized : "Unauthorized" ,
3550 Authorized : "Authorized" ,
@@ -152,28 +167,44 @@ describe("GlobalNavbar", () => {
152167 expect ( mockPush ) . toHaveBeenCalledWith ( "/" ) ;
153168 } ) ;
154169
155- it ( "renders default avatar when authorized and no profileImage" , ( ) => {
156- ( useAuth as MockedFunction < typeof useAuth > ) . mockReturnValue ( {
157- status : AuthStatus . Authorized ,
158- userId : 1 ,
159- profileImage : "" ,
160- } ) ;
161- render ( < GlobalNavbar /> ) ;
162- const avatar = screen . getByAltText ( "Profile" ) as HTMLImageElement ;
163- expect ( avatar ) . toBeInTheDocument ( ) ;
164- expect ( avatar . src ) . toContain ( "/img_avatar.png" ) ;
170+ it ( "renders default avatar when authorized and no profileImage" , ( ) => {
171+ ( useAuth as MockedFunction < typeof useAuth > ) . mockReturnValue (
172+ AuthStatus . Authorized ,
173+ ) ;
174+
175+ const mock = vi . fn ( ) . mockImplementation ( authService . getUser ) ;
176+ mock . mockImplementationOnce ( ( ) => {
177+ return {
178+ id : 1 ,
179+ username : "test" ,
180+ refreshToken : "test" ,
181+ profileImage : "" ,
182+ } ;
165183 } ) ;
166184
167- it ( "renders user avatar when authorized and profileImage exists" , ( ) => {
168- ( useAuth as MockedFunction < typeof useAuth > ) . mockReturnValue ( {
169- status : AuthStatus . Authorized ,
170- userId : 1 ,
171- profileImage : "avatars/1.png" ,
172- } ) ;
173- render ( < GlobalNavbar /> ) ;
174- const avatar = screen . getByAltText ( "Profile" ) as HTMLImageElement ;
175- expect ( avatar ) . toBeInTheDocument ( ) ;
176- expect ( avatar . src ) . toContain ( "/api/user/avatar?userId=1" ) ;
185+ render ( < GlobalNavbar /> ) ;
186+ const avatar = screen . getByAltText ( "Profile" ) as HTMLImageElement ;
187+ expect ( avatar ) . toBeInTheDocument ( ) ;
188+ expect ( avatar . src ) . toContain ( "/img_avatar.png" ) ;
189+ } ) ;
190+
191+ it ( "renders user avatar when authorized and profileImage exists" , ( ) => {
192+ ( useAuth as MockedFunction < typeof useAuth > ) . mockReturnValue (
193+ AuthStatus . Authorized ,
194+ ) ;
195+
196+ ( authService . getUser as ReturnType < typeof vi . fn > ) . mockReturnValue ( {
197+ id : 1 ,
198+ username : "test" ,
199+ refreshToken : "test" ,
200+ profileImage : "avatars/1.png" ,
177201 } ) ;
178202
203+ // In your test setup file or at the top of the test file
204+
205+ render ( < GlobalNavbar /> ) ;
206+ const avatar = screen . getByAltText ( "Profile" ) as HTMLImageElement ;
207+ expect ( avatar ) . toBeInTheDocument ( ) ;
208+ expect ( avatar . src ) . toContain ( "/api/user/avatar?userId=1" ) ;
209+ } ) ;
179210} ) ;
0 commit comments