From 90d178fd9ec5a487f9c702128e5776fd29a2c43c Mon Sep 17 00:00:00 2001 From: Steve Blaurock Date: Wed, 20 Dec 2023 14:28:18 -0700 Subject: [PATCH] [CSL-3108] Trim space adjustments (#183) * Do not trim autocomplete queries, add test cases for expected behavior * Lint fixes, adjust implementation and add tests * Remove outdated test * Update test description for consistency * Do not trim spaces from search and autocomplete requests * Remove only from tests * Remove unused test --- spec/src/modules/autocomplete.js | 16 ++++++++++++++++ spec/src/modules/search.js | 16 ++++++++++++++++ src/modules/autocomplete.js | 3 ++- src/modules/search.js | 3 ++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/spec/src/modules/autocomplete.js b/spec/src/modules/autocomplete.js index d2b0c4de..aff7b15d 100644 --- a/spec/src/modules/autocomplete.js +++ b/spec/src/modules/autocomplete.js @@ -473,6 +473,22 @@ describe('ConstructorIO - Autocomplete', () => { }); }); + it('Should not trim spaces from query', (done) => { + const queryWithSpaces = ` ${query} `; + const { autocomplete } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + autocomplete.getAutocompleteResults(queryWithSpaces).then((res) => { + expect(res.request.term).to.equal(queryWithSpaces); + expect(res).to.have.property('request').to.be.an('object'); + expect(res).to.have.property('sections').to.be.an('object'); + expect(res).to.have.property('result_id').to.be.an('string'); + done(); + }); + }); + it('Should return a response with a / query', (done) => { const { autocomplete } = new ConstructorIO({ apiKey: testApiKey, diff --git a/spec/src/modules/search.js b/spec/src/modules/search.js index 3f797608..9226cb38 100644 --- a/spec/src/modules/search.js +++ b/spec/src/modules/search.js @@ -542,6 +542,22 @@ describe('ConstructorIO - Search', () => { }); }); + it('Should not trim spaces from query', (done) => { + const queryWithSpaces = ` ${query} `; + const { search } = new ConstructorIO({ + apiKey: testApiKey, + fetch: fetchSpy, + }); + + search.getSearchResults(queryWithSpaces).then((res) => { + expect(res.request.term).to.equal(queryWithSpaces); + expect(res).to.have.property('request').to.be.an('object'); + expect(res).to.have.property('response').to.be.an('object'); + expect(res).to.have.property('result_id').to.be.an('string'); + done(); + }); + }); + it('Should properly transform non-breaking spaces in parameters', (done) => { const breakingSpaces = ' '; const sortBy = `relevance ${breakingSpaces} relevance`; diff --git a/src/modules/autocomplete.js b/src/modules/autocomplete.js index 9b8f2808..c0c06771 100644 --- a/src/modules/autocomplete.js +++ b/src/modules/autocomplete.js @@ -112,7 +112,8 @@ function createAutocompleteUrl(query, parameters, userParameters, options) { const queryString = qs.stringify(queryParams, { indices: false }); const cleanedQuery = query.replace(/^\//, '|'); // For compatibility with backend API - return `${serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(cleanedQuery))}?${queryString}`; + // Note: it is intentional that query is dispatched without being trimmed + return `${serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(cleanedQuery)}?${queryString}`; } /** diff --git a/src/modules/search.js b/src/modules/search.js index 3fa829ed..9d94baef 100644 --- a/src/modules/search.js +++ b/src/modules/search.js @@ -145,7 +145,8 @@ function createSearchUrl(query, parameters, userParameters, options, isVoiceSear const searchUrl = isVoiceSearch ? 'search/natural_language' : 'search'; - return `${serviceUrl}/${searchUrl}/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(query))}?${queryString}`; + // Note: it is intentional that query is dispatched without being trimmed + return `${serviceUrl}/${searchUrl}/${helpers.encodeURIComponentRFC3986(query)}?${queryString}`; } /**