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(storefront): STRF-8599 Drop Jquery: Replaced event's current target to the element passed from utils(on hook) #1821

Merged
merged 1 commit into from
Oct 9, 2020
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
23 changes: 13 additions & 10 deletions assets/js/test-unit/theme/common/faceted-search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ describe('FacetedSearch', () => {
describe('when price range form is submitted', () => {
let event;
let eventName;
let currentTarget = '#facet-range-form';

beforeEach(() => {
eventName = 'facetedSearch-range-submitted';
event = {
currentTarget: '#facet-range-form',
preventDefault: jest.fn(),
};

Expand All @@ -210,7 +210,7 @@ describe('FacetedSearch', () => {
});

it('should set `min_price` and `max_price` query param to corresponding form values if form is valid', () => {
hooks.emit(eventName, event);
hooks.emit(eventName, event, currentTarget);

expect(urlUtils.goToUrl).toHaveBeenCalledWith('/?min_price=0&max_price=100');
});
Expand All @@ -232,20 +232,21 @@ describe('FacetedSearch', () => {
describe('when price range form is submitted with other facets selected', () => {
let event;
let eventName;
let currentTarget;

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

jest.spyOn(urlUtils, 'goToUrl').mockImplementation(() => {});
jest.spyOn(facetedSearch.priceRangeValidator, 'areAll').mockImplementation(() => true);
});

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

expect(urlUtils.goToUrl).toHaveBeenCalledWith('/?brand[]=item1&brand[]=item2&min_price=0&max_price=50');
});
Expand All @@ -254,25 +255,26 @@ describe('FacetedSearch', () => {
describe('when sort filter is submitted', () => {
let event;
let eventName;
let currentTarget;

beforeEach(() => {
eventName = 'sortBy-submitted';
event = {
currentTarget: '#facet-sort',
preventDefault: jest.fn(),
};
currentTarget = '#facet-sort';

jest.spyOn(urlUtils, 'goToUrl').mockImplementation(() => {});
});

it('should set `sort` query param to the value of selected option', () => {
hooks.emit(eventName, event);
hooks.emit(eventName, event, currentTarget);

expect(urlUtils.goToUrl).toHaveBeenCalledWith('/?sort=featured');
});

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

expect(event.preventDefault).toHaveBeenCalled();
});
Expand All @@ -281,25 +283,26 @@ describe('FacetedSearch', () => {
describe('when a facet is clicked', () => {
let event;
let eventName;
let currentTarget;

beforeEach(() => {
eventName = 'facetedSearch-facet-clicked';
event = {
currentTarget: '[href="?brand=item1"]',
preventDefault: jest.fn(),
};
currentTarget = '[href="?brand=item1"]';

jest.spyOn(urlUtils, 'goToUrl').mockImplementation(() => {});
});

it('should change the URL of window to the URL of facet item', () => {
hooks.emit(eventName, event);
hooks.emit(eventName, event, currentTarget);

expect(urlUtils.goToUrl).toHaveBeenCalledWith('?brand=item1');
});

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

expect(event.preventDefault).toHaveBeenCalled();
});
Expand Down
4 changes: 2 additions & 2 deletions assets/js/theme/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ export default class Cart extends PageManager {
this.bindGiftWrappingForm();
});

utils.hooks.on('product-option-change', (event, option) => {
const $changedOption = $(option);
utils.hooks.on('product-option-change', (event, currentTarget) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we need to change places where we do the bind to pass currentTarget for all of them ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

const $changedOption = $(currentTarget);
const $form = $changedOption.parents('form');
const $submit = $('input.button', $form);
const $messageBox = $('.alertMessageBox');
Expand Down
4 changes: 2 additions & 2 deletions assets/js/theme/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import urlUtils from './common/utils/url-utils';
import Url from 'url';

export default class CatalogPage extends PageManager {
onSortBySubmit(event) {
onSortBySubmit(event, currentTarget) {
const url = Url.parse(window.location.href, true);
const queryParams = $(event.currentTarget).serialize().split('=');
const queryParams = $(currentTarget).serialize().split('=');

url.query[queryParams[0]] = queryParams[1];
delete url.query.page;
Expand Down
12 changes: 6 additions & 6 deletions assets/js/theme/common/faceted-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ class FacetedSearch {
this.toggleFacetItems($navList);
}

onFacetClick(event) {
const $link = $(event.currentTarget);
onFacetClick(event, currentTarget) {
const $link = $(currentTarget);
const url = $link.attr('href');

event.preventDefault();
Expand All @@ -363,9 +363,9 @@ class FacetedSearch {
}
}

onSortBySubmit(event) {
onSortBySubmit(event, currentTarget) {
const url = Url.parse(window.location.href, true);
const queryParams = $(event.currentTarget).serialize().split('=');
const queryParams = $(currentTarget).serialize().split('=');

url.query[queryParams[0]] = queryParams[1];
delete url.query.page;
Expand All @@ -379,15 +379,15 @@ class FacetedSearch {
urlUtils.goToUrl(Url.format({ pathname: url.pathname, search: urlUtils.buildQueryString(urlQueryParams) }));
}

onRangeSubmit(event) {
onRangeSubmit(event, currentTarget) {
event.preventDefault();

if (!this.priceRangeValidator.areAll(nod.constants.VALID)) {
return;
}

const url = Url.parse(window.location.href, true);
let queryParams = decodeURI($(event.currentTarget).serialize()).split('&');
let queryParams = decodeURI($(currentTarget).serialize()).split('&');
queryParams = urlUtils.parseQueryParams(queryParams);

for (const key in queryParams) {
Expand Down
4 changes: 2 additions & 2 deletions assets/js/theme/global/quick-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default function () {
});
}, 200);

utils.hooks.on('search-quick', (event) => {
const searchQuery = $(event.currentTarget).val();
utils.hooks.on('search-quick', (event, currentTarget) => {
const searchQuery = $(currentTarget).val();

// server will only perform search with at least 3 characters
if (searchQuery.length < 3) {
Expand Down
49 changes: 24 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "BigCommerce",
"license": "MIT",
"dependencies": {
"@bigcommerce/stencil-utils": "^5.0.3",
"@bigcommerce/stencil-utils": "^6.2.0",
"core-js": "^3.6.5",
"creditcards": "^3.0.1",
"easyzoom": "^2.5.3",
Expand Down