Skip to content

Commit

Permalink
Merge pull request #9894 from marmelab/fix-delete-record-even-if-id-i…
Browse files Browse the repository at this point in the history
…s-zero

Fix `useDelete` doesn't delete record if its id is zero
  • Loading branch information
fzaninotto authored Jun 3, 2024
2 parents 3512aff + be0c40b commit 084893c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/ra-core/src/dataProvider/useDelete.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,31 @@ describe('useDelete', () => {
});
});

it('should delete record even if id is zero', async () => {
const dataProvider = testDataProvider({
delete: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
let localDeleteOne;
const Dummy = () => {
const [deleteOne] = useDelete();
localDeleteOne = deleteOne;
return <span />;
};

render(
<CoreAdminContext dataProvider={dataProvider}>
<Dummy />
</CoreAdminContext>
);
localDeleteOne('foo', { id: 0, previousData: { id: 0, bar: 'bar' } });
await waitFor(() => {
expect(dataProvider.delete).toHaveBeenCalledWith('foo', {
id: 0,
previousData: { id: 0, bar: 'bar' },
});
});
});

describe('mutationOptions', () => {
it('when pessimistic, executes success side effects on success', async () => {
const onSuccess = jest.fn();
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/dataProvider/useDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const useDelete = <
'useDelete mutation requires a non-empty resource'
);
}
if (!callTimeId) {
if (callTimeId == null) {
throw new Error('useDelete mutation requires a non-empty id');
}
return dataProvider
Expand Down

0 comments on commit 084893c

Please sign in to comment.