Skip to content

Commit

Permalink
Cleanup, refactor and cover with tests section-config module
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Krzeszowiak committed Nov 26, 2019
1 parent 2bd4cb5 commit 976302d
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 22 deletions.
44 changes: 22 additions & 22 deletions app/code/Magento/Customer/view/frontend/web/js/section-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,65 @@
define(['underscore'], function (_) {
'use strict';

var baseUrls, sections, clientSideSections, sectionNames, canonize;
var baseUrls = [],
sections = [],
clientSideSections = [],
sectionNames = [],
canonize;

/**
* @param {String} url
* @return {String}
*/
canonize = function (url) {
var route = url,
key;
var route = url;

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

if (route != url) { //eslint-disable-line eqeqeq
break;
}
}
return route !== url;
});

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

return {
/**
* @param {String} url
* @return {Array}
* Returns a list of sections which should be invalidated for given URL.
* @param {String} url - URL which was requested.
* @return {Array} - List of sections to invalidate.
*/
getAffectedSections: function (url) {
var route = canonize(url),
actions = _.find(sections, function (val, section) {
var matched;

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

return matched && matched[0] == route; //eslint-disable-line eqeqeq
return matched && matched[0] === route;
}

return route.indexOf(section) === 0;
});

return _.union(_.toArray(actions), _.toArray(sections['*']));
return _.union(actions, sections['*']);
},

/**
* @param {*} allSections
* @return {*}
* Filters the list of given sections to the ones defined as client side.
* @param {Array} allSections - List of sections to check.
* @return {Array} - List of filtered sections.
*/
filterClientSideSections: function (allSections) {
if (Array.isArray(allSections)) {
return _.difference(allSections, clientSideSections);
}

return allSections;
return _.difference(allSections, clientSideSections);
},

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

/**
* Returns array of section names.
*
* @returns {Array}
*/
getSectionNames: function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/* eslint max-nested-callbacks: 0 */
define(['squire'], function (Squire) {
'use strict';

var injector = new Squire(),
obj;

beforeEach(function (done) {
// injector.mock(mocks);
injector.require(['Magento_Customer/js/section-config'], function (Constr) {
obj = Constr;
done();
});
});

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

describe('Magento_Customer/js/section-config', function () {
describe('"getAffectedSections" method', function () {
it('Does not throw before component is initialized.', function () {
expect(function () {
obj.getAffectedSections('http://localhost.com/path');
}).not.toThrow();
});

it('Returns proper sections when URL contains base URL.', function () {
obj['Magento_Customer/js/section-config']({
sections: {
'path': [
'section'
]
},
baseUrls: [
'http://localhost.com/',
'https://localhost.com/'
]
});

expect(obj.getAffectedSections('https://localhost.com/path')).toEqual(['section']);
});

it('Returns proper sections when glob pattern is used at the end.', function () {
obj['Magento_Customer/js/section-config']({
sections: {
'path/*': [
'section'
]
},
baseUrls: [
'http://localhost.com/',
'https://localhost.com/'
]
});

expect(obj.getAffectedSections('https://localhost.com/path/subpath')).toEqual(['section']);
});

it('Returns proper sections when glob pattern is used inside.', function () {
obj['Magento_Customer/js/section-config']({
sections: {
'*/subpath': [
'section'
]
},
baseUrls: [
'http://localhost.com/',
'https://localhost.com/'
]
});

expect(obj.getAffectedSections('https://localhost.com/path/subpath')).toEqual(['section']);
});

it('Strips "index.php" suffix from provided URL.', function () {
obj['Magento_Customer/js/section-config']({
sections: {
'path': [
'section'
]
},
baseUrls: [
'http://localhost.com/'
]
});

expect(obj.getAffectedSections('http://localhost.com/path/index.php')).toEqual(['section']);
});

it('Adds sections for all URLs "*" to found ones.', function () {
obj['Magento_Customer/js/section-config']({
sections: {
'path': [
'section'
],
'*': [
'all'
]
},
baseUrls: [
'http://localhost.com/'
]
});

expect(obj.getAffectedSections('http://localhost.com/path')).toEqual(['section', 'all']);
});

it('Returns "*" sections for all URLs.', function () {
obj['Magento_Customer/js/section-config']({
sections: {
'*': [
'all'
]
},
baseUrls: [
'http://localhost.com/'
]
});

expect(obj.getAffectedSections('http://localhost.com/path')).toEqual(['all']);
});
});

describe('"filterClientSideSections" method', function () {
it('Does not throw before component is initialized.', function () {
expect(function () {
obj.filterClientSideSections();
}).not.toThrow();
});

it('Returns empty array when all sections are client side.', function () {
var sections = ['test'];

obj['Magento_Customer/js/section-config']({
clientSideSections: sections
});
expect(obj.filterClientSideSections(sections)).toEqual([]);
});

it('Filters out client side sections.', function () {
var allSections = ['test', 'client'],
clientSections = ['client'];

obj['Magento_Customer/js/section-config']({
clientSideSections: clientSections
});
expect(obj.filterClientSideSections(allSections)).toEqual(['test']);
});
});

describe('"isClientSideSection" method', function () {
it('Does not throw before component is initialized.', function () {
expect(function () {
obj.isClientSideSection();
}).not.toThrow();
});

it('Returns true if section is defined as client side.', function () {
obj['Magento_Customer/js/section-config']({
clientSideSections: ['client']
});
expect(obj.isClientSideSection('client')).toBe(true);
});

it('Returns false if section is not defined as client side.', function () {
obj['Magento_Customer/js/section-config']({
clientSideSections: ['client']
});
expect(obj.isClientSideSection('test')).toBe(false);
});

it('Returns false if section is not client side and sections are not defined.', function () {
obj['Magento_Customer/js/section-config']({
clientSideSections: []
});
expect(obj.isClientSideSection('test')).toBe(false);
});
});

describe('"getSectionNames" method', function () {
it('Does not throw before component is initialized.', function () {
expect(function () {
obj.getSectionNames();
}).not.toThrow();
});

it('Returns defined section names.', function () {
var sections = ['test'];

obj['Magento_Customer/js/section-config']({
sectionNames: sections
});
expect(obj.getSectionNames()).toBe(sections);
});
});
});
});

0 comments on commit 976302d

Please sign in to comment.