import { act } from 'react';
import { createRoot } from 'react-dom/client';

export function renderHook<T>(useHook: () => T) {
    let currentValue: T;

    function TestComponent() {
        currentValue = useHook();
        return null;
    }

    const container = document.createElement('div');
    const root = createRoot(container);

    act(() => {
        root.render(<TestComponent />);
    });

    return {
        result: {
            get current() {
                return currentValue;
            },
        },
        rerender() {
            act(() => {
                root.render(<TestComponent />);
            });
        },
        unmount() {
            act(() => {
                root.unmount();
            });
        },
    };
}
