From 976302de9187d12d9ed0b54e06de3ca8a6f37b24 Mon Sep 17 00:00:00 2001 From: Mateusz Krzeszowiak Date: Tue, 26 Nov 2019 19:45:43 +0100 Subject: [PATCH 1/2] Cleanup, refactor and cover with tests section-config module --- .../view/frontend/web/js/section-config.js | 44 ++-- .../frontend/js/section-config.test.js | 206 ++++++++++++++++++ 2 files changed, 228 insertions(+), 22 deletions(-) create mode 100644 dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js diff --git a/app/code/Magento/Customer/view/frontend/web/js/section-config.js b/app/code/Magento/Customer/view/frontend/web/js/section-config.js index d346d5b070729..60482e5fee260 100644 --- a/app/code/Magento/Customer/view/frontend/web/js/section-config.js +++ b/app/code/Magento/Customer/view/frontend/web/js/section-config.js @@ -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) { @@ -72,7 +73,6 @@ define(['underscore'], function (_) { /** * Returns array of section names. - * * @returns {Array} */ getSectionNames: function () { diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js new file mode 100644 index 0000000000000..56ed546e28f8c --- /dev/null +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js @@ -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); + }); + }); + }); +}); From 5be754b7d3de102cb9ec6ef2f43dbdbcb797540a Mon Sep 17 00:00:00 2001 From: Mateusz Krzeszowiak Date: Thu, 28 Nov 2019 17:08:19 +0100 Subject: [PATCH 2/2] Add failing test and fix it --- .../view/frontend/web/js/section-config.js | 2 +- .../Customer/frontend/js/section-config.test.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Customer/view/frontend/web/js/section-config.js b/app/code/Magento/Customer/view/frontend/web/js/section-config.js index 60482e5fee260..107a177d0832f 100644 --- a/app/code/Magento/Customer/view/frontend/web/js/section-config.js +++ b/app/code/Magento/Customer/view/frontend/web/js/section-config.js @@ -50,7 +50,7 @@ define(['underscore'], function (_) { return route.indexOf(section) === 0; }); - return _.union(actions, sections['*']); + return _.union(_.toArray(actions), sections['*']); }, /** diff --git a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js index 56ed546e28f8c..ef7222883b738 100644 --- a/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js +++ b/dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/section-config.test.js @@ -128,6 +128,21 @@ define(['squire'], function (Squire) { expect(obj.getAffectedSections('http://localhost.com/path')).toEqual(['all']); }); + + it('Ignores capitalization in parts of URL.', function () { + obj['Magento_Customer/js/section-config']({ + sections: { + 'path': [ + 'section' + ] + }, + baseUrls: [ + 'http://localhost.com/' + ] + }); + + expect(obj.getAffectedSections('http://localhost.com/PaTh')).toEqual(['section']); + }); }); describe('"filterClientSideSections" method', function () {