Skip to content

Commit a0ceb97

Browse files
authored
ENGCOM-6305: M2C-21768 Validate product quantity on Wishlist update #25641
2 parents 6aae51b + e216f57 commit a0ceb97

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ define([
1616
groupedInfo: '#super-product-table input',
1717
downloadableInfo: '#downloadable-links-list input',
1818
customOptionsInfo: '.product-custom-option',
19-
qtyInfo: '#qty'
19+
qtyInfo: '#qty',
20+
actionElement: '[data-action="add-to-wishlist"]'
2021
},
2122

2223
/** @inheritdoc */
@@ -30,8 +31,10 @@ define([
3031
_bind: function () {
3132
var options = this.options,
3233
dataUpdateFunc = '_updateWishlistData',
34+
validateProductQty = '_validateWishlistQty',
3335
changeCustomOption = 'change ' + options.customOptionsInfo,
3436
changeQty = 'change ' + options.qtyInfo,
37+
updateWishlist = 'click ' + options.actionElement,
3538
events = {},
3639
key;
3740

@@ -45,6 +48,7 @@ define([
4548

4649
events[changeCustomOption] = dataUpdateFunc;
4750
events[changeQty] = dataUpdateFunc;
51+
events[updateWishlist] = validateProductQty;
4852

4953
for (key in options.productType) {
5054
if (options.productType.hasOwnProperty(key) && options.productType[key] + 'Info' in options) {
@@ -220,6 +224,23 @@ define([
220224

221225
$(form).attr('action', action).submit();
222226
});
227+
},
228+
229+
/**
230+
* Validate product quantity before updating Wish List
231+
*
232+
* @param {jQuery.Event} event
233+
* @private
234+
*/
235+
_validateWishlistQty: function (event) {
236+
var element = $(this.options.qtyInfo);
237+
238+
if (!(element.validation() && element.validation('isValid'))) {
239+
event.preventDefault();
240+
event.stopPropagation();
241+
242+
return;
243+
}
223244
}
224245
});
225246

dev/tests/js/jasmine/tests/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@
66
define([
77
'jquery',
88
'Magento_Wishlist/js/add-to-wishlist'
9-
], function ($) {
9+
], function ($, Widget) {
1010
'use strict';
1111

1212
describe('Testing addToWishlist widget', function () {
13-
var wdContainer;
13+
var wdContainer,
14+
wishlistWidget,
15+
eventMock = {
16+
preventDefault: jasmine.createSpy(),
17+
stopPropagation: jasmine.createSpy()
18+
};
1419

1520
beforeEach(function () {
1621
wdContainer = $('<input type="hidden" class="bundle-option-11 product bundle option" \n' +
1722
'name="bundle_option[11]" value="15" aria-required="true"/>');
23+
wishlistWidget = new Widget();
24+
$.fn.validation = {};
1825
});
1926

2027
afterEach(function () {
@@ -31,5 +38,15 @@ define([
3138
});
3239
expect(wdContainer.addToWishlist('option', 'bundleInfo')).toBe('test');
3340
});
41+
42+
it('verify update wichlist with validate product qty, valid qty', function () {
43+
var validation = spyOn($.fn, 'validation').and.returnValue(false);
44+
45+
wishlistWidget._validateWishlistQty(eventMock);
46+
expect(validation).toHaveBeenCalled();
47+
expect(eventMock.preventDefault).toHaveBeenCalled();
48+
expect(eventMock.stopPropagation).toHaveBeenCalled();
49+
});
50+
3451
});
3552
});

0 commit comments

Comments
 (0)