import { renderHook } from '@testing-library/react'; import { FC, PropsWithChildren } from 'react'; import { JSON } from '../../types'; import { MFEDataContext, useMFEDataContext } from '../mfe-data-context'; describe(`[web-components] ${useMFEDataContext.name}`, () => { const subject = (wrapper?: FC) => renderHook(() => useMFEDataContext(), { wrapper }); describe('when the MFEDataContext is provided by a parent', () => { const data = { foo: 'bar' }; const Wrapper: FC = ({ children }) => { return {children}; }; test('returns the context', () => { expect(subject(Wrapper).result.current).toEqual(data); }); }); describe('when the MFEDataContext is not provided by a parent', () => { const Wrapper: FC = ({ children }) => { return
{children}
; }; test('returns an empty object', () => { expect(subject(Wrapper).result.current).toEqual({}); }); }); describe('type definitions', () => { test('accepts properties typed as JSON', () => { renderHook(() => useMFEDataContext<{ data: JSON }>()); }); test('accepts properties typed as Record', () => { renderHook(() => useMFEDataContext<{ metadata: Record }>()); }); test('accepts primitive properties', () => { renderHook(() => useMFEDataContext<{ name: string; count: number; active: boolean }>()); }); test('accepts nested objects and arrays', () => { renderHook(() => useMFEDataContext<{ nested: { value: number }; list: string[] }>()); }); test('rejects non-serializable properties', () => { // @ts-expect-error Type '() => void' is not assignable to type 'never'. renderHook(() => useMFEDataContext<{ callback: () => void }>()); // @ts-expect-error Type 'Date' is not assignable to type 'never'. renderHook(() => useMFEDataContext<{ created: Date }>()); // @ts-expect-error Type 'Map' is not assignable to type 'never'. renderHook(() => useMFEDataContext<{ lookup: Map }>()); // @ts-expect-error Type 'Set' is not assignable to type 'never'. renderHook(() => useMFEDataContext<{ tags: Set }>()); // @ts-expect-error Type 'symbol' is not assignable to type 'never'. renderHook(() => useMFEDataContext<{ id: symbol }>()); }); }); });