Skip to content

Commit 43617bf

Browse files
committed
story and test for useDelete
1 parent 1cc1979 commit 43617bf

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
ErrorCase as ErrorCaseUndoable,
2121
SuccessCase as SuccessCaseUndoable,
2222
} from './useDelete.undoable.stories';
23-
import { MutationMode, Params } from './useDelete.stories';
23+
import { MutationMode, Params, InvalidateList } from './useDelete.stories';
2424

2525
describe('useDelete', () => {
2626
it('returns a callback that can be used with deleteOne arguments', async () => {
@@ -732,5 +732,16 @@ describe('useDelete', () => {
732732
});
733733
});
734734
});
735+
736+
it('invalidates getList query when dataProvider resolves in undoable mode', async () => {
737+
render(<InvalidateList mutationMode="undoable" />);
738+
await screen.findByText('Title: Hello');
739+
fireEvent.click(await screen.findByText('Delete'));
740+
await screen.findByText('resources.posts.notifications.deleted');
741+
fireEvent.click(screen.getByText('Close'));
742+
await waitFor(() => {
743+
expect(screen.queryByText('1: Hello')).toBeNull();
744+
});
745+
});
735746
});
736747
});

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

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
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

5-
import { CoreAdminContext } from '../core';
6+
import { CoreAdmin, CoreAdminContext, Resource } from '../core';
67
import { useDelete } from './useDelete';
78
import { useGetList } from './useGetList';
89
import type { DataProvider, MutationMode as MutationModeType } from '../types';
10+
import {
11+
EditBase,
12+
ListBase,
13+
RecordsIterator,
14+
useDeleteController,
15+
WithRecord,
16+
} from '../controller';
17+
import { TestMemoryRouter } from '../routing';
18+
import { useNotificationContext } from '../notification';
19+
import { useTakeUndoableMutation } from './undo';
920

1021
export default { title: 'ra-core/dataProvider/useDelete' };
1122

@@ -162,3 +173,110 @@ const ParamsCore = () => {
162173
</>
163174
);
164175
};
176+
177+
const Notification = () => {
178+
const { notifications, resetNotifications } = useNotificationContext();
179+
const takeMutation = useTakeUndoableMutation();
180+
181+
return notifications.length > 0 ? (
182+
<>
183+
<div>{notifications[0].message}</div>
184+
<div style={{ display: 'flex', gap: '16px' }}>
185+
<button
186+
onClick={() => {
187+
if (notifications[0].notificationOptions?.undoable) {
188+
const mutation = takeMutation();
189+
if (mutation) {
190+
mutation({ isUndo: false });
191+
}
192+
}
193+
resetNotifications();
194+
}}
195+
>
196+
Close
197+
</button>
198+
</div>
199+
</>
200+
) : null;
201+
};
202+
203+
const DeleteButton = ({ mutationMode }: { mutationMode: MutationModeType }) => {
204+
const { isPending, handleDelete } = useDeleteController({
205+
mutationMode,
206+
redirect: 'list',
207+
});
208+
return (
209+
<button onClick={handleDelete} disabled={isPending}>
210+
Delete
211+
</button>
212+
);
213+
};
214+
215+
export const InvalidateList = ({
216+
mutationMode,
217+
}: {
218+
mutationMode: MutationModeType;
219+
}) => {
220+
const dataProvider = fakeRestDataProvider(
221+
{
222+
posts: [
223+
{ id: 1, title: 'Hello' },
224+
{ id: 2, title: 'World' },
225+
],
226+
},
227+
process.env.NODE_ENV !== 'test',
228+
process.env.NODE_ENV === 'test' ? 10 : 1000
229+
);
230+
231+
return (
232+
<TestMemoryRouter initialEntries={['/posts/1']}>
233+
<CoreAdmin dataProvider={dataProvider}>
234+
<Resource
235+
name="posts"
236+
edit={
237+
<EditBase>
238+
<div>
239+
<h1>Edit Post</h1>
240+
<WithRecord
241+
render={record => (
242+
<div>Title: {record.title}</div>
243+
)}
244+
/>
245+
<DeleteButton mutationMode={mutationMode} />
246+
</div>
247+
</EditBase>
248+
}
249+
list={
250+
<ListBase loading={<p>Loading...</p>}>
251+
<RecordsIterator
252+
render={(record: any) => (
253+
<div
254+
style={{
255+
display: 'flex',
256+
gap: '8px',
257+
alignItems: 'center',
258+
}}
259+
>
260+
{record.id}: {record.title}
261+
</div>
262+
)}
263+
/>
264+
<Notification />
265+
</ListBase>
266+
}
267+
/>
268+
</CoreAdmin>
269+
</TestMemoryRouter>
270+
);
271+
};
272+
InvalidateList.args = {
273+
mutationMode: 'undoable',
274+
};
275+
InvalidateList.argTypes = {
276+
mutationMode: {
277+
control: {
278+
type: 'select',
279+
},
280+
options: ['pessimistic', 'optimistic', 'undoable'],
281+
},
282+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ describe('useUpdate', () => {
659659
});
660660
});
661661

662-
it('invalidates getList query dataProvider resolves in undoable mode', async () => {
662+
it('invalidates getList query when dataProvider resolves in undoable mode', async () => {
663663
render(<InvalidateList mutationMode="undoable" />);
664664
fireEvent.change(await screen.findByDisplayValue('Hello'), {
665665
target: { value: 'Hello changed' },

0 commit comments

Comments
 (0)