import * as React from 'react';
import { renderHook } from '@testing-library/react';
import { AuthClientContext, useAuthClient } from '../authContext';
import { AuthClient } from '../types';
import { createAuthClient } from './mocks';

describe('useAuthClient', () => {
    const renderAuthHook = (authClient?: AuthClient) =>
        renderHook(() => useAuthClient(), {
            wrapper: ({ children }: { children: React.ReactNode }) => (
                <AuthClientContext.Provider value={{ authClient }}>
                    {children}
                </AuthClientContext.Provider>
            ),
        });

    test('returns client in context', () => {
        const authClient = createAuthClient();
        const { result } = renderAuthHook(authClient);
        expect(result.current).toEqual(authClient);
    });

    test('throws error if not set', () => {
        const consoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
        expect(() => {
            renderAuthHook();
        }).toThrow('AuthContext not initialized with an AuthClient');
        consoleError.mockRestore();
    });
});
