From c161742325316aa9c9a5cddb670e6a5ca2bd4a9c Mon Sep 17 00:00:00 2001 From: Bharat Kashyap Date: Mon, 21 Oct 2024 17:00:41 +0530 Subject: [PATCH] fix: Add a test for `useSession` --- .../src/useSession/useSession.test.tsx | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 packages/toolpad-core/src/useSession/useSession.test.tsx diff --git a/packages/toolpad-core/src/useSession/useSession.test.tsx b/packages/toolpad-core/src/useSession/useSession.test.tsx new file mode 100644 index 00000000000..d0add057a91 --- /dev/null +++ b/packages/toolpad-core/src/useSession/useSession.test.tsx @@ -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 {children}; +} + +describe('useSession hook', () => { + test('should return session data when authenticated', () => { + const { result } = renderHook(() => useSession(), { + wrapper: ({ children }) => {children}, + }); + + expect(result.current).toEqual(mockSession); + }); + + test('should return null session when not authenticated', () => { + const { result } = renderHook(() => useSession(), { + wrapper: ({ children }) => {children}, + }); + + expect(result.current).toBeNull(); + }); +});