Skip to content

Commit accc877

Browse files
authored
ENGCOM-6666: Cleanup, refactor and cover with tests section-config module #25764
2 parents 3bd07a1 + 1ed9026 commit accc877

File tree

2 files changed

+243
-22
lines changed

2 files changed

+243
-22
lines changed

app/code/Magento/Customer/view/frontend/web/js/section-config.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,65 @@
66
define(['underscore'], function (_) {
77
'use strict';
88

9-
var baseUrls, sections, clientSideSections, sectionNames, canonize;
9+
var baseUrls = [],
10+
sections = [],
11+
clientSideSections = [],
12+
sectionNames = [],
13+
canonize;
1014

1115
/**
1216
* @param {String} url
1317
* @return {String}
1418
*/
1519
canonize = function (url) {
16-
var route = url,
17-
key;
20+
var route = url;
1821

19-
for (key in baseUrls) { //eslint-disable-line guard-for-in
20-
route = url.replace(baseUrls[key], '');
22+
_.some(baseUrls, function (baseUrl) {
23+
route = url.replace(baseUrl, '');
2124

22-
if (route != url) { //eslint-disable-line eqeqeq
23-
break;
24-
}
25-
}
25+
return route !== url;
26+
});
2627

2728
return route.replace(/^\/?index.php\/?/, '').toLowerCase();
2829
};
2930

3031
return {
3132
/**
32-
* @param {String} url
33-
* @return {Array}
33+
* Returns a list of sections which should be invalidated for given URL.
34+
* @param {String} url - URL which was requested.
35+
* @return {Array} - List of sections to invalidate.
3436
*/
3537
getAffectedSections: function (url) {
3638
var route = canonize(url),
3739
actions = _.find(sections, function (val, section) {
3840
var matched;
3941

42+
// Covers the case where "*" works as a glob pattern.
4043
if (section.indexOf('*') >= 0) {
4144
section = section.replace(/\*/g, '[^/]+') + '$';
4245
matched = route.match(section);
4346

44-
return matched && matched[0] == route; //eslint-disable-line eqeqeq
47+
return matched && matched[0] === route;
4548
}
4649

4750
return route.indexOf(section) === 0;
4851
});
4952

50-
return _.union(_.toArray(actions), _.toArray(sections['*']));
53+
return _.union(_.toArray(actions), sections['*']);
5154
},
5255

5356
/**
54-
* @param {*} allSections
55-
* @return {*}
57+
* Filters the list of given sections to the ones defined as client side.
58+
* @param {Array} allSections - List of sections to check.
59+
* @return {Array} - List of filtered sections.
5660
*/
5761
filterClientSideSections: function (allSections) {
58-
if (Array.isArray(allSections)) {
59-
return _.difference(allSections, clientSideSections);
60-
}
61-
62-
return allSections;
62+
return _.difference(allSections, clientSideSections);
6363
},
6464

6565
/**
66-
* @param {String} sectionName
66+
* Tells if section is defined as client side.
67+
* @param {String} sectionName - Name of the section to check.
6768
* @return {Boolean}
6869
*/
6970
isClientSideSection: function (sectionName) {
@@ -72,7 +73,6 @@ define(['underscore'], function (_) {
7273

7374
/**
7475
* Returns array of section names.
75-
*
7676
* @returns {Array}
7777
*/
7878
getSectionNames: function () {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint max-nested-callbacks: 0 */
7+
define(['squire'], function (Squire) {
8+
'use strict';
9+
10+
var injector = new Squire(),
11+
obj;
12+
13+
beforeEach(function (done) {
14+
// injector.mock(mocks);
15+
injector.require(['Magento_Customer/js/section-config'], function (Constr) {
16+
obj = Constr;
17+
done();
18+
});
19+
});
20+
21+
afterEach(function () {
22+
try {
23+
injector.clean();
24+
injector.remove();
25+
} catch (e) {}
26+
});
27+
28+
describe('Magento_Customer/js/section-config', function () {
29+
describe('"getAffectedSections" method', function () {
30+
it('Does not throw before component is initialized.', function () {
31+
expect(function () {
32+
obj.getAffectedSections('http://localhost.com/path');
33+
}).not.toThrow();
34+
});
35+
36+
it('Returns proper sections when URL contains base URL.', function () {
37+
obj['Magento_Customer/js/section-config']({
38+
sections: {
39+
'path': [
40+
'section'
41+
]
42+
},
43+
baseUrls: [
44+
'http://localhost.com/',
45+
'https://localhost.com/'
46+
]
47+
});
48+
49+
expect(obj.getAffectedSections('https://localhost.com/path')).toEqual(['section']);
50+
});
51+
52+
it('Returns proper sections when glob pattern is used at the end.', function () {
53+
obj['Magento_Customer/js/section-config']({
54+
sections: {
55+
'path/*': [
56+
'section'
57+
]
58+
},
59+
baseUrls: [
60+
'http://localhost.com/',
61+
'https://localhost.com/'
62+
]
63+
});
64+
65+
expect(obj.getAffectedSections('https://localhost.com/path/subpath')).toEqual(['section']);
66+
});
67+
68+
it('Returns proper sections when glob pattern is used inside.', function () {
69+
obj['Magento_Customer/js/section-config']({
70+
sections: {
71+
'*/subpath': [
72+
'section'
73+
]
74+
},
75+
baseUrls: [
76+
'http://localhost.com/',
77+
'https://localhost.com/'
78+
]
79+
});
80+
81+
expect(obj.getAffectedSections('https://localhost.com/path/subpath')).toEqual(['section']);
82+
});
83+
84+
it('Strips "index.php" suffix from provided URL.', function () {
85+
obj['Magento_Customer/js/section-config']({
86+
sections: {
87+
'path': [
88+
'section'
89+
]
90+
},
91+
baseUrls: [
92+
'http://localhost.com/'
93+
]
94+
});
95+
96+
expect(obj.getAffectedSections('http://localhost.com/path/index.php')).toEqual(['section']);
97+
});
98+
99+
it('Adds sections for all URLs "*" to found ones.', function () {
100+
obj['Magento_Customer/js/section-config']({
101+
sections: {
102+
'path': [
103+
'section'
104+
],
105+
'*': [
106+
'all'
107+
]
108+
},
109+
baseUrls: [
110+
'http://localhost.com/'
111+
]
112+
});
113+
114+
expect(obj.getAffectedSections('http://localhost.com/path')).toEqual(['section', 'all']);
115+
});
116+
117+
it('Returns "*" sections for all URLs.', function () {
118+
obj['Magento_Customer/js/section-config']({
119+
sections: {
120+
'*': [
121+
'all'
122+
]
123+
},
124+
baseUrls: [
125+
'http://localhost.com/'
126+
]
127+
});
128+
129+
expect(obj.getAffectedSections('http://localhost.com/path')).toEqual(['all']);
130+
});
131+
132+
it('Ignores capitalization in parts of URL.', function () {
133+
obj['Magento_Customer/js/section-config']({
134+
sections: {
135+
'path': [
136+
'section'
137+
]
138+
},
139+
baseUrls: [
140+
'http://localhost.com/'
141+
]
142+
});
143+
144+
expect(obj.getAffectedSections('http://localhost.com/PaTh')).toEqual(['section']);
145+
});
146+
});
147+
148+
describe('"filterClientSideSections" method', function () {
149+
it('Does not throw before component is initialized.', function () {
150+
expect(function () {
151+
obj.filterClientSideSections();
152+
}).not.toThrow();
153+
});
154+
155+
it('Returns empty array when all sections are client side.', function () {
156+
var sections = ['test'];
157+
158+
obj['Magento_Customer/js/section-config']({
159+
clientSideSections: sections
160+
});
161+
expect(obj.filterClientSideSections(sections)).toEqual([]);
162+
});
163+
164+
it('Filters out client side sections.', function () {
165+
var allSections = ['test', 'client'],
166+
clientSections = ['client'];
167+
168+
obj['Magento_Customer/js/section-config']({
169+
clientSideSections: clientSections
170+
});
171+
expect(obj.filterClientSideSections(allSections)).toEqual(['test']);
172+
});
173+
});
174+
175+
describe('"isClientSideSection" method', function () {
176+
it('Does not throw before component is initialized.', function () {
177+
expect(function () {
178+
obj.isClientSideSection();
179+
}).not.toThrow();
180+
});
181+
182+
it('Returns true if section is defined as client side.', function () {
183+
obj['Magento_Customer/js/section-config']({
184+
clientSideSections: ['client']
185+
});
186+
expect(obj.isClientSideSection('client')).toBe(true);
187+
});
188+
189+
it('Returns false if section is not defined as client side.', function () {
190+
obj['Magento_Customer/js/section-config']({
191+
clientSideSections: ['client']
192+
});
193+
expect(obj.isClientSideSection('test')).toBe(false);
194+
});
195+
196+
it('Returns false if section is not client side and sections are not defined.', function () {
197+
obj['Magento_Customer/js/section-config']({
198+
clientSideSections: []
199+
});
200+
expect(obj.isClientSideSection('test')).toBe(false);
201+
});
202+
});
203+
204+
describe('"getSectionNames" method', function () {
205+
it('Does not throw before component is initialized.', function () {
206+
expect(function () {
207+
obj.getSectionNames();
208+
}).not.toThrow();
209+
});
210+
211+
it('Returns defined section names.', function () {
212+
var sections = ['test'];
213+
214+
obj['Magento_Customer/js/section-config']({
215+
sectionNames: sections
216+
});
217+
expect(obj.getSectionNames()).toBe(sections);
218+
});
219+
});
220+
});
221+
});

0 commit comments

Comments
 (0)