Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CP): ES-156 Fixed the filtering issues with price range filter selection #1471

Merged
merged 2 commits into from
Apr 10, 2019
Merged
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
30 changes: 29 additions & 1 deletion assets/js/test-unit/theme/common/faceted-search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ describe('FacetedSearch', () => {
'<input name="min_price" value="0">' +
'<input name="max_price" value="100">' +
'</form>' +
'<form id="facet-range-form-with-other-facets">' +
'<input name="brand[]" value="item1">' +
'<input name="brand[]" value="item2">' +
'<input name="min_price" value="0">' +
'<input name="max_price" value="50">' +
'</form>' +
'</div>' +
'</div>';

Expand Down Expand Up @@ -216,13 +222,35 @@ describe('FacetedSearch', () => {
expect(urlUtils.goToUrl).not.toHaveBeenCalled();
});

it('should prevent default event', function() {
it('should prevent default event', () => {
hooks.emit(eventName, event);

expect(event.preventDefault).toHaveBeenCalled();
});
});

describe('when price range form is submitted with other facets selected', () => {
let event;
let eventName;

beforeEach(() => {
eventName = 'facetedSearch-range-submitted';
event = {
currentTarget: '#facet-range-form-with-other-facets',
preventDefault: jasmine.createSpy('preventDefault'),
};

spyOn(urlUtils, 'goToUrl');
spyOn(facetedSearch.priceRangeValidator, 'areAll').and.returnValue(true);
});

it('send `min_price` and `max_price` query params if form is valid', () => {
hooks.emit(eventName, event);

expect(urlUtils.goToUrl).toHaveBeenCalledWith('/context.html?brand[]=item1&brand[]=item2&min_price=0&max_price=50');
});
});

describe('when sort filter is submitted', () => {
let event;
let eventName;
Expand Down
20 changes: 20 additions & 0 deletions assets/js/test-unit/theme/common/url-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,25 @@ describe('Url Utilities', () => {

expect(queryString).toEqual(expectedQueryString);
});

it('should parse the input query params from the input array and return the query string object', () => {
const queryInput = [
'brand[]=38',
'brand[]=39',
'brand[]=40',
'search_query=',
'min_price=15',
'max_price=40',
];
const expectedResult = {
'brand[]': ['38', '39', '40'],
max_price: '40',
min_price: '15',
search_query: '',
};
const queryStringObj = urlUtil.parseQueryParams(queryInput);

expect(queryStringObj).toEqual(expectedResult);
});
});
});
13 changes: 10 additions & 3 deletions assets/js/theme/common/faceted-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,17 @@ class FacetedSearch {
return;
}

const url = Url.parse(window.location.href);
const queryParams = decodeURI($(event.currentTarget).serialize());
const url = Url.parse(window.location.href, true);
bc-krishsenthilraj marked this conversation as resolved.
Show resolved Hide resolved
let queryParams = decodeURI($(event.currentTarget).serialize()).split('&');
queryParams = urlUtils.parseQueryParams(queryParams);

urlUtils.goToUrl(Url.format({ pathname: url.pathname, search: `?${queryParams}` }));
for (const key in queryParams) {
if (queryParams.hasOwnProperty(key)) {
url.query[key] = queryParams[key];
}
}

urlUtils.goToUrl(Url.format({ pathname: url.pathname, search: urlUtils.buildQueryString(url.query) }));
}

onStateChange() {
Expand Down
20 changes: 20 additions & 0 deletions assets/js/theme/common/url-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ const urlUtils = {

return out.substring(1);
},

parseQueryParams: (queryData) => {
bc-krishsenthilraj marked this conversation as resolved.
Show resolved Hide resolved
const params = {};

for (let i = 0; i < queryData.length; i++) {
const temp = queryData[i].split('=');

if (temp[0] in params) {
if (Array.isArray(params[temp[0]])) {
params[temp[0]].push(temp[1]);
} else {
params[temp[0]] = [params[temp[0]], temp[1]];
}
} else {
params[temp[0]] = temp[1];
}
}

return params;
},
};

export default urlUtils;