Skip to content

Commit afa8efa

Browse files
committed
ACP2E-2260: Recently viewed product data not updated as per the store view
- Add test coverage
1 parent ec9f5b9 commit afa8efa

File tree

4 files changed

+235
-8
lines changed

4 files changed

+235
-8
lines changed

app/code/Magento/Catalog/view/frontend/web/js/product/provider.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ define([
9898
return;
9999
}
100100

101+
// Filter initial ids to remove "out of scope" and "outdated" data
102+
this.ids(
103+
this.filterIds(this.ids())
104+
);
101105
this.initIdsListener();
102106
this.idsMerger(
103107
this.idsStorage.get(),
@@ -170,7 +174,7 @@ define([
170174

171175
if (!_.isEmpty(data)) {
172176
this.ids(
173-
this.filterIds(_.extend(this.ids(), data))
177+
this.filterIds(_.extend(utils.copy(this.ids()), data))
174178
);
175179
}
176180
},

app/code/Magento/Catalog/view/frontend/web/js/product/storage/data-storage.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,9 @@ define([
271271
sentDataIds = _.keys(this.request.data);
272272
currentDataIds = _.keys(ids);
273273

274-
_.each(currentDataIds, function (id) {
275-
if (_.lastIndexOf(sentDataIds, id) === -1) {
276-
return false;
277-
}
274+
return _.every(currentDataIds, function (id) {
275+
return _.lastIndexOf(sentDataIds, id) !== -1;
278276
});
279-
280-
return true;
281277
}
282278

283279
return false;
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/************************************************************************
2+
*
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained
13+
* from Adobe.
14+
* ************************************************************************
15+
*/
16+
17+
/* eslint-disable max-nested-callbacks */
18+
define([
19+
'squire',
20+
'underscore'
21+
], function (Squire, _) {
22+
'use strict';
23+
24+
var injector = new Squire(),
25+
customerData,
26+
productResolver,
27+
productResolverIds,
28+
storage,
29+
mocks,
30+
obj,
31+
localStorageIds,
32+
namespace = 'namespace',
33+
windowCheckoutData,
34+
timestamp;
35+
36+
beforeEach(function (done) {
37+
customerData = {};
38+
productResolverIds = [];
39+
productResolver = jasmine.createSpy().and.callFake(function () {
40+
return productResolverIds;
41+
});
42+
storage = {
43+
onStorageInit: jasmine.createSpy(),
44+
createStorage: jasmine.createSpy()
45+
};
46+
mocks = {
47+
'Magento_Customer/js/customer-data': customerData,
48+
'Magento_Catalog/js/product/view/product-ids-resolver': productResolver,
49+
'Magento_Catalog/js/product/storage/storage-service': storage
50+
};
51+
injector.mock(mocks);
52+
injector.require(['Magento_Catalog/js/product/provider'], function (UiClass) {
53+
timestamp = new Date().getTime() / 1000;
54+
obj = new UiClass({
55+
identifiersConfig: {
56+
namespace: namespace
57+
},
58+
ids: {
59+
'website-1-1': {
60+
added_at: timestamp - 300,
61+
product_id: 1,
62+
scope_id: 1
63+
}
64+
},
65+
data: {
66+
store: '1',
67+
currency: 'USD',
68+
productCurrentScope: 'website'
69+
}
70+
});
71+
localStorageIds = {
72+
'website-1-2': {
73+
added_at: timestamp - 60,
74+
product_id: 2,
75+
scope_id: 1
76+
},
77+
'website-1-3': {
78+
added_at: timestamp - 120,
79+
product_id: 3,
80+
scope_id: 1
81+
}
82+
};
83+
done();
84+
});
85+
windowCheckoutData = window.checkout;
86+
});
87+
88+
afterEach(function () {
89+
try {
90+
injector.clean();
91+
injector.remove();
92+
} catch (e) {
93+
}
94+
window.localStorage.clear();
95+
window.checkout = windowCheckoutData;
96+
});
97+
98+
describe('Magento_Catalog/js/product/provider', function () {
99+
describe('"_resolveDataByIds" method', function () {
100+
beforeEach(function () {
101+
obj.initIdsListener = jasmine.createSpy();
102+
obj.idsMerger = jasmine.createSpy();
103+
obj.idsHandler = jasmine.createSpy();
104+
obj.filterIds = jasmine.createSpy().and.returnValue({});
105+
});
106+
it('check "window.checkout" is required', function () {
107+
window.checkout = undefined;
108+
obj.ids = jasmine.createSpy();
109+
110+
obj._resolveDataByIds();
111+
112+
expect(obj.ids).not.toHaveBeenCalled();
113+
expect(obj.filterIds).not.toHaveBeenCalled();
114+
expect(obj.idsHandler).not.toHaveBeenCalled();
115+
expect(obj.initIdsListener).not.toHaveBeenCalled();
116+
expect(obj.idsMerger).not.toHaveBeenCalled();
117+
});
118+
it('check that initial ids, localstorage ids and ids from customer data are processed', function () {
119+
var initialIds,
120+
customerDataIds,
121+
customerDataGet;
122+
123+
initialIds = obj.ids();
124+
customerDataIds = {
125+
4: {},
126+
6: {}
127+
};
128+
customerDataGet = function () {
129+
return {
130+
items: customerDataIds
131+
};
132+
};
133+
window.checkout = {
134+
baseUrl: 'http://localhost/',
135+
websiteId: 1
136+
};
137+
obj.idsStorage = {
138+
get: jasmine.createSpy().and.returnValue(localStorageIds),
139+
lifetime: 1000
140+
};
141+
obj.prepareDataFromCustomerData = jasmine.createSpy().and.returnValue(customerDataIds);
142+
customerData.get = jasmine.createSpy().and.returnValue(customerDataGet);
143+
144+
obj._resolveDataByIds();
145+
146+
expect(obj.filterIds).toHaveBeenCalledOnceWith(initialIds);
147+
expect(obj.idsHandler).toHaveBeenCalledOnceWith({});
148+
expect(obj.initIdsListener).toHaveBeenCalled();
149+
expect(customerData.get).toHaveBeenCalledOnceWith(namespace);
150+
expect(obj.prepareDataFromCustomerData).toHaveBeenCalledOnceWith({items: customerDataIds});
151+
expect(obj.idsMerger).toHaveBeenCalledOnceWith(localStorageIds, customerDataIds);
152+
});
153+
});
154+
describe('"idsMerger" method', function () {
155+
beforeEach(function () {
156+
obj.idsHandler = jasmine.createSpy();
157+
obj.filterIds = jasmine.createSpy().and.returnValue({});
158+
});
159+
it('check merge empty', function () {
160+
obj.idsMerger({}, {});
161+
162+
expect(obj.filterIds).not.toHaveBeenCalled();
163+
expect(obj.idsHandler).not.toHaveBeenCalled();
164+
});
165+
166+
it('check merge not empty', function () {
167+
var initialIds = obj.ids();
168+
169+
obj.idsMerger(localStorageIds, {});
170+
171+
expect(obj.filterIds).toHaveBeenCalledOnceWith(_.extend({}, initialIds, localStorageIds));
172+
expect(obj.idsHandler).toHaveBeenCalledOnceWith({});
173+
});
174+
});
175+
176+
describe('"prepareDataFromCustomerData" method', function () {
177+
it('argument is empty', function () {
178+
expect(obj.prepareDataFromCustomerData({})).toEqual({});
179+
});
180+
181+
it('argument is an object and has "items" property', function () {
182+
expect(obj.prepareDataFromCustomerData({items: {1: {}, 2: {}}})).toEqual({1: {}, 2: {}});
183+
});
184+
185+
it('argument is an object and does not have "items" property', function () {
186+
expect(obj.prepareDataFromCustomerData({1: {}, 2: {}})).toEqual({1: {}, 2: {}});
187+
});
188+
});
189+
190+
describe('"filterIds" method', function () {
191+
beforeEach(function () {
192+
window.checkout = {
193+
websiteId: 1
194+
};
195+
obj.idsStorage = {
196+
lifetime: 1000
197+
};
198+
});
199+
200+
it('filters out "out of scope" ids', function () {
201+
window.checkout.websiteId = 2;
202+
expect(obj.filterIds(localStorageIds)).toEqual({});
203+
});
204+
205+
it('filters out expired ids', function () {
206+
obj.idsStorage.lifetime = 70;
207+
expect(obj.filterIds(localStorageIds)).toEqual({2: localStorageIds['website-1-2']});
208+
});
209+
210+
it('filters out current product id', function () {
211+
productResolverIds.push(2);
212+
expect(obj.filterIds(localStorageIds)).toEqual({3: localStorageIds['website-1-3']});
213+
});
214+
});
215+
});
216+
});

dev/tests/js/jasmine/tests/app/code/Magento/Catalog/frontend/js/product/storage/data-storage.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,20 @@ define([
347347
expect(obj.hasIdsInSentRequest(ids)).toBe(false);
348348
});
349349

350-
it('check calls "hasIdsInSentRequest" with request data', function () {
350+
it('check calls "hasIdsInSentRequest" with request data #1', function () {
351351
expect(obj.hasIdsInSentRequest(ids)).toBe(true);
352352
});
353+
354+
it('check calls "hasIdsInSentRequest" with request data #2', function () {
355+
obj.request = {
356+
data: {
357+
'2': {
358+
data: 'value'
359+
}
360+
}
361+
};
362+
expect(obj.hasIdsInSentRequest(ids)).toBe(false);
363+
});
353364
});
354365
describe('"addDataFromPageCache" method', function () {
355366
beforeEach(function () {

0 commit comments

Comments
 (0)