|
| 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