diff --git a/cypress/components/select/filterable-select/filterable-select.cy.tsx b/cypress/components/select/filterable-select/filterable-select.cy.tsx index 3b0c855ee7..77901b2ac4 100644 --- a/cypress/components/select/filterable-select/filterable-select.cy.tsx +++ b/cypress/components/select/filterable-select/filterable-select.cy.tsx @@ -1018,6 +1018,7 @@ context("Tests for FilterableSelect component", () => { selectOption(positionOfElement(position)).click(); cy.get("@onChange").should("have.been.calledWith", { target: { value: option }, + selectionConfirmed: true, }); }); @@ -1147,6 +1148,93 @@ context("Tests for FilterableSelect component", () => { }); }); + describe("selection confirmed", () => { + it("is set on the event when options are clicked", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectListText("One").click(); + cy.get('[data-element="confirmed-selection-1"]').should("exist"); + dropdownButton().click(); + selectListText("Five").click(); + cy.get('[data-element="confirmed-selection-1"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + dropdownButton().click(); + selectListText("Seven").click(); + cy.get('[data-element="confirmed-selection-5"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-7"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option using ArrowDown key to navigate", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-1"]').should("exist"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-1"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-3"]').should("exist"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-3"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-5"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-6"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option using ArrowUp key to navigate", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-9"]').should("exist"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-9"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-7"]').should("exist"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-7"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-5"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-4"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option after filtering", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + commonDataElementInputPreview().click().type("th"); + cy.get('[data-element="confirmed-selection-3"]').should("not.exist"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-3"]').should("exist"); + }); + }); + + it("should not throw when filter text does not match option text", () => { + CypressMountWithProviders( + + ); + + commonDataElementInputPreview().type("abc"); + selectInput().realPress("Enter"); + getDataElementByValue("input").should("have.attr", "value", ""); + }); + describe("Accessibility tests for FilterableSelect component", () => { it("should pass accessibilty tests for FilterableSelect", () => { CypressMountWithProviders(); diff --git a/cypress/components/select/multi-select/multi-select.cy.tsx b/cypress/components/select/multi-select/multi-select.cy.tsx index f489129f92..149152d81c 100644 --- a/cypress/components/select/multi-select/multi-select.cy.tsx +++ b/cypress/components/select/multi-select/multi-select.cy.tsx @@ -915,6 +915,7 @@ context("Tests for MultiSelect component", () => { selectOption(positionOfElement(position)).click(); cy.get("@onChange").should("have.been.calledWith", { target: { value: option }, + selectionConfirmed: true, }); }); @@ -1099,6 +1100,141 @@ context("Tests for MultiSelect component", () => { }); }); + describe("selection confirmed", () => { + it("is set on the event when options are clicked", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectListText("One").click(); + selectListText("Five").click(); + selectListText("Seven").click(); + + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 3); + cy.get('[data-element="confirmed-selection-1"]').should("exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + cy.get('[data-element="confirmed-selection-7"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option using ArrowDown key to navigate", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 4); + cy.get('[data-element="confirmed-selection-1"]').should("exist"); + cy.get('[data-element="confirmed-selection-3"]').should("exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + cy.get('[data-element="confirmed-selection-6"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option using ArrowUp key to navigate", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 4); + cy.get('[data-element="confirmed-selection-9"]').should("exist"); + cy.get('[data-element="confirmed-selection-7"]').should("exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + cy.get('[data-element="confirmed-selection-4"]').should("exist"); + }); + + it("is set on the event when the selected options are removed via Backspace key", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 4); + + selectInput().realPress("Backspace"); + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 3); + selectInput().realPress("Backspace"); + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 2); + selectInput().realPress("Backspace"); + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 1); + selectInput().realPress("Backspace"); + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 0); + }); + + it("is set on the event when the selected options are removed via clicking close icon of Pills", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectListText("One").click(); + selectListText("Five").click(); + selectListText("Seven").click(); + + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 3); + pillCloseIcon().eq(2).click(); + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 2); + pillCloseIcon().eq(1).click(); + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 1); + pillCloseIcon().eq(0).click(); + cy.get('[data-element="confirmed-selections"]') + .children() + .should("have.length", 0); + }); + }); + + it("should not add an empty Pill when filter text does not match option text", () => { + CypressMountWithProviders(); + + multiSelectPill().should("not.exist"); + commonDataElementInputPreview().type("abc"); + selectInput().realPress("Enter"); + multiSelectPill().should("not.exist"); + }); + describe("Accessibility tests for MultiSelect component", () => { it("should pass accessibilty tests for MultiSelect", () => { CypressMountWithProviders(); diff --git a/cypress/components/select/simple-select/simple-select.cy.tsx b/cypress/components/select/simple-select/simple-select.cy.tsx index 7a96cd582f..2222149372 100644 --- a/cypress/components/select/simple-select/simple-select.cy.tsx +++ b/cypress/components/select/simple-select/simple-select.cy.tsx @@ -1007,6 +1007,80 @@ context("Tests for SimpleSelect component", () => { }); }); + describe("selection confirmed", () => { + it("is set on the event when options are clicked", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectListText("One").click(); + cy.get('[data-element="confirmed-selection-1"]').should("exist"); + dropdownButton().click(); + selectListText("Five").click(); + cy.get('[data-element="confirmed-selection-1"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + dropdownButton().click(); + selectListText("Seven").click(); + cy.get('[data-element="confirmed-selection-5"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-7"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option using ArrowDown key to navigate", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-1"]').should("exist"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-1"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-3"]').should("exist"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-3"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + selectInput().realPress("ArrowDown"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-5"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-6"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option using ArrowUp key to navigate", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-9"]').should("exist"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-9"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-7"]').should("exist"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-7"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-5"]').should("exist"); + selectInput().realPress("ArrowUp"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-5"]').should("not.exist"); + cy.get('[data-element="confirmed-selection-4"]').should("exist"); + }); + + it("is set on the event when Enter key is pressed on an option after using alpha key", () => { + CypressMountWithProviders(); + + dropdownButton().click(); + selectInput().type("t", { force: true }); + cy.get('[data-element="confirmed-selection-2"]').should("not.exist"); + selectInput().realPress("Enter"); + cy.get('[data-element="confirmed-selection-2"]').should("exist"); + }); + }); + describe("Accessibility tests for SimpleSelect component", () => { it("should pass accessibility tests for SimpleSelect", () => { CypressMountWithProviders(); diff --git a/src/components/select/filterable-select/filterable-select-test.stories.tsx b/src/components/select/filterable-select/filterable-select-test.stories.tsx index 4953773c01..e5aace0979 100644 --- a/src/components/select/filterable-select/filterable-select-test.stories.tsx +++ b/src/components/select/filterable-select/filterable-select-test.stories.tsx @@ -1,6 +1,11 @@ -import React, { useState } from "react"; +import React, { useState, useRef } from "react"; import partialAction from "../../../../.storybook/utils/partial-action"; -import { FilterableSelect, Option, FilterableSelectProps } from ".."; +import { + FilterableSelect, + Option, + FilterableSelectProps, + CustomSelectChangeEvent, +} from ".."; import OptionRow from "../option-row/option-row.component"; import Dialog from "../../dialog"; import Button from "../../button"; @@ -8,6 +13,7 @@ import Button from "../../button"; export default { component: FilterableSelect, title: "Select/Filterable/Test", + excludeStories: ["SelectionConfirmed"], parameters: { info: { disable: true }, chromatic: { @@ -49,7 +55,7 @@ DefaultStory.storyName = "default"; export const FilterableSelectComponent = ( props: Partial ) => { - const [value, setValue] = React.useState(""); + const [value, setValue] = useState(""); function onChangeHandler(event: React.ChangeEvent) { setValue(event.target.value); @@ -87,9 +93,9 @@ export const FilterableSelectComponent = ( export const FilterableSelectWithLazyLoadingComponent = ( props: Partial ) => { - const preventLoading = React.useRef(false); - const [value, setValue] = React.useState("black"); - const [isLoading, setIsLoading] = React.useState(true); + const preventLoading = useRef(false); + const [value, setValue] = useState("black"); + const [isLoading, setIsLoading] = useState(true); const asyncList = [