Skip to content

Commit 73209f5

Browse files
authored
docs(sentry): Add dev docs for renderHookWithProviders (#14941)
Also adds docs about additionalWrapper
1 parent 4ef2b39 commit 73209f5

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

develop-docs/frontend/using-rtl.mdx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ const organization = OrganizationFixture({access: ['org:admin'], features: ['my-
3232
render(<Example />, {organization});
3333
```
3434

35+
### Add extra providers
36+
37+
If your component needs more context, pass an `additionalWrapper` to `render()` to compose extra providers inside our default test providers.
38+
39+
```tsx
40+
import {render, screen} from "sentry-test/reactTestingLibrary";
41+
42+
function MyExtraProviders({children}: {children: React.ReactNode}) {
43+
return <SomeContext.Provider value={{}}>{children}</SomeContext.Provider>;
44+
}
45+
46+
render(<Example />, {additionalWrapper: MyExtraProviders});
47+
48+
expect(screen.getByText(/example/i)).toBeInTheDocument();
49+
```
50+
3551
## Testing route changes
3652

3753
When using `render()`, an in-memory router is used, which will react to navigations with `useNavigate()` or interations with `Link` components. If your component relies on the URL, you can define the initial state in `initialRouterConfig`. You can access the current router state by referencing the returned `router` class, as well as navigate programmatically with `router.navigate()`.
@@ -221,3 +237,55 @@ import {render, screen, userEvent} from "sentry-test/reactTestingLibrary";
221237
render(<Example />);
222238
userEvent.type(screen.getByLabelText("Search by name"), "sentry");
223239
```
240+
241+
## Testing hooks with providers
242+
243+
Use `renderHookWithProviders()` to test hooks with the same built-in providers as `render()` (organization, theme, query client, and an in-memory router). It returns the regular `renderHook` result plus a `router` helper you can use to inspect location and navigate.
244+
245+
Set the initial URL and define route patterns via `initialRouterConfig` (use `route` for a single route or `routes` for multiple):
246+
247+
```tsx
248+
import {useParams} from 'react-router-dom';
249+
import {renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary';
250+
251+
function useRouteId() {
252+
const {id} = useParams();
253+
return id;
254+
}
255+
256+
const {result, router} = renderHookWithProviders(useRouteId, {
257+
initialRouterConfig: {
258+
location: {pathname: '/foo/123/'},
259+
route: '/foo/:id/',
260+
},
261+
});
262+
263+
expect(result.current).toBe('123');
264+
265+
// Navigate programmatically
266+
router.navigate('/foo/456/');
267+
268+
await waitFor(() => expect(result.current).toBe('456'));
269+
```
270+
271+
You can also provide additional providers or override the default organization via options:
272+
273+
```tsx
274+
import {OrganizationFixture} from 'sentry-fixture/organization';
275+
import {renderHookWithProviders} from 'sentry-test/reactTestingLibrary';
276+
277+
function useFeatureFlag() {
278+
// implementation under test
279+
}
280+
281+
function MyExtraProviders({children}: {children: React.ReactNode}) {
282+
return <SomeContext.Provider value={{}}>{children}</SomeContext.Provider>;
283+
}
284+
285+
const organization = OrganizationFixture({features: ['my-feature-flag']});
286+
287+
const {result} = renderHookWithProviders(useFeatureFlag, {
288+
organization,
289+
additionalWrapper: MyExtraProviders,
290+
});
291+
```

0 commit comments

Comments
 (0)