Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/ra-core/src/dataProvider/useCreate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
describe('useCreate', () => {
it('returns a callback that can be used with create arguments', async () => {
const dataProvider = testDataProvider({
create: jest.fn(() => Promise.resolve({ data: { id: 1 } } as any)),
create: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
let localCreate;
const Dummy = () => {
Expand All @@ -60,7 +60,7 @@ describe('useCreate', () => {

it('returns a callback that can be used with no arguments', async () => {
const dataProvider = testDataProvider({
create: jest.fn(() => Promise.resolve({ data: { id: 1 } } as any)),
create: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
let localCreate;
const Dummy = () => {
Expand All @@ -84,7 +84,7 @@ describe('useCreate', () => {

it('uses a custom mutationFn with mutation middlewares', async () => {
const dataProvider = testDataProvider({
create: jest.fn(() => Promise.resolve({ data: { id: 1 } } as any)),
create: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
const customMutationFn = jest.fn(async params => ({
id: 1,
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('useCreate', () => {

it('uses call time params over hook time params', async () => {
const dataProvider = testDataProvider({
create: jest.fn(() => Promise.resolve({ data: { id: 1 } } as any)),
create: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
let localCreate;
const Dummy = () => {
Expand All @@ -203,7 +203,7 @@ describe('useCreate', () => {

it('calls onSettled when provided in hook time options', async () => {
const dataProvider = testDataProvider({
create: jest.fn(() => Promise.resolve({ data: { id: 1 } } as any)),
create: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
let localCreate;
const onSettled = jest.fn();
Expand All @@ -230,7 +230,7 @@ describe('useCreate', () => {

it('accepts a meta parameter', async () => {
const dataProvider = testDataProvider({
create: jest.fn(() => Promise.resolve({ data: { id: 1 } } as any)),
create: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
let localCreate;
const Dummy = () => {
Expand Down Expand Up @@ -260,7 +260,7 @@ describe('useCreate', () => {
});

const dataProvider = testDataProvider({
create: jest.fn(() => Promise.resolve({ data: { id: 1 } } as any)),
create: jest.fn(() => Promise.resolve({ data: { id: 0 } } as any)),
});
let localCreate;
const Dummy = () => {
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('useCreate', () => {
}
const dataProvider = testDataProvider({
create: jest.fn(() =>
Promise.resolve({ data: { id: 1, sku: 'abc' } } as any)
Promise.resolve({ data: { id: 0, sku: 'abc' } } as any)
),
});
let localCreate;
Expand Down
64 changes: 64 additions & 0 deletions packages/ra-core/src/dataProvider/useCreate.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,67 @@ InvalidateList.argTypes = {
options: ['pessimistic', 'optimistic', 'undoable'],
},
};

export const CreateIdZero = ({
mutationMode,
}: {
mutationMode: MutationModeType;
}) => {
const dataProvider = fakeRestDataProvider(
{
posts: [],
},
process.env.NODE_ENV !== 'test',
process.env.NODE_ENV === 'test' ? 10 : 1000
);

return (
<TestMemoryRouter initialEntries={['/posts/create']}>
<CoreAdmin dataProvider={dataProvider}>
<Resource
name="posts"
create={
<CreateBase mutationMode={mutationMode}>
<Form>
{mutationMode !== 'pessimistic' && (
<TextInput source="id" defaultValue={0} />
)}
<TextInput source="title" />
<button type="submit">Save</button>
</Form>
</CreateBase>
}
list={
<ListBase loading={<p>Loading...</p>}>
<RecordsIterator
render={(record: any) => (
<div
style={{
display: 'flex',
gap: '8px',
alignItems: 'center',
}}
>
{record.id}: {record.title}
</div>
)}
/>
<Notification />
</ListBase>
}
/>
</CoreAdmin>
</TestMemoryRouter>
);
};
CreateIdZero.args = {
mutationMode: 'undoable',
};
CreateIdZero.argTypes = {
mutationMode: {
control: {
type: 'select',
},
options: ['pessimistic', 'optimistic', 'undoable'],
},
};
2 changes: 1 addition & 1 deletion packages/ra-core/src/dataProvider/useCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const useCreate = <
mutationMode === 'pessimistic'
? result?.id
: params.data?.id;
if (!id) {
if (id === undefined || id === null) {
throw new Error(
'Invalid dataProvider response for create: missing id'
);
Expand Down
Loading