Skip to content

Commit

Permalink
fix: Add a test for useSession
Browse files Browse the repository at this point in the history
  • Loading branch information
bharatkashyap committed Oct 21, 2024
1 parent f2ef0b7 commit c161742
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/toolpad-core/src/useSession/useSession.test.tsx
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();
});
});

0 comments on commit c161742

Please sign in to comment.