Skip to content

Commit

Permalink
MC-34415: Shopping Cart is not empty after removing item
Browse files Browse the repository at this point in the history
  • Loading branch information
ameysar committed Jun 11, 2020
1 parent e583544 commit 4d24466
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 10 deletions.
16 changes: 9 additions & 7 deletions app/code/Magento/Checkout/view/frontend/web/js/empty-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* See COPYING.txt for license details.
*/

define([
'Magento_Customer/js/customer-data'
], function (customerData) {
define(['Magento_Customer/js/customer-data'], function (customerData) {
'use strict';

var cartData = customerData.get('cart');
return function () {
var cartData = customerData.get('cart');

if (cartData().items && cartData().items.length !== 0) {
customerData.reload(['cart'], false);
}
customerData.getInitCustomerData().done(function () {
if (cartData().items && cartData().items.length !== 0) {
customerData.reload(['cart'], false);
}
});
};
});
13 changes: 12 additions & 1 deletion app/code/Magento/Customer/view/frontend/web/js/customer-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ define([
invalidateCacheByCloseCookieSession,
dataProvider,
buffer,
customerData;
customerData,
deferred = $.Deferred();

url.setBaseUrl(window.BASE_URL);
options.sectionLoadUrl = url.build('customer/section/load');
Expand Down Expand Up @@ -341,6 +342,15 @@ define([
$.cookieStorage.set('section_data_ids', sectionDataIds);
},

/**
* Checks if customer data is initialized.
*
* @returns {jQuery.Deferred}
*/
getInitCustomerData: function () {
return deferred.promise();
},

/**
* @param {Object} settings
* @constructor
Expand All @@ -350,6 +360,7 @@ define([
invalidateCacheBySessionTimeOut(settings);
invalidateCacheByCloseCookieSession();
customerData.init();
deferred.resolve();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
?>
<form id="checkout_multishipping_form"
data-mage-init='{
"multiShipping":{},
"multiShipping": {"itemsQty": <?= /* @noEscape */ (int)$block->getCheckout()->getQuote()->getItemsSummaryQty() ?>},
"cartUpdate": {
"validationURL": "<?= $block->escapeUrl($block->getUrl('multishipping/checkout/checkItems')) ?>",
"eventName": "updateMulticartItemQty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

define([
'jquery',
'Magento_Customer/js/customer-data',
'jquery-ui-modules/widget'
], function ($) {
], function ($, customerData) {
'use strict';

$.widget('mage.multiShipping', {
options: {
itemsQty: 0,
addNewAddressBtn: 'button[data-role="add-new-address"]', // Add a new multishipping address.
addNewAddressFlag: '#add_new_address_flag', // Hidden input field with value 0 or 1.
canContinueBtn: 'button[data-role="can-continue"]', // Continue (update quantity or go to shipping).
Expand All @@ -22,10 +24,24 @@ define([
* @private
*/
_create: function () {
this._prepareCartData();
$(this.options.addNewAddressBtn).on('click', $.proxy(this._addNewAddress, this));
$(this.options.canContinueBtn).on('click', $.proxy(this._canContinue, this));
},

/**
* Takes cart items qty from current cart data and compare it with current items qty
* Reloads cart data if cart items qty is wrong
* @private
*/
_prepareCartData: function () {
var cartData = customerData.get('cart');

if (cartData()['summary_count'] !== this.options.itemsQty) {
customerData.reload(['cart'], false);
}
},

/**
* Add a new address. Set the hidden input field and submit the form. Then enter a new shipping address.
* @private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/* eslint-disable max-nested-callbacks */
/*jscs:disable jsDoc*/
define([
'squire', 'jquery', 'ko'
], function (Squire, $, ko) {
'use strict';

describe('Magento_Checkout/js/empty-cart', function () {
var injector = new Squire(),
cartData = ko.observable({}),
mocks = {
'Magento_Customer/js/customer-data': {
get: jasmine.createSpy('get', function () {
return cartData;
}).and.callThrough(),
reload: jasmine.createSpy(),
getInitCustomerData: function () {}
}
},
deferred,
emptyCart;

beforeEach(function (done) {
injector.mock(mocks);
injector.require(['Magento_Checkout/js/empty-cart'], function (instance) {
emptyCart = instance;
done();
});
});

afterEach(function () {
try {
injector.clean();
injector.remove();
} catch (e) {}

cartData({});
});

describe('Check Cart data preparation process', function () {
it('Tests that Cart data is NOT checked before initialization', function () {
spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () {
deferred = $.Deferred();

return deferred.promise();
});
expect(emptyCart()).toBe(undefined);

expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
expect(mocks['Magento_Customer/js/customer-data'].getInitCustomerData).toHaveBeenCalled();
expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled();
});

it('Tests that Cart data does NOT reload if there are no items in it', function () {
spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () {
deferred = $.Deferred();

deferred.resolve();

return deferred.promise();
});
cartData({
items: []
});
emptyCart();

expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled();
});

it('Tests that Cart data is checked only after initialization', function () {
spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () {
deferred = $.Deferred();

return deferred.promise();
});
cartData({
items: [1]
});
emptyCart();

expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled();

deferred.resolve();

expect(mocks['Magento_Customer/js/customer-data'].reload).toHaveBeenCalledWith(['cart'], false);
});

it('Tests that Cart data reloads if it has items', function () {
spyOn(mocks['Magento_Customer/js/customer-data'], 'getInitCustomerData').and.callFake(function () {
deferred = $.Deferred();

deferred.resolve();

return deferred.promise();
});
cartData({
items: [1]
});
emptyCart();

expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
expect(mocks['Magento_Customer/js/customer-data'].reload).toHaveBeenCalledWith(['cart'], false);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/* eslint-disable max-nested-callbacks */
define([
'squire',
'jquery',
'ko',
'multiShipping'
], function (Squire, $, ko, MultiShipping) {
'use strict';

describe('Magento_Multishipping/js/multi-shipping', function () {
var injector = new Squire(),
Obj;

describe('Check Cart data preparation process', function () {
var customerData = ko.observable({}),
mocks = {
'Magento_Customer/js/customer-data': {
get: jasmine.createSpy('get', function () {
return customerData;
}).and.callThrough(),
reload: jasmine.createSpy()
}
},
summaryCount = {};

beforeEach(function (done) {
injector.mock(mocks);
injector.require(['multiShipping'], function (Instance) {
Obj = Instance;
done();
});
});

afterEach(function () {
try {
injector.clean();
injector.remove();
} catch (e) {}

customerData({});
});

it('Prepare Cart data with the same items qty', function () {
summaryCount['summary_count'] = 0;
customerData(summaryCount);
new Obj({});

expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
expect(mocks['Magento_Customer/js/customer-data'].reload).not.toHaveBeenCalled();
});

it('Prepare Cart data with different items qty', function () {
summaryCount['summary_count'] = 1;
customerData(summaryCount);
new Obj({});

expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
expect(mocks['Magento_Customer/js/customer-data'].reload).toHaveBeenCalledWith(['cart'], false);
});
});

describe('Check Multishipping events', function () {
var addNewAddressBtn,
addressflag,
canContinueBtn,
canContinueFlag;

beforeEach(function () {
addNewAddressBtn = $('<button type="button" data-role="add-new-address"/>');
addressflag = $('<input type="hidden" value="0" id="add_new_address_flag"/>');
canContinueBtn = $('<button type="submit" data-role="can-continue" data-flag="1"/>');
canContinueFlag = $('<input type="hidden" value="0" id="can_continue_flag"/>');
$(document.body).append(addNewAddressBtn)
.append(addressflag)
.append(canContinueBtn)
.append(canContinueFlag);
});

afterEach(function () {
addNewAddressBtn.remove();
addressflag.remove();
canContinueBtn.remove();
canContinueFlag.remove();
});

it('Check add new address event', function () {
Obj = new MultiShipping({});
Obj.element = jasmine.createSpyObj('element', ['submit']);
addNewAddressBtn.click();

expect(Obj.element.submit).toHaveBeenCalled();
expect(addressflag.val()).toBe('1');
});

it('Check can continue event', function () {
Obj = new MultiShipping({});
Obj.element = jasmine.createSpyObj('element', ['submit']);
canContinueBtn.click();

expect(Obj.element.submit).not.toHaveBeenCalled();
expect(canContinueFlag.val()).toBe('1');
});
});
});
});

0 comments on commit 4d24466

Please sign in to comment.