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

refactor(filterable-select): convert unit tests to RTL #7036

Merged
merged 1 commit into from
Oct 28, 2024
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 @@ -203,6 +203,7 @@ export const FilterableSelect = React.forwardRef<
newValue: string | Record<string, unknown>,
selectionConfirmed: boolean
) => {
/* istanbul ignore else */
if (onChange) {
onChange(createCustomEvent(newValue, selectionConfirmed));
}
Expand Down Expand Up @@ -280,7 +281,12 @@ export const FilterableSelect = React.forwardRef<

if (!matchingOption || matchingOption.props.text === undefined) {
setTextValue(filterText || "");
} else if (

return;
}

/* istanbul ignore else */
if (
isClosing ||
matchingOption.props.text
?.toLowerCase()
Expand Down Expand Up @@ -432,7 +438,9 @@ export const FilterableSelect = React.forwardRef<
const onFilterChange = useStableCallback(
onFilterChangeProp as (filterTextArg: unknown) => void
);

const isFirstRender = useRef(true);

useEffect(() => {
if (onFilterChange && !isFirstRender.current) {
onFilterChange(filterText);
Expand Down Expand Up @@ -537,6 +545,14 @@ export const FilterableSelect = React.forwardRef<
});
}

useEffect(() => {
return () => {
if (focusTimer.current) {
clearTimeout(focusTimer.current);
}
};
}, []);

function handleTextboxFocus(event: React.FocusEvent<HTMLInputElement>) {
const triggerFocus = () => onFocus?.(event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,39 @@ test.describe("When focused", () => {
});

test.describe("FilterableSelect component", () => {
test("should not select an option when the user types non-matching filter text in the input and then presses the Enter key", async ({
page,
mount,
}) => {
await mount(<FilterableSelectComponent />);

await selectInput(page).fill("f");
await selectInput(page).press("Enter");
await expect(getDataElementByValue(page, "input")).toHaveValue("");
});

test("should not select an option when the user types non-matching filter text and then presses ArrowDown key", async ({
page,
mount,
}) => {
await mount(<FilterableSelectComponent />);

await selectInput(page).fill("f");
await selectInput(page).press("ArrowDown");
await expect(getDataElementByValue(page, "input")).toHaveValue("f");
});

test("should not select an option when the user types non-matching filter text and then presses ArrowUp key", async ({
page,
mount,
}) => {
await mount(<FilterableSelectComponent />);

await selectInput(page).fill("f");
await selectInput(page).press("ArrowUp");
await expect(getDataElementByValue(page, "input")).toHaveValue("f");
});

testData.forEach((labelValue) => {
test(`should render label using ${labelValue} special characters`, async ({
mount,
Expand Down
Loading
Loading