Skip to content

Commit

Permalink
[CSL-3108] Trim space adjustments (#183)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sblaurock authored Dec 20, 2023
1 parent 98a4de0 commit 90d178f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
16 changes: 16 additions & 0 deletions spec/src/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions spec/src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
3 changes: 2 additions & 1 deletion src/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

/**
Expand Down

0 comments on commit 90d178f

Please sign in to comment.