From a5a2cbcf7fe26964971667747aa09e8a58c9cb8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tylek?= Date: Mon, 18 Nov 2019 16:58:43 +0100 Subject: [PATCH 1/3] M2C-21768 Validate product quantity on Wishlist update M2C-21768 Add updateWishlist event --- .../view/frontend/web/js/add-to-wishlist.js | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js b/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js index aca843872af65..7dd2d0d6227c3 100644 --- a/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js +++ b/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js @@ -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 */ @@ -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; @@ -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) { @@ -220,7 +224,24 @@ 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; + } + }, + }); return $.mage.addToWishlist; From 5415e99c0525dc3a505ef035a11e283bb446b026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tylek?= Date: Thu, 21 Nov 2019 18:03:39 +0100 Subject: [PATCH 2/3] M2C-21768 Fix tests --- .../Wishlist/view/frontend/web/js/add-to-wishlist.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js b/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js index 7dd2d0d6227c3..55cd77b196be5 100644 --- a/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js +++ b/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js @@ -236,12 +236,12 @@ define([ var element = $(this.options.qtyInfo); if (!(element.validation() && element.validation('isValid'))) { - event.preventDefault(); - event.stopPropagation(); - return; - } - }, + event.preventDefault(); + event.stopPropagation(); + return; + } + } }); return $.mage.addToWishlist; From b3982062f89f247abd5ee784e160f734c3e86a16 Mon Sep 17 00:00:00 2001 From: Nazar Klovanych Date: Tue, 14 Jan 2020 16:18:24 +0200 Subject: [PATCH 3/3] Cover changes with jasmine tests --- .../frontend/web/js/add-to-wishlist.test.js | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.test.js index 207d14bf990c3..f157ae27ee532 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.test.js @@ -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 = $(''); + wishlistWidget = new Widget(); + $.fn.validation = {}; }); afterEach(function () { @@ -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(); + }); + }); });