Replies: 2 comments 5 replies
-
Hi @monocle, This is really great. Thanks for sharing the code :). |
Beta Was this translation helpful? Give feedback.
1 reply
-
Thanks a lot @monocle , works great <3 The only thing is that these 3 classes are marked as deprecated : I used the suggested methods from TanStack and here is my code in the end : // utils.tsx
import { Outlet, RouterProvider, createMemoryHistory, createRootRoute, createRoute, createRouter } from '@tanstack/react-router';
import { render } from '@testing-library/react';
function createTestRouter(component: () => JSX.Element) {
const rootRoute = createRootRoute({
component: Outlet,
});
const componentRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component,
});
const router = createRouter({
routeTree: rootRoute.addChildren([componentRoute]),
history: createMemoryHistory(),
});
return router;
}
export function renderWithContext(component: () => JSX.Element) {
const router = createTestRouter(component);
return render(<RouterProvider router={router} />);
} // my-test.test.tsx
import '@testing-library/jest-dom';
import { screen, waitFor } from '@testing-library/react';
import { expect, test } from 'vitest';
import { MyComponent } from './my-component.tsx';
import { renderWithContext } from './utils';
test('MyComponent render correctly', async () => {
renderWithContext(MyComponent);
expect(screen).toBeDefined();
await waitFor(() => expect(screen.getByRole('heading')).toBeInTheDocument());
expect(screen.getByRole('heading')).toHaveTextContent('My title');
}); |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Spent the morning figuring this out. Not sure if this is the best way to get testing to work. This is using Vitest.
Had to use
waitFor
with the memory history. However, when I used thecreateHashHistory
that didn't seem to be necessary.If there is a better way to do this, please let me know.
(Might be helpful for #583)
Beta Was this translation helpful? Give feedback.
All reactions