Skip to content

Commit 572a078

Browse files
committed
update recipes
1 parent f1a16e8 commit 572a078

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

docs_headless/src/content/docs/recipes/Caching.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ layout: default
33
title: "Caching"
44
---
55

6-
# Caching
7-
86
Not hitting the server is the best way to improve a web app performance, and its ecological footprint too (network and datacenter usage account for about 40% of the CO2 emissions in IT). React-admin comes with a built-in cache-first approach called *optimistic rendering*, and it supports caching both at the HTTP level and the application level.
97

108
## Optimistic Rendering
@@ -86,11 +84,11 @@ Finally, if your API uses GraphQL, it probably doesn't offer HTTP caching.
8684

8785
## Application Cache
8886

89-
React-admin uses react-query for data fetching. React-query comes with its own caching system, allowing you to skip API calls completely. React-admin calls this the *application cache*. It's a good way to overcome the limitations if the HTTP cache. **This cache is opt-in** - you have to enable it by setting a custom `queryClient` in your `<Admin>` with a specific `staleTime` option.
87+
React-admin uses react-query for data fetching. React-query comes with its own caching system, allowing you to skip API calls completely. React-admin calls this the *application cache*. It's a good way to overcome the limitations if the HTTP cache. **This cache is opt-in** - you have to enable it by setting a custom `queryClient` in your `<CoreAdminContext>` with a specific `staleTime` option.
9088

9189
```jsx
9290
import { QueryClient } from '@tanstack/react-query';
93-
import { Admin, Resource } from 'react-admin';
91+
import { CoreAdminContext, CoreAdminUI } from 'ra-core';
9492

9593
const App = () => {
9694
const queryClient = new QueryClient({
@@ -101,9 +99,11 @@ const App = () => {
10199
},
102100
});
103101
return (
104-
<Admin dataProvider={dataProvider} queryClient={queryClient}>
105-
<Resource name="posts" />
106-
</Admin>
102+
<CoreAdminContext dataProvider={dataProvider} queryClient={queryClient}>
103+
<CoreAdminUI>
104+
<Resource name="posts" />
105+
</CoreAdminUI>
106+
</CoreAdminContext>
107107
);
108108
}
109109
```

docs_headless/src/content/docs/recipes/UnitTesting.md

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,45 @@ layout: default
33
title: "Unit Testing"
44
---
55

6-
# Unit Testing
7-
86
React-admin relies heavily on unit tests (powered by [Jest](https://facebook.github.io/jest/) and [react-testing-library](https://testing-library.com/docs/react-testing-library/intro)) to ensure that its code is working as expected.
97

108
That means that each individual component and hook can be tested in isolation. That also means that if you have to test your own components and hooks based on react-admin, this should be straightforward.
119

12-
## AdminContext Wrapper
10+
## CoreAdminContext Wrapper
1311

14-
Some of react-admin's components depend on a context for translation, theming, data fetching, etc. If you write a component that depends on a react-admin component, chances are the test runner will complain about a missing context.
12+
Some of react-admin's components depend on a context for translation, data fetching, etc. If you write a component that depends on a react-admin component, chances are the test runner will complain about a missing context.
1513

16-
Wrap your tested component inside `<AdminContext>` to avoid this problem:
14+
Wrap your tested component inside `<CoreAdminContext>` to avoid this problem:
1715

1816
```jsx
1917
import React from 'react';
20-
import { AdminContext } from 'react-admin';
18+
import { CoreAdminContext } from 'ra-core';
2119
import { render, screen } from '@testing-library/react';
2220

2321
import MyComponent from './MyComponent';
2422

2523
test('<MyComponent>', async () => {
2624
render(
27-
<AdminContext>
25+
<CoreAdminContext>
2826
<MyComponent />
29-
</AdminContext>
27+
</CoreAdminContext>
3028
);
3129
const items = await screen.findAllByText(/Item #[0-9]: /)
3230
expect(items).toHaveLength(10)
3331
})
3432
```
3533

36-
**Tip**: you can also pass `AdminContext` as the `wrapper` option to the `render()` function:
34+
**Tip**: you can also pass `CoreAdminContext` as the `wrapper` option to the `render()` function:
3735

3836
```jsx
3937
import React from 'react';
40-
import { AdminContext } from 'react-admin';
38+
import { CoreAdminContext } from 'ra-core';
4139
import { render, screen } from '@testing-library/react';
4240

4341
import MyComponent from './MyComponent';
4442

4543
test('<MyComponent>', async () => {
46-
render(<MyComponent />, { wrapper: AdminContext });
44+
render(<MyComponent />, { wrapper: CoreAdminContext });
4745

4846
const items = await screen.findAllByText(/Item #[0-9]: /)
4947
expect(items).toHaveLength(10)
@@ -52,25 +50,25 @@ const items = await screen.findAllByText(/Item #[0-9]: /)
5250

5351
## Mocking Providers
5452

55-
`<AdminContext>` accepts the same props as `<Admin>`, so you can pass a custom `dataProvider`, `authProvider`, or `i18nProvider` for testing purposes.
53+
`<CoreAdminContext>` accepts the same props as `<Admin>`, so you can pass a custom `dataProvider`, `authProvider`, or `i18nProvider` for testing purposes.
5654

5755
For instance, if the component to test calls the `useGetOne` hook:
5856

5957
{% raw %}
6058
```jsx
6159
import React from 'react';
62-
import { AdminContext } from 'react-admin';
60+
import { CoreAdminContext } from 'ra-core';
6361
import { render, screen } from '@testing-library/react';
6462

6563
import MyComponent from './MyComponent';
6664

6765
test('<MyComponent>', async () => {
6866
render(
69-
<AdminContext dataProvider={{
67+
<CoreAdminContext dataProvider={{
7068
getOne: () => Promise.resolve({ data: { id: 1, name: 'foo' } }),
7169
}}>
7270
<MyComponent />
73-
</AdminContext>
71+
</CoreAdminContext>
7472
);
7573
const items = await screen.findAllByText(/Item #[0-9]: /)
7674
expect(items).toHaveLength(10)
@@ -82,18 +80,18 @@ test('<MyComponent>', async () => {
8280

8381
```jsx
8482
import React from 'react';
85-
import { AdminContext, testDataProvider } from 'react-admin';
83+
import { CoreAdminContext, testDataProvider } from 'ra-core';
8684
import { render, screen } from '@testing-library/react';
8785

8886
import MyComponent from './MyComponent';
8987

9088
test('<MyComponent>', async () => {
9189
render(
92-
<AdminContext dataProvider={testDataProvider({
90+
<CoreAdminContext dataProvider={testDataProvider({
9391
getOne: () => Promise.resolve({ data: { id: 1, name: 'foo' } }),
9492
})}>
9593
<MyComponent />
96-
</AdminContext>
94+
</CoreAdminContext>
9795
);
9896
const items = await screen.findAllByText(/Item #[0-9]: /)
9997
expect(items).toHaveLength(10)
@@ -102,28 +100,28 @@ test('<MyComponent>', async () => {
102100

103101
## Resetting The Store
104102

105-
The react-admin Store is persistent. This means that if a test modifies an item in the store, the updated value will be changed in the next test. This will cause seemingly random test failures when you use `useStore()` in your tests, or any feature depending on the store (e.g. `<DataTable>` row selection, sidebar state, language selection).
103+
The react-admin Store is persistent. This means that if a test modifies an item in the store, the updated value will be changed in the next test. This will cause seemingly random test failures when you use `useStore()` in your tests, or any feature depending on the store (e.g. row selection, sidebar state, language selection).
106104

107105
To isolate your unit tests, pass a new `memoryStore` at each test:
108106

109107
```jsx
110-
import { memoryStore } from 'react-admin';
108+
import { memoryStore } from 'ra-core';
111109

112110
test('<MyComponent>', async () => {
113111
const { getByText } = render(
114-
<AdminContext store={memoryStore()}>
112+
<CoreAdminContext store={memoryStore()}>
115113
<MyComponent />
116-
</AdminContext>
114+
</CoreAdminContext>
117115
);
118116
const items = await screen.findAllByText(/Item #[0-9]: /);
119117
expect(items).toHaveLength(10);
120118
})
121119
```
122120

123-
If you don't need `<AdminContext>`, you can just wrap your component with a `<StoreContextProvider>`:
121+
If you don't need `<CoreAdminContext>`, you can just wrap your component with a `<StoreContextProvider>`:
124122

125123
```jsx
126-
import { StoreContextProvider, memoryStore } from 'react-admin';
124+
import { StoreContextProvider, memoryStore } from 'ra-core';
127125

128126
test('<MyComponent>', async () => {
129127
const { getByText } = render(
@@ -148,7 +146,7 @@ Here is an example with Jest and TestingLibrary, which is testing the [`UserShow
148146
// UserShow.spec.js
149147
import * as React from "react";
150148
import { render, fireEvent } from '@testing-library/react';
151-
import { AdminContext } from 'react-admin';
149+
import { CoreAdminContext } from 'ra-core';
152150

153151
import UserShow from './UserShow';
154152

@@ -169,9 +167,9 @@ describe('UserShow', () => {
169167
})
170168
}
171169
const testUtils = render(
172-
<AdminContext dataProvider={dataProvider}>
170+
<CoreAdminContext dataProvider={dataProvider}>
173171
<UserShow permissions="user" id="1" />
174-
</AdminContext>
172+
</CoreAdminContext>
175173
);
176174

177175
expect(testUtils.queryByDisplayValue('1')).not.toBeNull();
@@ -195,9 +193,9 @@ describe('UserShow', () => {
195193
})
196194
}
197195
const testUtils = render(
198-
<AdminContext dataProvider={dataProvider}>
196+
<CoreAdminContext dataProvider={dataProvider}>
199197
<UserShow permissions="user" id="1" />
200-
</AdminContext>
198+
</CoreAdminContext>
201199
);
202200

203201
expect(testUtils.queryByDisplayValue('1')).not.toBeNull();
@@ -213,9 +211,9 @@ describe('UserShow', () => {
213211
})
214212
}
215213
const testUtils = render(
216-
<AdminContext dataProvider={dataProvider}>
214+
<CoreAdminContext dataProvider={dataProvider}>
217215
<UserShow permissions="user" id="1" />
218-
</AdminContext>
216+
</CoreAdminContext>
219217
);
220218

221219
fireEvent.click(testUtils.getByText('Security'));

0 commit comments

Comments
 (0)