Skip to content

Commit bd02e15

Browse files
author
Anna Bukatar
committed
ACP2E-1547: Persistent Shopping Cart results in an Invalid state change requested error in checkout
1 parent bd8634b commit bd02e15

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

app/code/Magento/Checkout/view/frontend/web/js/model/quote.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ define([
141141
});
142142

143143
return total;
144+
},
145+
146+
/**
147+
* @return {Boolean}
148+
*/
149+
isPersistent: function () {
150+
return !!Number(quoteData['is_persistent']);
144151
}
145152
};
146153
});

app/code/Magento/Checkout/view/frontend/web/js/model/shipping-rate-processor/new-address.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ define([
5252
shippingService.setShippingRates(cache);
5353
shippingService.isLoading(false);
5454
} else {
55+
let async = quote.isPersistent() ? false : true;
56+
5557
storage.post(
56-
serviceUrl, payload, false
58+
serviceUrl, payload, false, 'application/json', {}, async
5759
).done(function (result) {
5860
rateRegistry.set(address.getCacheKey(), result);
5961
shippingService.setShippingRates(result);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'squire',
8+
'jquery',
9+
'ko'
10+
], function (Squire, $, ko) {
11+
'use strict';
12+
13+
var injector = new Squire(),
14+
mixin,
15+
serviceUrl = 'rest/V1/guest-carts/estimate-shipping-methods',
16+
mocks = {
17+
'mage/storage': {
18+
post: function () {} // jscs:ignore jsDoc
19+
},
20+
'Magento_Customer/js/customer-data': {
21+
get: jasmine.createSpy().and.returnValue(
22+
ko.observable({
23+
'data_id': 1
24+
})
25+
)
26+
},
27+
'Magento_Checkout/js/model/url-builder': {
28+
createUrl: jasmine.createSpy().and.returnValue(serviceUrl)
29+
}
30+
};
31+
32+
describe('Magento_Checkout/js/model/shipping-rate-processor/new-address', function () {
33+
beforeEach(function (done) {
34+
window.checkoutConfig = {
35+
'quoteData': {
36+
'is_persistent': '0'
37+
}
38+
};
39+
40+
injector.mock(mocks);
41+
injector.require(['Magento_Checkout/js/model/shipping-rate-processor/new-address'], function (Mixin) {
42+
mixin = Mixin;
43+
done();
44+
});
45+
});
46+
47+
afterEach(function () {
48+
try {
49+
injector.clean();
50+
injector.remove();
51+
} catch (e) {}
52+
53+
delete window.checkoutConfig.quoteData.is_persistent;
54+
});
55+
56+
it('Check that estimate-shipping-methods API is called synchronously for persistent cart', function () {
57+
var deferral = new $.Deferred();
58+
59+
window.checkoutConfig.quoteData.is_persistent = '1';
60+
spyOn(mocks['mage/storage'], 'post').and.callFake(function () {
61+
return deferral.resolve({});
62+
});
63+
64+
mixin.getRates({
65+
/** Stub */
66+
'getCacheKey': function () {
67+
return false;
68+
}
69+
});
70+
71+
expect(mocks['mage/storage'].post).toHaveBeenCalledWith(
72+
serviceUrl,
73+
'{"address":{}}',
74+
false,
75+
'application/json',
76+
{},
77+
false
78+
);
79+
});
80+
81+
it('Check that estimate-shipping-methods API is called asynchronously', function () {
82+
var deferral = new $.Deferred();
83+
84+
spyOn(mocks['mage/storage'], 'post').and.callFake(function () {
85+
return deferral.resolve({});
86+
});
87+
88+
mixin.getRates({
89+
/** Stub */
90+
'getCacheKey': function () {
91+
return false;
92+
}
93+
});
94+
95+
expect(mocks['mage/storage'].post).toHaveBeenCalledWith(
96+
serviceUrl,
97+
'{"address":{}}',
98+
false,
99+
'application/json',
100+
{},
101+
true
102+
);
103+
});
104+
});
105+
});

lib/web/mage/storage.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,23 @@ define(['jquery', 'mage/url'], function ($, urlBuilder) {
3636
* @param {Boolean} global
3737
* @param {String} contentType
3838
* @param {Object} headers
39+
* @param {Boolean} async
3940
* @returns {Deferred}
4041
*/
41-
post: function (url, data, global, contentType, headers) {
42+
post: function (url, data, global, contentType, headers, async) {
4243
headers = headers || {};
4344
global = global === undefined ? true : global;
4445
contentType = contentType || 'application/json';
46+
async = async === undefined ? true : async;
4547

4648
return $.ajax({
4749
url: urlBuilder.build(url),
4850
type: 'POST',
4951
data: data,
5052
global: global,
5153
contentType: contentType,
52-
headers: headers
54+
headers: headers,
55+
async: async
5356
});
5457
},
5558

0 commit comments

Comments
 (0)