Skip to content

Commit cbd173b

Browse files
committed
Extend test cases
1 parent 4af03f4 commit cbd173b

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

packages/react-select/src/__tests__/Select.test.tsx

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,20 +3120,42 @@ test('renders with custom theme', () => {
31203120
).toEqual(primary);
31213121
});
31223122

3123-
test('form validation with `required` prop', () => {
3124-
const components = (value?: Option) => (
3125-
<form id="formTest">
3126-
<Select {...BASIC_PROPS} required value={value || null} />
3127-
</form>
3128-
);
3123+
cases(
3124+
'`required` prop',
3125+
({ props = BASIC_PROPS }) => {
3126+
const components = (value: Option | null | undefined = null) => (
3127+
<form id="formTest">
3128+
<Select {...props} required value={value} />
3129+
</form>
3130+
);
31293131

3130-
const { container, rerender } = render(components());
3132+
const { container, rerender } = render(components());
31313133

3132-
expect(
3133-
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
3134-
).toEqual(false);
3135-
rerender(components(BASIC_PROPS.options[0]));
3136-
expect(
3137-
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
3138-
).toEqual(true);
3139-
});
3134+
expect(
3135+
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
3136+
).toEqual(false);
3137+
rerender(components(props.options[0]));
3138+
expect(
3139+
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
3140+
).toEqual(true);
3141+
},
3142+
{
3143+
'single select > should validate with value': {
3144+
props: {
3145+
...BASIC_PROPS,
3146+
},
3147+
},
3148+
'single select (isSearchable is false) > should validate with value': {
3149+
props: {
3150+
...BASIC_PROPS,
3151+
isSearchable: false,
3152+
},
3153+
},
3154+
'multi select > should validate with value': {
3155+
props: {
3156+
...BASIC_PROPS,
3157+
isMulti: true,
3158+
},
3159+
},
3160+
}
3161+
);

packages/react-select/src/__tests__/StateManaged.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,23 @@ cases<KeyboardInteractionOpts>(
475475
},
476476
}
477477
);
478+
479+
test('`required` prop > should validate', () => {
480+
const { container } = render(
481+
<form id="formTest">
482+
<Select {...BASIC_PROPS} menuIsOpen required />
483+
</form>
484+
);
485+
486+
expect(
487+
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
488+
).toEqual(false);
489+
490+
let selectOption = container.querySelectorAll('div.react-select__option')[3];
491+
492+
userEvent.click(selectOption);
493+
494+
expect(
495+
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
496+
).toEqual(true);
497+
});

0 commit comments

Comments
 (0)