0
votes

I'm trying to test a hook that returns a tree of react elements, it works as expected when I pass the resulting object directly into JSX using something like

const newLayout = useLayoutBuilder(layout);
...
return <div>{newLayout}</div>

But when I try to test it using react testing library it throws the "Objects are not valid as a react child error". Calling the rerender method on the resulting object stops the error but the resulting DOM has nothing in it

This is the hook's return:

<EditorComponent testid={layout.id} id={layout.id} key={layout.id}>{layout.children.map((component, index) => buildLayout(component, nextPath, index, layout.id, newStackingDirection))}</EditorComponent>

This is what I've got so far in the test:

describe('useLayoutBuilder', () => {
    it('should do something', () => {
        const NewLayout = renderHook(() => useLayoutBuilder(layout))
        const {container} = render(NewLayout.rerender());
        const compnents = container.querySelector('#parent')
        console.log(compnents);
    });
});

Why does this work normally but not in test?