Skip to content

Commit

Permalink
test improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Keresztes Zsolt <178369172+zsoltker@users.noreply.github.com>
  • Loading branch information
zsoltker committed Feb 4, 2025
1 parent ff8483a commit d243adb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
28 changes: 23 additions & 5 deletions tests/common/testHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import React, { ReactNode } from 'react';
import { MemoryRouter } from 'react-router';
import { Route, Routes } from 'react-router-dom';

export const withRouter = (component: ReactNode) => (<>
<MemoryRouter>
{component}
</MemoryRouter>
</>)
/**
* Wraps a component with a MemoryRouter.
* @param component - The React node to wrap.
* @param initialEntry - (Optional) The initial route for the router.
* @param path - (Optional) The route path to render the component.
*/
export const withRouter = (component: ReactNode, initialEntry?: string, path?: string) => {
if (initialEntry && path) {
return (<>
<MemoryRouter initialEntries={[initialEntry]}>
<Routes>
<Route path={path} element={component}/>
</Routes>
</MemoryRouter>
</>);
}
return (<>
<MemoryRouter>
{component}
</MemoryRouter>
</>)
}
10 changes: 2 additions & 8 deletions tests/components/resources/ResourceDetailsPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import '@testing-library/jest-dom';
import { MemoryRouter, Route, Routes } from 'react-router-dom';

import ResourceDetailsPage from '../../../src/components/resources/ResourceDetailsPage';
import { withRouter } from '../../common/testHelper';

jest.mock('@vcmap/core', () => ({
VcsApp: jest.fn().mockImplementation(() => ({
Expand Down Expand Up @@ -54,13 +54,7 @@ describe('ResourceDetailsPage', () => {

it('should render a resource detail page', async () => {

render(
<MemoryRouter initialEntries={['/resources/123']}>
<Routes>
<Route path="/resources/:resourceId" element={<ResourceDetailsPage/>}/>
</Routes>
</MemoryRouter>
);
render(withRouter(<ResourceDetailsPage/>, '/resources/123', '/resources/:resourceId'));

await waitFor(() => {
expect(screen.getByRole('heading', { name: /Resource 1/i })).toBeInTheDocument();
Expand Down

0 comments on commit d243adb

Please sign in to comment.