Skip to content
Open
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
53 changes: 53 additions & 0 deletions spec/src/modules/browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,59 @@ describe('ConstructorIO - Browse', () => {
});
});

it('Should send refined filters supplied within the qs param as a top level url parameter', (done) => {
const qsParam = { refined_filters: { group_id: 'BrandXY' } };
const { browse } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

browse.getBrowseResults(filterName, filterValue, { qsParam }).then((res) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important Issue: All three new tests use the callback-style done pattern without a .catch() handler. If the promise rejects (e.g. network or assertion error), done is never called and the test silently times out rather than failing with a meaningful error. Add a rejection handler:

browse.getBrowseResults(filterName, filterValue, { qsParam })
  .then((res) => {
    // assertions...
    done();
  })
  .catch(done);

This matches the pattern already used in the surrounding existing tests in this file.

const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(res).to.have.property('request').to.be.an('object');
expect(requestedUrlParams).to.have.property('refined_filters');
expect(requestedUrlParams.refined_filters).to.have.property('group_id').to.equal('BrandXY');
expect(requestedUrlParams).to.not.have.property('qs');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This assertion verifies that qs is absent when qsParam contains only refined_filters. Consider adding a complementary assertion that refined_filters is not nested inside a qs key (i.e. the old behaviour is definitively gone). The current assertions are correct but an explicit negative check would make the test intent clearer and catch future regressions more precisely.

expect(res.request.refined_filters).to.deep.equal(qsParam.refined_filters);
done();
});
});

it('Should retain the remaining qs param values when sending refined filters as a top level url parameter', (done) => {
const qsParam = {
num_results_per_page: '10',
refined_filters: { group_id: 'BrandXY' },
};
const { browse } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

browse.getBrowseResults(filterName, filterValue, { qsParam }).then((res) => {
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);

expect(requestedUrlParams.refined_filters).to.have.property('group_id').to.equal('BrandXY');
expect(JSON.parse(requestedUrlParams.qs)).to.deep.equal({ num_results_per_page: '10' });
expect(res.request.num_results_per_page).to.equal(10);
expect(res.request.refined_filters).to.deep.equal(qsParam.refined_filters);
done();
});
});

it('Should not mutate the supplied qs param when sending refined filters as a top level url parameter', (done) => {
const qsParam = { refined_filters: { group_id: 'BrandXY' } };
const { browse } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

browse.getBrowseResults(filterName, filterValue, { qsParam }).then(() => {
expect(qsParam).to.deep.equal({ refined_filters: { group_id: 'BrandXY' } });
done();
});
});

it('Should properly encode path parameters', (done) => {
const specialCharacters = '+[]&';
const filterNameSpecialCharacters = `name ${specialCharacters}`;
Expand Down
12 changes: 10 additions & 2 deletions src/modules/browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,17 @@ function createQueryParams(parameters, userParameters, options) {
queryParams.pre_filter_expression = JSON.stringify(preFilterExpression);
}

// Pull qs param from parameters
// Pull qs param from parameters - refined filters are sent as a top level parameter
if (qsParam) {
queryParams.qs = JSON.stringify(qsParam);
const { refined_filters: refinedFilters, ...remainingQsParam } = qsParam;

if (refinedFilters) {
queryParams.refined_filters = refinedFilters;
}

if (Object.keys(remainingQsParam).length) {
queryParams.qs = JSON.stringify(remainingQsParam);
}
}
}

Expand Down
Loading