diff --git a/frontend/cypress/e2e/pages/SearchPage.cy.ts b/frontend/cypress/e2e/pages/SearchPage.cy.ts index a715a9b1b5..a26afcfac1 100644 --- a/frontend/cypress/e2e/pages/SearchPage.cy.ts +++ b/frontend/cypress/e2e/pages/SearchPage.cy.ts @@ -343,6 +343,31 @@ describe("Search Page", () => { }); }); + describe("when user fills in the search box with extra spaces", () => { + beforeEach(() => { + cy.fillFormEntry("#search-box", " hello world ", { skipBlur: true }); + }); + + it("trims the entered string before making the API call", () => { + cy.wait("@predictiveSearch").then((interception) => { + expect(interception.request.query.keyword).to.eq("hello world"); + }); + }); + + describe("and clicks the Search button", () => { + beforeEach(() => { + cy.wait("@predictiveSearch"); + cy.get("#search-button").click(); + }); + it("trims the entered string before making the API call", () => { + cy.wait("@fullSearch").then((interception) => { + const { query } = interception.request; + expect(query.keyword).to.eq("hello world"); + }); + }); + }); + }); + describe("Search with no keywords", () => { beforeEach(() => { cy.get("#search-button").click(); diff --git a/frontend/src/pages/SearchPage.vue b/frontend/src/pages/SearchPage.vue index 07a877b0a1..02fc8fde4c 100644 --- a/frontend/src/pages/SearchPage.vue +++ b/frontend/src/pages/SearchPage.vue @@ -43,7 +43,9 @@ const pageNumber = ref(1); const totalItems = ref(0); const pageSize = ref(10); -const searchKeyword = ref(""); +const rawSearchKeyword = ref(""); + +const searchKeyword = computed(() => rawSearchKeyword.value.trim().replaceAll(/ +/g, " ")); const lastSearchKeyword = ref(""); // empty is valid @@ -219,7 +221,7 @@ onMounted(() => { tip="" placeholder="Search by client number, name or acronym" size="lg" - v-model="searchKeyword" + v-model="rawSearchKeyword" :contents="searchResultToCodeNameValueList(content)" :validations="validations" :validations-on-change="validationsOnChange" diff --git a/frontend/stub/mappings/client_search.json b/frontend/stub/mappings/client_search.json index c6986cb7f8..2393861100 100644 --- a/frontend/stub/mappings/client_search.json +++ b/frontend/stub/mappings/client_search.json @@ -49,7 +49,7 @@ "method": "GET", "queryParameters": { "keyword": { - "matches": "empty" + "matches": ".*empty.*" } } }, @@ -96,7 +96,7 @@ "method": "GET", "queryParameters": { "keyword": { - "matches": "empty" + "matches": ".*empty.*" }, "page": { "matches": "^[0-5]$"