Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Dec 16, 2024
1 parent 003f7f9 commit 10b4633
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
40 changes: 38 additions & 2 deletions packages/ra-core/src/form/Form.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -809,18 +809,54 @@ describe('Form', () => {
expectedValue: 'from-search',
},
])(
'should support overriding the record values from the location $from',
'should support prefilling the from values from the location $from',
async ({ url, expectedValue }) => {
render(<MultiRoutesForm url={url} />);
await screen.findByDisplayValue('lorem');
expect(
(await screen.findByLabelText<HTMLInputElement>('title')).value
).toEqual('');
expect(
(screen.getByText('Submit') as HTMLInputElement).disabled
).toEqual(false);
fireEvent.click(screen.getByText('Settings'));
await screen.findByDisplayValue(expectedValue);
expect(
screen.getByText<HTMLInputElement>('Submit').disabled
).toEqual(false);
}
);
it.each([
{
from: 'state',
url: {
pathname: '/form/general',
state: { record: { body: 'from-state' } },
},
expectedValue: 'from-state',
},
{
from: 'search query',
url: `/form/general?source=${encodeURIComponent(JSON.stringify({ body: 'from-search' }))}` as To,
expectedValue: 'from-search',
},
])(
'should support overriding the record values from the location $from',
async ({ url, expectedValue }) => {
render(
<MultiRoutesForm
url={url}
initialRecord={{ title: 'lorem', body: 'unmodified' }}
/>
);
await screen.findByDisplayValue('lorem');
expect(
(screen.getByText('Submit') as HTMLInputElement).disabled
).toEqual(false);
fireEvent.click(screen.getByText('Settings'));
await screen.findByDisplayValue(expectedValue);
expect(
screen.getByText<HTMLInputElement>('Submit').disabled
).toEqual(false);
}
);
});
40 changes: 30 additions & 10 deletions packages/ra-core/src/form/Form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { Form } from './Form';
import { useInput } from './useInput';
import { required, ValidationError } from './validation';
import { mergeTranslations } from '../i18n';
import { I18nProvider } from '../types';
import { I18nProvider, RaRecord } from '../types';
import {
RecordContextProvider,
SaveContextProvider,
TestMemoryRouter,
useNotificationContext,
Expand Down Expand Up @@ -415,18 +416,32 @@ export const ServerSideValidation = () => {
);
};

export const MultiRoutesForm = ({ url }: { url?: any }) => (
export const MultiRoutesForm = ({
url,
initialRecord,
}: {
url?: any;
initialRecord?: Partial<RaRecord>;
}) => (
<TestMemoryRouter key={url} initialEntries={[url]}>
<CoreAdminContext i18nProvider={defaultI18nProvider}>
<Routes>
<Route path="/form/*" element={<FormWithSubRoutes />} />
<Route
path="/form/*"
element={
<RecordContextProvider value={initialRecord}>
<FormWithSubRoutes />
</RecordContextProvider>
}
/>
</Routes>
</CoreAdminContext>
</TestMemoryRouter>
);

MultiRoutesForm.args = {
url: 'unmodified',
initialRecord: 'none',
};

MultiRoutesForm.argTypes = {
Expand All @@ -446,17 +461,22 @@ MultiRoutesForm.argTypes = {
},
control: { type: 'select' },
},
initialRecord: {
options: ['none', 'provided'],
mapping: {
none: undefined,
provided: { title: 'lorem', body: 'unmodified' },
},
control: { type: 'select' },
},
};

const record = { title: 'lorem', body: 'unmodified' };
const FormWithSubRoutes = () => {
return (
<>
<Form record={record}>
<TabbedForm />
<SubmitButton />
</Form>
</>
<Form>
<TabbedForm />
<SubmitButton />
</Form>
);
};

Expand Down

0 comments on commit 10b4633

Please sign in to comment.