@@ -6,6 +6,7 @@ import React from 'react';
66import { render , screen , fireEvent , waitFor , act } from '@testing-library/react' ;
77import '@testing-library/jest-dom' ;
88import SessionBox from '@/components/messageSystem/SessionBox' ;
9+ import { useSessionActions } from '@/hooks/useSessionActions' ;
910
1011// Mock Next.js router
1112const mockPush = jest . fn ( ) ;
@@ -87,6 +88,11 @@ jest.mock('@/utils/avatarUtils', () => ({
8788 processAvatarUrl : jest . fn ( ( url ) => url || '/default-avatar.png' ) ,
8889} ) ) ;
8990
91+ // Mock the useSessionActions hook
92+ jest . mock ( '@/hooks/useSessionActions' , ( ) => ( {
93+ useSessionActions : jest . fn ( ) ,
94+ } ) ) ;
95+
9096// Mock data
9197const mockUser = {
9298 _id : '6873cc50ac4e1d6e1cddf33f' ,
@@ -110,8 +116,8 @@ const mockSession = {
110116 _id : 'session-id-1' ,
111117 user1Id : mockUser ,
112118 user2Id : mockOtherUser ,
113- skill1Id : { _id : 'skill1' , name : 'React' } ,
114- skill2Id : { _id : 'skill2' , name : 'Node.js' } ,
119+ skill1Id : { _id : 'skill1' , skillName : 'React' } ,
120+ skill2Id : { _id : 'skill2' , skillName : 'Node.js' } ,
115121 descriptionOfService1 : 'I can teach React basics' ,
116122 descriptionOfService2 : 'I want to learn Node.js' ,
117123 startDate : '2025-01-15' ,
@@ -197,9 +203,33 @@ describe('SessionBox Component', () => {
197203 onSessionUpdate : jest . fn ( )
198204 } ;
199205
206+ // Default mock implementation for useSessionActions
207+ const defaultMockSessionActions = {
208+ sessions : [ ] ,
209+ counterOffers : { } ,
210+ loading : false ,
211+ processingSession : null ,
212+ pendingSessionCount : 0 ,
213+ activeSessionCount : 0 ,
214+ fetchSessions : jest . fn ( ) ,
215+ handleAcceptReject : jest . fn ( ) ,
216+ handleDeleteSession : jest . fn ( ) ,
217+ handleCounterOfferResponse : jest . fn ( ) ,
218+ handleRequestCompletion : jest . fn ( ) ,
219+ handleCompletionResponse : jest . fn ( ) ,
220+ handleRatingSubmit : jest . fn ( ) ,
221+ setLoading : jest . fn ( ) ,
222+ setSessions : jest . fn ( ) ,
223+ setCounterOffers : jest . fn ( ) ,
224+ setPendingSessionCount : jest . fn ( ) ,
225+ setActiveSessionCount : jest . fn ( )
226+ } ;
227+
200228 beforeEach ( ( ) => {
201229 jest . clearAllMocks ( ) ;
202230 global . fetch = createMockFetch ( { } ) ;
231+ // Reset to default mock implementation
232+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( defaultMockSessionActions ) ;
203233 } ) ;
204234
205235 afterEach ( ( ) => {
@@ -210,6 +240,12 @@ describe('SessionBox Component', () => {
210240 it ( 'should show New Session button' , async ( ) => {
211241 global . fetch = createMockFetch ( { sessions : [ ] } ) ;
212242
243+ // Mock the hook to return empty sessions
244+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
245+ ...defaultMockSessionActions ,
246+ sessions : [ ]
247+ } ) ;
248+
213249 render ( < SessionBox { ...defaultProps } /> ) ;
214250
215251 await waitFor ( ( ) => {
@@ -220,6 +256,12 @@ describe('SessionBox Component', () => {
220256 it ( 'should show empty state when no sessions exist' , async ( ) => {
221257 global . fetch = createMockFetch ( { sessions : [ ] } ) ;
222258
259+ // Mock the hook to return empty sessions
260+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
261+ ...defaultMockSessionActions ,
262+ sessions : [ ]
263+ } ) ;
264+
223265 render ( < SessionBox { ...defaultProps } /> ) ;
224266
225267 await waitFor ( ( ) => {
@@ -236,11 +278,19 @@ describe('SessionBox Component', () => {
236278 counterOffers : [ ]
237279 } ) ;
238280
281+ // Mock the hook to return the session
282+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
283+ ...defaultMockSessionActions ,
284+ sessions : [ mockSession ] ,
285+ pendingSessionCount : 1 ,
286+ activeSessionCount : 1
287+ } ) ;
288+
239289 render ( < SessionBox { ...defaultProps } /> ) ;
240290
241291 await waitFor ( ( ) => {
242- expect ( screen . getByText ( 'I can teach React basics ' ) ) . toBeInTheDocument ( ) ;
243- expect ( screen . getByText ( 'I want to learn Node.js' ) ) . toBeInTheDocument ( ) ;
292+ expect ( screen . getByText ( 'React' ) ) . toBeInTheDocument ( ) ;
293+ expect ( screen . getByText ( 'Node.js' ) ) . toBeInTheDocument ( ) ;
244294 expect ( screen . getByText ( 'pending' ) ) . toBeInTheDocument ( ) ;
245295 } ) ;
246296 } ) ;
@@ -257,6 +307,14 @@ describe('SessionBox Component', () => {
257307 counterOffers : [ ]
258308 } ) ;
259309
310+ // Mock the hook to return multiple sessions
311+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
312+ ...defaultMockSessionActions ,
313+ sessions : multiplePendingSessions ,
314+ pendingSessionCount : 3 ,
315+ activeSessionCount : 3
316+ } ) ;
317+
260318 render ( < SessionBox { ...defaultProps } /> ) ;
261319
262320 await waitFor ( ( ) => {
@@ -269,6 +327,12 @@ describe('SessionBox Component', () => {
269327 it ( 'should open create session modal when New Session button is clicked' , async ( ) => {
270328 global . fetch = createMockFetch ( { sessions : [ ] } ) ;
271329
330+ // Mock the hook to return empty sessions
331+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
332+ ...defaultMockSessionActions ,
333+ sessions : [ ]
334+ } ) ;
335+
272336 render ( < SessionBox { ...defaultProps } /> ) ;
273337
274338 await waitFor ( ( ) => {
@@ -291,6 +355,14 @@ describe('SessionBox Component', () => {
291355 counterOffers : [ ]
292356 } ) ;
293357
358+ // Mock the hook to return 3 active sessions
359+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
360+ ...defaultMockSessionActions ,
361+ sessions : multiplePendingSessions ,
362+ pendingSessionCount : 3 ,
363+ activeSessionCount : 3
364+ } ) ;
365+
294366 render ( < SessionBox { ...defaultProps } /> ) ;
295367
296368 await waitFor ( ( ) => {
@@ -322,7 +394,7 @@ describe('SessionBox Component', () => {
322394 render ( < SessionBox { ...defaultProps } /> ) ;
323395
324396 await waitFor ( ( ) => {
325- expect ( screen . getByText ( 'Failed to load user information ' ) ) . toBeInTheDocument ( ) ;
397+ expect ( screen . getByText ( 'Failed to load user' ) ) . toBeInTheDocument ( ) ;
326398 } ) ;
327399 } ) ;
328400
@@ -351,6 +423,12 @@ describe('SessionBox Component', () => {
351423 it ( 'should close create session modal when close button is clicked' , async ( ) => {
352424 global . fetch = createMockFetch ( { sessions : [ ] } ) ;
353425
426+ // Mock the hook to return empty sessions
427+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
428+ ...defaultMockSessionActions ,
429+ sessions : [ ]
430+ } ) ;
431+
354432 render ( < SessionBox { ...defaultProps } /> ) ;
355433
356434 await waitFor ( ( ) => {
@@ -374,6 +452,14 @@ describe('SessionBox Component', () => {
374452 counterOffers : [ ]
375453 } ) ;
376454
455+ // Mock the hook to return the session
456+ ( useSessionActions as jest . MockedFunction < typeof useSessionActions > ) . mockReturnValue ( {
457+ ...defaultMockSessionActions ,
458+ sessions : [ mockSession ] ,
459+ pendingSessionCount : 1 ,
460+ activeSessionCount : 1
461+ } ) ;
462+
377463 render ( < SessionBox { ...defaultProps } /> ) ;
378464
379465 await waitFor ( ( ) => {
0 commit comments