Skip to content

Commit

Permalink
Extend test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rall3n committed Mar 3, 2022
1 parent 7a768e9 commit 0379b44
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
52 changes: 37 additions & 15 deletions packages/react-select/src/__tests__/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3120,20 +3120,42 @@ test('renders with custom theme', () => {
).toEqual(primary);
});

test('form validation with `required` prop', () => {
const components = (value?: Option) => (
<form id="formTest">
<Select {...BASIC_PROPS} required value={value || null} />
</form>
);
cases(
'`required` prop',
({ props = BASIC_PROPS }) => {
const components = (value: Option | null | undefined = null) => (
<form id="formTest">
<Select {...props} required value={value} />
</form>
);

const { container, rerender } = render(components());
const { container, rerender } = render(components());

expect(
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
).toEqual(false);
rerender(components(BASIC_PROPS.options[0]));
expect(
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
).toEqual(true);
});
expect(
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
).toEqual(false);
rerender(components(props.options[0]));
expect(
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
).toEqual(true);
},
{
'single select > should validate with value': {
props: {
...BASIC_PROPS,
},
},
'single select (isSearchable is false) > should validate with value': {
props: {
...BASIC_PROPS,
isSearchable: false,
},
},
'multi select > should validate with value': {
props: {
...BASIC_PROPS,
isMulti: true,
},
},
}
);
20 changes: 20 additions & 0 deletions packages/react-select/src/__tests__/StateManaged.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,23 @@ cases<KeyboardInteractionOpts>(
},
}
);

test('`required` prop > should validate', () => {
const { container } = render(
<form id="formTest">
<Select {...BASIC_PROPS} menuIsOpen required />
</form>
);

expect(
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
).toEqual(false);

let selectOption = container.querySelectorAll('div.react-select__option')[3];

userEvent.click(selectOption);

expect(
container.querySelector<HTMLFormElement>('#formTest')?.checkValidity()
).toEqual(true);
});

0 comments on commit 0379b44

Please sign in to comment.