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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const mockedValidate = vi.fn();
const COMPONENT_NAME = "test-autoSuggestInput";
const PLACEHOLDER_MOCK = "Test Placeholder";

beforeEach(() => {
vi.mocked(useField).mockReturnValue({
function getMockUseFieldReturnValue() {
return {
getInputProps: vi.fn().mockReturnValue({
id: COMPONENT_NAME,
placeholder: PLACEHOLDER_MOCK,
Expand All @@ -39,7 +39,11 @@ beforeEach(() => {
clearError: vi.fn(),
reset: vi.fn(),
validate: mockedValidate,
});
};
}

beforeEach(() => {
vi.mocked(useField).mockReturnValue(getMockUseFieldReturnValue());

vi.spyOn(useDataListOptions, "default").mockReturnValue(
getDataListOptions("airports"),
Expand Down Expand Up @@ -269,4 +273,27 @@ describe("AutoSuggestInput", () => {
await user.tab();
expect(input).not.toHaveFocus();
});

it("adds aria-describedby to the input when there's an error", () => {
const mockField = getMockUseFieldReturnValue();
mockField.error.mockReturnValue("required");

vi.mocked(useField).mockReturnValue(mockField);
const { getByRole } = render(
<AutoSuggestInput
name={COMPONENT_NAME}
placeholder="placeholder"
dataList="airports"
errorMessages={[{ code: "required", text: "Field is required" }]}
isDisabled={false}
/>,
);

const input = getByRole("combobox");

expect(input).toHaveAttribute(
"aria-describedby",
`${COMPONENT_NAME}-error`,
);
});
});
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import { components, type InputProps } from "react-select";
import {
components,
type InputProps,
type Props,
type GroupBase,
} from "react-select";
import type { DataListOptions } from "~/services/dataListOptions/getDataListOptions";
import { INPUT_CHAR_LIMIT } from "~/services/validation/inputlimits";

const CustomInput = (props: InputProps<DataListOptions, false>) => (
<components.Input
// avoid to clear the previous auto suggestion input when press enter
// this cannot be tested, thus manually tested with device(s)
onKeyDown={(e) => {
if (e.key === "Enter" && !props.selectProps.menuIsOpen) {
e.preventDefault();
}
}}
{...props}
maxLength={INPUT_CHAR_LIMIT}
aria-required={props.selectProps.className?.includes(
"auto-suggest-input-required",
)}
/>
);
//Will become obsolete with the next version bump: https://github.com/JedWatson/react-select/pull/6016
type CustomSelectProps = Props<
Comment thread
aaschlote marked this conversation as resolved.
DataListOptions,
false,
GroupBase<DataListOptions>
> & {
"aria-describedby": string;
};

const CustomInput = (props: InputProps<DataListOptions, false>) => {
const selectProps = props.selectProps as CustomSelectProps;
return (
<components.Input
// avoid to clear the previous auto suggestion input when press enter
// this cannot be tested, thus manually tested with device(s)
onKeyDown={(e) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the testing library be used to test this behaviour?

if (e.key === "Enter" && !props.selectProps.menuIsOpen) {
e.preventDefault();
}
}}
{...props}
maxLength={INPUT_CHAR_LIMIT}
aria-describedby={selectProps["aria-describedby"]}
aria-required={props.selectProps.className?.includes(
"auto-suggest-input-required",
)}
/>
);
};
export default CustomInput;