Skip to content

Commit 28b702d

Browse files
authored
enhance: Add renderDataHook export (#3238)
1 parent fdd3680 commit 28b702d

File tree

20 files changed

+279
-98
lines changed

20 files changed

+279
-98
lines changed

.changeset/brown-crews-impress.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@data-client/test': patch
3+
---
4+
5+
Add renderDataHook() export
6+
7+
This can be used instead of `makeRenderDataHook(DataProvider)`
8+
9+
```ts
10+
import { renderDataHook } from '@data-client/test';
11+
12+
const { result, waitFor } = renderDataHook(
13+
() => {
14+
return useSuspense(ArticleResource.get, { id: 5 });
15+
},
16+
{
17+
initialFixtures: [
18+
{
19+
endpoint: ArticleResource.get,
20+
args: [{ id: 5 }],
21+
response,
22+
},
23+
],
24+
},
25+
);
26+
```

.changeset/gorgeous-dingos-push.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@data-client/react': patch
3+
'@data-client/test': patch
4+
---
5+
6+
Update test docs link

.changeset/wild-elephants-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@data-client/test': patch
3+
---
4+
5+
makeRenderDataClient() -> makeRenderDataHook() (but keep the old name still)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ For the small price of 9kb gziped.    [🏁Get started now](https://da
348348
</table>
349349

350350
- Components: [&lt;DataProvider/>](https://dataclient.io/docs/api/DataProvider), [&lt;AsyncBoundary/>](https://dataclient.io/docs/api/AsyncBoundary), [&lt;ErrorBoundary/>](https://dataclient.io/docs/api/ErrorBoundary), [&lt;MockResolver/>](https://dataclient.io/docs/api/MockResolver)
351-
- Data Mocking: [Fixture](https://dataclient.io/docs/api/Fixtures#successfixture), [Interceptor](https://dataclient.io/docs/api/Fixtures#interceptor), [renderDataClient()](https://dataclient.io/docs/api/makeRenderDataClient)
351+
- Data Mocking: [Fixture](https://dataclient.io/docs/api/Fixtures#successfixture), [Interceptor](https://dataclient.io/docs/api/Fixtures#interceptor), [renderDataHook()](https://dataclient.io/docs/api/renderDataHook)
352352
- Middleware: [LogoutManager](https://dataclient.io/docs/api/LogoutManager), [NetworkManager](https://dataclient.io/docs/api/NetworkManager), [SubscriptionManager](https://dataclient.io/docs/api/SubscriptionManager), [PollingSubscription](https://dataclient.io/docs/api/PollingSubscription), [DevToolsManager](https://dataclient.io/docs/api/DevToolsManager)
353353

354354
### [Define Data](https://dataclient.io/docs/getting-started/resource)

docs/core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ const incrementInterceptor: Interceptor = {
618618
</Tabs>
619619

620620
- [Mock data for storybook](./guides/storybook.md) with [MockResolver](./api/MockResolver.md)
621-
- [Test hooks](./guides/unit-testing-hooks.md) with [makeRenderDataClient()](./api/makeRenderDataClient.md)
621+
- [Test hooks](./guides/unit-testing-hooks.md) with [renderDataHook()](./api/renderDataHook.md)
622622
- [Test components](./guides/unit-testing-components.md) with [MockResolver](./api/MockResolver.md) and [mockInitialState()](./api/mockInitialState.md)
623623

624624
## Demo

docs/core/api/Controller.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ and retrieval performance.
2222

2323
- [Managers](./Manager.md) as the first argument in [Manager.middleware](./Manager.md#middleware)
2424
- React with [useController()](./useController.md)
25-
- [Unit testing hooks](../guides/unit-testing-hooks.md) with [renderDataClient()](./makeRenderDataClient.md#controller)
25+
- [Unit testing hooks](../guides/unit-testing-hooks.md) with [renderDataHook()](./renderDataHook.md#controller)
2626

2727
```ts
2828
class Controller {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: makeRenderDataHook()
3+
---
4+
5+
```typescript
6+
function makeRenderDataHook(
7+
Provider: React.ComponentType<ProviderProps>,
8+
): RenderDataClientFunction;
9+
```
10+
11+
`makeRenderDataHook()` is useful to test hooks that rely on the `Reactive Data Client`. It creates a renderDataClient()
12+
function that mirrors [@testing-library/react-hooks](https://github.com/testing-library/react-hooks-testing-library)'s [renderHook()](https://react-hooks-testing-library.com/reference/api#renderhook-options) but does so with a `<Suspense/>` boundary
13+
as well as in a `<Provider />` context.
14+
15+
## Arguments
16+
17+
### Provider
18+
19+
```typescript
20+
interface ProviderProps {
21+
children: React.ReactNode;
22+
managers: Manager[];
23+
initialState: State<unknown>;
24+
Controller: typeof Controller;
25+
}
26+
```
27+
28+
The Reactive Data Client [&lt;DataProvider /&gt;](./DataProvider.md)
29+
30+
- `import { DataProvider } from @data-client/react;`
31+
- `import { DataProvider } from @data-client/react/redux;`
32+
33+
## Example
34+
35+
36+
```typescript
37+
import { DataProvider } from '@data-client/react/redux';
38+
import { makeRenderDataHook } from '@data-client/test';
39+
40+
const response = {
41+
id: 5,
42+
title: 'hi ho',
43+
content: 'whatever',
44+
tags: ['a', 'best', 'react'],
45+
};
46+
47+
beforeEach(() => {
48+
renderDataHook = makeRenderDataHook(DataProvider);
49+
});
50+
51+
it('should resolve useSuspense()', async () => {
52+
const { result, waitFor } = renderDataHook(
53+
() => {
54+
return useSuspense(ArticleResource.get, response);
55+
},
56+
{
57+
resolverFixtures: [
58+
{
59+
endpoint: ArticleResource.get,
60+
response: ({ id }) => ({ ...response, id }),
61+
},
62+
{
63+
endpoint: ArticleResource.partialUpdate,
64+
response: ({ id }, body) => ({ ...body, id }),
65+
},
66+
],
67+
},
68+
);
69+
// this indicates suspense
70+
expect(result.current).toBeUndefined();
71+
await waitFor(() => expect(result.current).toBeDefined());
72+
expect(result.current instanceof ArticleResource).toBe(true);
73+
expect(result.current.title).toBe(response.title);
74+
await controller.fetch(
75+
ArticleResource.partialUpdate,
76+
{ id: response.id },
77+
{ title: 'updated title' },
78+
);
79+
expect(result.current.title).toBe('updated title');
80+
});
81+
```

0 commit comments

Comments
 (0)