Skip to content

Commit c33c6f6

Browse files
committed
story and test
1 parent 7dc6c23 commit c33c6f6

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

packages/ra-core/src/dataProvider/useUpdate.spec.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import {
3131
WithMiddlewaresSuccess as WithMiddlewaresSuccessUndoable,
3232
WithMiddlewaresError as WithMiddlewaresErrorUndoable,
3333
} from './useUpdate.undoable.stories';
34-
import { Middleware, MutationMode, Params } from './useUpdate.stories';
34+
import {
35+
Middleware,
36+
MutationMode,
37+
Params,
38+
InvalidateList,
39+
} from './useUpdate.stories';
3540

3641
describe('useUpdate', () => {
3742
describe('mutate', () => {
@@ -653,6 +658,18 @@ describe('useUpdate', () => {
653658
});
654659
});
655660
});
661+
662+
it('invalidates getList query dataProvider resolves in undoable mode', async () => {
663+
render(<InvalidateList mutationMode="undoable" />);
664+
fireEvent.change(await screen.findByDisplayValue('Hello'), {
665+
target: { value: 'Hello changed' },
666+
});
667+
fireEvent.click(screen.getByText('Save'));
668+
await screen.findByText('resources.posts.notifications.updated');
669+
fireEvent.click(screen.getByText('Close'));
670+
await screen.findByText(/Hello changed/);
671+
});
672+
656673
describe('pessimistic mutation mode', () => {
657674
it('updates getOne query cache when dataProvider promise resolves', async () => {
658675
const queryClient = new QueryClient();

packages/ra-core/src/dataProvider/useUpdate.stories.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { useState } from 'react';
33
import { QueryClient, useIsMutating } from '@tanstack/react-query';
4+
import fakeRestDataProvider from 'ra-data-fakerest';
45

56
import { CoreAdmin, CoreAdminContext, Resource } from '../core';
67
import { useUpdate } from './useUpdate';
@@ -333,3 +334,60 @@ const TextInput = (props: InputProps) => {
333334
</div>
334335
);
335336
};
337+
338+
export const InvalidateList = ({
339+
mutationMode = 'undoable',
340+
}: {
341+
mutationMode?: MutationModeType;
342+
}) => {
343+
const dataProvider = fakeRestDataProvider(
344+
{
345+
posts: [
346+
{ id: 1, title: 'Hello' },
347+
{ id: 2, title: 'World' },
348+
],
349+
},
350+
process.env.NODE_ENV !== 'test',
351+
process.env.NODE_ENV === 'test' ? 10 : 1000
352+
);
353+
return (
354+
<TestMemoryRouter initialEntries={['/posts/1']}>
355+
<CoreAdmin dataProvider={dataProvider}>
356+
<Resource
357+
name="posts"
358+
list={
359+
<ListBase loading={<p>Loading...</p>}>
360+
<RecordsIterator
361+
render={record => (
362+
<div>
363+
{record.id}: {record.title}
364+
</div>
365+
)}
366+
/>
367+
<Notification />
368+
</ListBase>
369+
}
370+
edit={
371+
<EditBase mutationMode={mutationMode}>
372+
<Form>
373+
<TextInput source="title" />
374+
<button type="submit">Save</button>
375+
</Form>
376+
</EditBase>
377+
}
378+
/>
379+
</CoreAdmin>
380+
</TestMemoryRouter>
381+
);
382+
};
383+
InvalidateList.args = {
384+
mutationMode: 'undoable',
385+
};
386+
InvalidateList.argTypes = {
387+
mutationMode: {
388+
control: {
389+
type: 'select',
390+
},
391+
options: ['pessimistic', 'optimistic', 'undoable'],
392+
},
393+
};

0 commit comments

Comments
 (0)