-
I've been following the official documentation to create per-page layouts with Next. However, I'm not quite sure how to test these in isolation using React Testing Library. I'm a little rusty with TypeScript, so apologies if the answer is blindingly obvious. The layout component looks something like this: const MyLayout = (page: ReactElement) => (
<div className="container">
{page}
<span>Footnote</span>
</div>
); And the corresponding test: describe('MyLayout', () => {
it('renders the footnote', () => {
render() // <-- render MyLayout here.
const footnote = screen.getByText('Footnote');
expect(footnote).toBeInTheDocument();
}
} For context, my pages consume the layout using Any guidance is much appreciated. Thanks for taking a look! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
I guess you want to do something like this? describe('MyLayout', () => {
it('renders the footnote', () => {
render(MyLayout(<div>Dummy</div>)); // <-- render MyLayout here.
const footnote = screen.getByText('Footnote');
expect(footnote).toBeInTheDocument();
expect(screen.getByText(/dummy/i)).toBeInTheDocument();
});
}); |
Beta Was this translation helpful? Give feedback.
-
I had a error and I made this.
Success
|
Beta Was this translation helpful? Give feedback.
-
I've been using this approach: const renderComponent = () => {
const page = <MyPage />;
const pageWithLayout = MyPage.getLayout(page);
return render(pageWithLayout);
}; |
Beta Was this translation helpful? Give feedback.
-
You can test the layout in isolation by rendering it as a regular React component — just wrap a simple mock page element inside it. For example: import { render, screen } from '@testing-library/react'; describe('MyLayout', () => { Mock Page ;render(MyLayout(mockPage));
}); Since your layout function takes a ReactElement and returns JSX, you can just call it directly in the test. |
Beta Was this translation helpful? Give feedback.
I guess you want to do something like this?