|
| 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 [<DataProvider />](./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