refactor: extract reusable test+render utils
This commit is contained in:
parent
30aa3ee0a4
commit
51c8b929b3
5 changed files with 53 additions and 18 deletions
|
@ -24,6 +24,14 @@ two](https://github.com/mcataford/rss-reader/discussions/10) if you do. :tada:
|
|||
Once set up, `yarn start` will run the application locally (including a local instance of the Netlify function that
|
||||
handles CORS proxying).
|
||||
|
||||
### Testing
|
||||
|
||||
Frontend component tests are written using [React Testing Library](https://testing-library.com/docs/react-testing-library/intro/).
|
||||
|
||||
Rendering components should be done via the `testHelpers/renderUtils` exports, which provides a `renderComponent` helper
|
||||
that wraps the component in all the contexts provided to the application. This also sets up
|
||||
`@testing-library/user-events`.
|
||||
|
||||
## Contributing
|
||||
|
||||
The project welcomes contributions as long as they fit within the general roadmap, which is still TBD. Any contribution
|
||||
|
|
|
@ -1,28 +1,13 @@
|
|||
import { describe, it, expect, vi, afterEach } from "vitest";
|
||||
import { screen, render } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { screen } from "@testing-library/react";
|
||||
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { NavigationProvider } from "@/hooks/useNavigation";
|
||||
import { renderComponent as renderComponentInner } from "@/testHelpers/renderUtils";
|
||||
import * as NavigationHook from "@/hooks/useNavigation";
|
||||
|
||||
import NavigationBar from "./NavigationBar";
|
||||
|
||||
function Wrappers({ children }) {
|
||||
return (
|
||||
<NavigationProvider>
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
{children}
|
||||
</QueryClientProvider>
|
||||
</NavigationProvider>
|
||||
);
|
||||
}
|
||||
|
||||
function renderComponent() {
|
||||
return {
|
||||
...render(<NavigationBar />, { wrapper: Wrappers }),
|
||||
user: userEvent.setup(),
|
||||
};
|
||||
return renderComponentInner(NavigationBar);
|
||||
}
|
||||
|
||||
describe("NavigationBar", () => {
|
||||
|
|
12
src/testHelpers/components.tsx
Normal file
12
src/testHelpers/components.tsx
Normal file
|
@ -0,0 +1,12 @@
|
|||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { NavigationProvider } from "@/hooks/useNavigation";
|
||||
|
||||
export function TestComponentWrapper({ children }) {
|
||||
return (
|
||||
<NavigationProvider>
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
{children}
|
||||
</QueryClientProvider>
|
||||
</NavigationProvider>
|
||||
);
|
||||
}
|
16
src/testHelpers/renderUtils.tsx
Normal file
16
src/testHelpers/renderUtils.tsx
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { render } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
|
||||
import { TestComponentWrapper } from "./components";
|
||||
|
||||
export function renderComponent<ComponentProps>(
|
||||
Component,
|
||||
props?: ComponentProps,
|
||||
) {
|
||||
return {
|
||||
...render(<Component {...(props ?? {})} />, {
|
||||
wrapper: TestComponentWrapper,
|
||||
}),
|
||||
user: userEvent.setup(),
|
||||
};
|
||||
}
|
14
testSetup.tsx
Normal file
14
testSetup.tsx
Normal file
|
@ -0,0 +1,14 @@
|
|||
import userEvent from "@testing-library/user-event";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
|
||||
import { NavigationProvider } from "@/hooks/useNavigation";
|
||||
|
||||
export function TestComponentWrappers({ children }) {
|
||||
return (
|
||||
<NavigationProvider>
|
||||
<QueryClientProvider client={new QueryClient()}>
|
||||
{children}
|
||||
</QueryClientProvider>
|
||||
</NavigationProvider>
|
||||
);
|
||||
}
|
Loading…
Reference in a new issue