Skip to content

Commit

Permalink
Lint fixes, adjust implementation and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sblaurock committed Dec 19, 2023
1 parent 9b78a88 commit f25457a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
10 changes: 6 additions & 4 deletions spec/src/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,6 @@ describe('ConstructorIO - Autocomplete', () => {
});

autocomplete.getAutocompleteResults(' ').then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

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');
Expand Down Expand Up @@ -497,8 +495,6 @@ describe('ConstructorIO - Autocomplete', () => {
});

autocomplete.getAutocompleteResults(queryWithSpaces).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

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');
Expand Down Expand Up @@ -627,6 +623,12 @@ describe('ConstructorIO - Autocomplete', () => {
return expect(autocomplete.getAutocompleteResults(null)).to.eventually.be.rejected;
});

it('Should be rejected when query consisting of only non-breaking spaces is provided', () => {
const { autocomplete } = new ConstructorIO(validOptions);

return expect(autocomplete.getAutocompleteResults(' ')).to.eventually.be.rejected;
});

it('Should be rejected when invalid numResults parameter is provided', () => {
const { autocomplete } = new ConstructorIO(validOptions);

Expand Down
2 changes: 0 additions & 2 deletions spec/src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,6 @@ describe('ConstructorIO - Search', () => {
});

search.getSearchResults(queryWithSpaces).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res.request.term).to.equal(utilsHelpers.trimNonBreakingSpaces(queryWithSpaces));
expect(res).to.have.property('request').to.be.an('object');
expect(res).to.have.property('response').to.be.an('object');
Expand Down
8 changes: 6 additions & 2 deletions src/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ function createAutocompleteUrl(query, parameters, userParameters, options) {
queryParams.i = clientId;
queryParams.s = sessionId;

// Validate query (term) is provided
if (!query || typeof query !== 'string') {
// Trim non breaking spaces from query
const queryTrimmed = helpers.trimNonBreakingSpaces(query);

// Validate query (term) is provided and consists of more than non-breaking spaces
if (!queryTrimmed || typeof queryTrimmed !== 'string') {
throw new Error('query is a required parameter of type string');
}

Expand Down Expand Up @@ -112,6 +115,7 @@ function createAutocompleteUrl(query, parameters, userParameters, options) {
const queryString = qs.stringify(queryParams, { indices: false });
const cleanedQuery = query.replace(/^\//, '|'); // For compatibility with backend API

// Note: it is intentional that query is dispatched without being trimmed (`queryTrimmed`)
return `${serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(cleanedQuery)}?${queryString}`;
}

Expand Down
8 changes: 4 additions & 4 deletions src/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function createSearchUrl(query, parameters, userParameters, options, isVoiceSear
queryParams.s = sessionId;

// Trim non breaking spaces from query
query = helpers.trimNonBreakingSpaces(query);
const queryTrimmed = helpers.trimNonBreakingSpaces(query);

// Validate query (term) is provided
if (!query || typeof query !== 'string') {
// Validate query (term) is provided and consists of more than non-breaking spaces
if (!queryTrimmed || typeof queryTrimmed !== 'string') {
throw new Error('query is a required parameter of type string');
}

Expand Down Expand Up @@ -148,7 +148,7 @@ function createSearchUrl(query, parameters, userParameters, options, isVoiceSear

const searchUrl = isVoiceSearch ? 'search/natural_language' : 'search';

return `${serviceUrl}/${searchUrl}/${helpers.encodeURIComponentRFC3986(query)}?${queryString}`;
return `${serviceUrl}/${searchUrl}/${helpers.encodeURIComponentRFC3986(queryTrimmed)}?${queryString}`;
}

/**
Expand Down

0 comments on commit f25457a

Please sign in to comment.