--- title: Testing order: 9 --- # Testing [MODES: framework, data] ## Introduction When components use things like `useLoaderData`, ``, etc, they are required to be rendered in context of a React Router app. The `createRoutesStub` function creates that context to test components in isolation. Consider a login form component that relies on `useActionData` ```tsx import { useActionData } from "react-router"; export function LoginForm() { const { errors } = useActionData(); return (
); } ``` We can test this component with `createRoutesStub`. It takes an array of objects that resemble route modules with loaders, actions, and components. ```tsx import { createRoutesStub } from "react-router"; import { render, screen, waitFor, } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { LoginForm } from "./LoginForm"; test("LoginForm renders error messages", async () => { const USER_MESSAGE = "Username is required"; const PASSWORD_MESSAGE = "Password is required"; const Stub = createRoutesStub([ { path: "/login", Component: LoginForm, action() { return { errors: { username: USER_MESSAGE, password: PASSWORD_MESSAGE, }, }; }, }, ]); // render the app stub at "/login" render(