@homebound/rtl-utils

Common test helpers for use with React Testing Library.

Usage no npm install needed!

<script type="module">
  import homeboundRtlUtils from 'https://cdn.skypack.dev/@homebound/rtl-utils';
</script>

README

rtl-utils

Common test helpers for use with React Testing Library.

rtl-utils provides syntax sugar for rendering a Component in RTL with app-specific state/wrapper components, as well as some convenience methods for generating test events. Some common wrappers are also included.

Examples

Router wrapper

atUrl will wrap a test component in a MemoryRouter at a specific URL, e.g.

import { atUrl, render } from "@homebound/rtl-utils";

...

await render(<ContactPanel {...props} />, atUrl("/contact"));

Apollo wrapper

Returns an Apollo provider that will respond with the mocks responses.

import { render, withApollo } from "@homebound/rtl-utils";

...

const apollo = withApollo([
  newEstimateResponse(),
  newTestSaveLeadResponse({ data: { errorMessage: "error happened" } }),
]);
const c = await render(<GraphQLPage {...props} />, apollo);

History wrapper

Returns a Router wrapper with a its history prop set to newHistoryMock if the user does not provide a history mock of their own.

import { render, withHistory } from "@homebound/rtl-utils";
import { newHistoryMock } from "../auth/testHistory";

...

const c = await render(<ScheduleTable {...props} />, withHistory(historyMock));