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

fix(fe:FSADT1-1521): clear the AutoComplete field when preventSelection is true #1230

Merged
merged 2 commits into from
Oct 10, 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
13 changes: 4 additions & 9 deletions frontend/cypress/e2e/pages/SearchPage.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ describe("Search Page", () => {
});

it("displays autocomplete results", () => {
cy.get("#search-box")
.find("cds-combo-box-item")
.should("have.length", 3)
.should("be.visible");

cy.wait("@predictiveSearch").then((interception) => {
const data = interception.response.body;

cy.wrap(data).should("be.an", "array").and("have.length", 3);
cy.wrap(data).should("be.an", "array").and("have.length.greaterThan", 0);

cy.get("#search-box")
.find("cds-combo-box-item")
Expand Down Expand Up @@ -89,7 +84,7 @@ describe("Search Page", () => {
cy.wait("@predictiveSearch").then((interception) => {
const data = interception.response.body;

cy.wrap(data).should("be.an", "array").and("have.length.greaterThan", 3);
cy.wrap(data).should("be.an", "array").and("have.length.greaterThan", 0);

cy.get("#search-box")
.find("cds-combo-box-item")
Expand All @@ -102,11 +97,11 @@ describe("Search Page", () => {
});

describe("and user clicks a result", () => {
const clientNumber = "00001297";
const clientNumber = "00054076";
beforeEach(() => {
cy.get("#search-box")
.find("cds-combo-box-item")
.should("have.length", 3)
.should("have.length.greaterThan", 0)
.should("be.visible");

cy.get("#search-box").find(`cds-combo-box-item[data-value^="${clientNumber}"]`).click();
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/components/forms/AutoCompleteInputComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ const selectAutocompleteItem = (event: any) => {
};

const preSelectAutocompleteItem = (event: any) => {
const newValue = event?.detail?.item?.getAttribute("data-id");
emit("click:option", newValue);
if (props.preventSelection) {
event?.preventDefault();
if (event?.detail?.item) {
const newValue = event?.detail?.item?.getAttribute("data-id");
emit("click:option", newValue);
if (props.preventSelection) {
event?.preventDefault();
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Auto Complete Input Component", () => {
{ code: "TC", name: "TAMCADA" },
{ code: "TD", name: "TADANARA" },
];

const eventSelectContent = (value: string) => {
return {
detail: {
Expand All @@ -21,6 +22,14 @@ describe("Auto Complete Input Component", () => {
};
};

const eventClearContent = () => {
return {
detail: {
item: undefined,
},
};
};

const setInputValue = async (inputWrapper: DOMWrapper<HTMLInputElement>, value: string) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down Expand Up @@ -309,29 +318,46 @@ describe("Auto Complete Input Component", () => {
expect(wrapper.emitted("update:selected-value")![0][0]).toEqual(selectedContent);
});

it('emits the "click:option" event with the code of the clicked option and prevents selecting it', async () => {
const wrapper = mount(AutoCompleteInputComponent, {
props: {
id,
modelValue: "",
contents,
validations: [],
label: id,
tip: "",
preventSelection: true,
},
describe("when preventSelection is true", () => {
let wrapper: VueWrapper;
beforeEach(() => {
wrapper = mount(AutoCompleteInputComponent, {
props: {
id,
modelValue: "",
contents,
validations: [],
label: id,
tip: "",
preventSelection: true,
},
});
});

await wrapper.setProps({ modelValue: "T" });
await wrapper.find(`#${id}`).trigger("input");
it('emits the "click:option" event with the code of the clicked option and prevents selecting it', async () => {
await wrapper.setProps({ modelValue: "T" });
await wrapper.find(`#${id}`).trigger("input");

const code = "TB";
await wrapper.find(`#${id}`).trigger("cds-combo-box-beingselected", eventSelectContent(code));
const code = "TB";
await wrapper.find(`#${id}`).trigger("cds-combo-box-beingselected", eventSelectContent(code));

expect(wrapper.emitted("click:option")).toBeTruthy();
expect(wrapper.emitted("click:option")![0][0]).toEqual(code);
expect(wrapper.emitted("click:option")).toBeTruthy();
expect(wrapper.emitted("click:option")![0][0]).toEqual(code);

expect(wrapper.emitted("update:selected-value")).toBeFalsy();
});

expect(wrapper.emitted("update:selected-value")).toBeFalsy();
it("doesn't prevent the clearing action", async () => {
await wrapper.setProps({ modelValue: "T" });
await wrapper.find(`#${id}`).trigger("input");

// User initiated event to clear the value
await wrapper.find(`#${id}`).trigger("cds-combo-box-beingselected", eventClearContent());

// Clears the value
expect(wrapper.emitted("update:selected-value")).toBeTruthy();
expect(wrapper.emitted("update:selected-value")![0][0]).toEqual(undefined);
});
});

it('emits the "update:selected-value" event when an option from the list is clicked', async () => {
Expand Down
Loading