Skip to content

Commit

Permalink
Fix useUpdate throws an error when record id is a valid falsy value s…
Browse files Browse the repository at this point in the history
…uch as zero
  • Loading branch information
djhi committed Jun 26, 2024
1 parent 05da7f5 commit f93228f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions packages/ra-core/src/dataProvider/useUpdate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ describe('useUpdate', () => {
});
});

it('accepts falsy value that are not null nor undefined as the record id', async () => {
const dataProvider = {
update: jest.fn(() =>
Promise.resolve({ data: { id: 1 } } as any)
),
} as any;
let localUpdate;
const Dummy = () => {
const [update] = useUpdate('foo', {
id: 0,
data: { bar: 'baz' },
previousData: { id: 0, bar: 'bar' },
});
localUpdate = update;
return <span />;
};

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

it('replaces hook call time params by and callback time params', async () => {
const dataProvider = {
update: jest.fn(() =>
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/dataProvider/useUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export const useUpdate = <RecordType extends RaRecord = any, ErrorType = Error>(
'useUpdate mutation requires a non-empty resource'
);
}
if (!callTimeId) {
if (callTimeId == null) {
throw new Error('useUpdate mutation requires a non-empty id');
}
if (!callTimeData) {
Expand Down

0 comments on commit f93228f

Please sign in to comment.