Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prevent CheckboxGroupInput from changing selected values type #7248

Merged
merged 6 commits into from
Feb 16, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
WiXSL committed Feb 16, 2022
commit 3f6803856419786799728c3e40fbdcb3dc92a8cc
60 changes: 60 additions & 0 deletions packages/ra-ui-materialui/src/input/CheckboxGroupInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -217,6 +217,66 @@ describe('<CheckboxGroupInput />', () => {
expect(queryByText('Can I help you?')).not.toBeNull();
});

it('should not parse selected values types to numbers if all values types are non numbers', () => {
const handleSubmit = jest.fn();
const { getByLabelText } = render(
<Form
onSubmit={handleSubmit}
initialValues={{ notifications: ['31', '42'] }}
render={({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<CheckboxGroupInput
source="notifications"
choices={[
{ id: 12, name: 'Ray Hakt' },
{ id: 31, name: 'Ann Gullar' },
{ id: 42, name: 'Sean Phonee' },
]}
/>
<button type="submit" aria-label="Save" />
</form>
)}
/>
);
const input = getByLabelText('Ray Hakt') as HTMLInputElement;
fireEvent.click(input);

fireEvent.click(getByLabelText('Save'));
expect(handleSubmit.mock.calls[0][0]).toEqual({
notifications: ['31', '42', '12'],
});
});

it('should parse selected values types to numbers if some are numbers', () => {
const handleSubmit = jest.fn();
const { getByLabelText } = render(
<Form
onSubmit={handleSubmit}
initialValues={{ notifications: [31, 42] }}
render={({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<CheckboxGroupInput
source="notifications"
choices={[
{ id: 12, name: 'Ray Hakt' },
{ id: 31, name: 'Ann Gullar' },
{ id: 42, name: 'Sean Phonee' },
]}
/>
<button type="submit" aria-label="Save" />
</form>
)}
/>
);
const input = getByLabelText('Ray Hakt') as HTMLInputElement;
fireEvent.click(input);

fireEvent.click(getByLabelText('Save'));
expect(handleSubmit.mock.calls[0][0]).toEqual({
notifications: [31, 42, 12],
});
});

describe('error message', () => {
it('should not be displayed if field is pristine', () => {
const { container } = render(