Skip to content

Commit f42d8b3

Browse files
committed
Merge branch 'MAGETWO-89532' into MPI-PR
2 parents 7c0a49c + bf12959 commit f42d8b3

File tree

2 files changed

+69
-22
lines changed
  • app/code/Magento/Checkout/view/frontend/web/js
  • dev/tests/js/jasmine/tests/app/code/Magento/Checkout/frontend/js

2 files changed

+69
-22
lines changed

app/code/Magento/Checkout/view/frontend/web/js/sidebar.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ define([
220220
* @param {HTMLElement} elem
221221
*/
222222
_updateItemQtyAfter: function (elem) {
223+
var productData = this._getProductById(Number(elem.data('cart-item')));
224+
225+
if (!_.isUndefined(productData)) {
226+
$(document).trigger('ajax:updateCartItemQty', productData['product_sku']);
227+
}
223228
this._hideItemButton(elem);
224229
},
225230

@@ -242,15 +247,26 @@ define([
242247
* @private
243248
*/
244249
_removeItemAfter: function (elem) {
245-
var productData = _.find(customerData.get('cart')().items, function (item) {
246-
return Number(elem.data('cart-item')) === Number(item['item_id']);
247-
});
250+
var productData = this._getProductById(Number(elem.data('cart-item')));
248251

249252
if (!_.isUndefined(productData)) {
250253
$(document).trigger('ajax:removeFromCart', productData['product_sku']);
251254
}
252255
},
253256

257+
/**
258+
* Retrieves product data by Id.
259+
*
260+
* @param {Number} productId - product Id
261+
* @returns {Object|undefined}
262+
* @private
263+
*/
264+
_getProductById: function (productId) {
265+
return _.find(customerData.get('cart')().items, function (item) {
266+
return productId === Number(item['item_id']);
267+
});
268+
},
269+
254270
/**
255271
* @param {String} url - ajax url
256272
* @param {Object} data - post data for ajax call

dev/tests/js/jasmine/tests/app/code/Magento/Checkout/frontend/js/sidebar.test.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,24 @@ define([
2121
}
2222
}
2323
},
24-
sidebar;
24+
sidebar,
25+
cartData = {
26+
'items': [
27+
{
28+
'item_id': 1,
29+
'product_sku': 'bundle'
30+
},
31+
{
32+
'item_id': 5,
33+
'product_sku': 'simple'
34+
},
35+
{
36+
'item_id': 7,
37+
'product_sku': 'configurable'
38+
}
39+
]
40+
},
41+
cart = ko.observable(cartData);
2542

2643
beforeEach(function (done) {
2744
injector.mock(mocks);
@@ -32,24 +49,6 @@ define([
3249
});
3350

3451
describe('Check remove mini-cart item callback.', function () {
35-
var cartData = {
36-
'items': [
37-
{
38-
'item_id': 1,
39-
'product_sku': 'bundle'
40-
},
41-
{
42-
'item_id': 5,
43-
'product_sku': 'simple'
44-
},
45-
{
46-
'item_id': 7,
47-
'product_sku': 'configurable'
48-
}
49-
]
50-
},
51-
cart = ko.observable(cartData);
52-
5352
beforeEach(function () {
5453
spyOn(jQuery.fn, 'trigger');
5554
spyOn(mocks['Magento_Customer/js/customer-data'], 'get').and.returnValue(cart);
@@ -74,5 +73,37 @@ define([
7473
expect(jQuery('body').trigger).not.toHaveBeenCalled();
7574
});
7675
});
76+
77+
describe('Check update item quantity callback.', function () {
78+
beforeEach(function () {
79+
spyOn(jQuery.fn, 'trigger');
80+
spyOn(mocks['Magento_Customer/js/customer-data'], 'get').and.returnValue(cart);
81+
});
82+
83+
it('Method "_updateItemQtyAfter" is defined', function () {
84+
expect(sidebar._updateItemQtyAfter).toBeDefined();
85+
});
86+
87+
it('Cart item is exists', function () {
88+
var elem = $('<input>').data('cart-item', 5);
89+
90+
spyOn(sidebar, '_hideItemButton');
91+
92+
sidebar._updateItemQtyAfter(elem);
93+
expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
94+
expect(jQuery('body').trigger).toHaveBeenCalledWith('ajax:updateCartItemQty', 'simple');
95+
expect(sidebar._hideItemButton).toHaveBeenCalledWith(elem);
96+
});
97+
98+
it('Cart item doesn\'t exists', function () {
99+
var elem = $('<input>').data('cart-item', 100);
100+
101+
spyOn(sidebar, '_hideItemButton');
102+
103+
sidebar._updateItemQtyAfter(elem);
104+
expect(jQuery('body').trigger).not.toHaveBeenCalled();
105+
expect(sidebar._hideItemButton).toHaveBeenCalledWith(elem);
106+
});
107+
});
77108
});
78109
});

0 commit comments

Comments
 (0)