Skip to content

M2C-21768 Validate product quantity on Wishlist update #25641

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ define([
groupedInfo: '#super-product-table input',
downloadableInfo: '#downloadable-links-list input',
customOptionsInfo: '.product-custom-option',
qtyInfo: '#qty'
qtyInfo: '#qty',
actionElement: '[data-action="add-to-wishlist"]'
},

/** @inheritdoc */
Expand All @@ -30,8 +31,10 @@ define([
_bind: function () {
var options = this.options,
dataUpdateFunc = '_updateWishlistData',
validateProductQty = '_validateWishlistQty',
changeCustomOption = 'change ' + options.customOptionsInfo,
changeQty = 'change ' + options.qtyInfo,
updateWishlist = 'click ' + options.actionElement,
events = {},
key;

Expand All @@ -45,6 +48,7 @@ define([

events[changeCustomOption] = dataUpdateFunc;
events[changeQty] = dataUpdateFunc;
events[updateWishlist] = validateProductQty;

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

$(form).attr('action', action).submit();
});
},

/**
* Validate product quantity before updating Wish List
*
* @param {jQuery.Event} event
* @private
*/
_validateWishlistQty: function (event) {
var element = $(this.options.qtyInfo);

if (!(element.validation() && element.validation('isValid'))) {
event.preventDefault();
event.stopPropagation();

return;
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
define([
'jquery',
'Magento_Wishlist/js/add-to-wishlist'
], function ($) {
], function ($, Widget) {
'use strict';

describe('Testing addToWishlist widget', function () {
var wdContainer;
var wdContainer,
wishlistWidget,
eventMock = {
preventDefault: jasmine.createSpy(),
stopPropagation: jasmine.createSpy()
};

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

afterEach(function () {
Expand All @@ -31,5 +38,15 @@ define([
});
expect(wdContainer.addToWishlist('option', 'bundleInfo')).toBe('test');
});

it('verify update wichlist with validate product qty, valid qty', function () {
var validation = spyOn($.fn, 'validation').and.returnValue(false);

wishlistWidget._validateWishlistQty(eventMock);
expect(validation).toHaveBeenCalled();
expect(eventMock.preventDefault).toHaveBeenCalled();
expect(eventMock.stopPropagation).toHaveBeenCalled();
});

});
});