import * as React from 'react';
import { renderHook } from '@testing-library/react';
import { IdentityContext } from '../identityContext';
import { Identity } from '../types';
import { useIdentity } from '../useIdentity';

describe('useIdentity', () => {
    const renderIdentityHook = (identity?: Identity) =>
        renderHook(() => useIdentity(), {
            wrapper: ({ children }: { children: React.ReactNode }) => (
                <IdentityContext.Provider value={{ identity }}>{children}</IdentityContext.Provider>
            ),
        });

    test('returns identity in context', () => {
        const identity = {} as Identity;
        const { result } = renderIdentityHook(identity);
        expect(result.current).toBe(identity);
    });

    test('throws error if not set', () => {
        expect(() => {
            renderIdentityHook();
        }).toThrow('IdentityContext not initialized with an identity');
    });
});
