-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2ef0b7
commit c161742
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { describe, test, expect } from 'vitest'; | ||
import { useSession } from './useSession'; | ||
import { Session, SessionContext } from '../AppProvider/AppProvider'; | ||
|
||
// Mock the session data | ||
const mockSession = { | ||
user: { | ||
name: 'Bharat Kashyap', | ||
email: 'bharat@mui.com', | ||
image: 'https://avatars.githubusercontent.com/u/19550456', | ||
}, | ||
}; | ||
|
||
interface TestWrapperProps { | ||
session: Session | null; | ||
children: React.ReactNode; | ||
} | ||
|
||
function TestWrapper({ children, session }: TestWrapperProps) { | ||
return <SessionContext.Provider value={session}>{children}</SessionContext.Provider>; | ||
} | ||
|
||
describe('useSession hook', () => { | ||
test('should return session data when authenticated', () => { | ||
const { result } = renderHook(() => useSession(), { | ||
wrapper: ({ children }) => <TestWrapper session={mockSession}>{children}</TestWrapper>, | ||
}); | ||
|
||
expect(result.current).toEqual(mockSession); | ||
}); | ||
|
||
test('should return null session when not authenticated', () => { | ||
const { result } = renderHook(() => useSession(), { | ||
wrapper: ({ children }) => <TestWrapper session={null}>{children}</TestWrapper>, | ||
}); | ||
|
||
expect(result.current).toBeNull(); | ||
}); | ||
}); |