diff --git a/README.md b/README.md index 3c26016..da55a01 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/components/NavigationBar.test.tsx b/src/components/NavigationBar.test.tsx index eac5b57..33f466c 100644 --- a/src/components/NavigationBar.test.tsx +++ b/src/components/NavigationBar.test.tsx @@ -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 ( - - - {children} - - - ); -} - function renderComponent() { - return { - ...render(, { wrapper: Wrappers }), - user: userEvent.setup(), - }; + return renderComponentInner(NavigationBar); } describe("NavigationBar", () => { diff --git a/src/testHelpers/components.tsx b/src/testHelpers/components.tsx new file mode 100644 index 0000000..4f045f3 --- /dev/null +++ b/src/testHelpers/components.tsx @@ -0,0 +1,12 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { NavigationProvider } from "@/hooks/useNavigation"; + +export function TestComponentWrapper({ children }) { + return ( + + + {children} + + + ); +} diff --git a/src/testHelpers/renderUtils.tsx b/src/testHelpers/renderUtils.tsx new file mode 100644 index 0000000..5e3a62e --- /dev/null +++ b/src/testHelpers/renderUtils.tsx @@ -0,0 +1,16 @@ +import { render } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; + +import { TestComponentWrapper } from "./components"; + +export function renderComponent( + Component, + props?: ComponentProps, +) { + return { + ...render(, { + wrapper: TestComponentWrapper, + }), + user: userEvent.setup(), + }; +} diff --git a/testSetup.tsx b/testSetup.tsx new file mode 100644 index 0000000..49afee5 --- /dev/null +++ b/testSetup.tsx @@ -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 ( + + + {children} + + + ); +}