|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint max-nested-callbacks: 0 */ |
| 7 | +// jscs:disable jsDoc |
| 8 | + |
| 9 | +require.config({ |
| 10 | + paths: { |
| 11 | + 'mixins': 'mage/requirejs/mixins' |
| 12 | + } |
| 13 | +}); |
| 14 | + |
| 15 | +define(['rjsResolver', 'mixins'], function (resolver, mixins) { |
| 16 | + 'use strict'; |
| 17 | + |
| 18 | + describe('mixins module', function () { |
| 19 | + beforeEach(function (done) { |
| 20 | + spyOn(mixins, 'hasMixins').and.callThrough(); |
| 21 | + spyOn(mixins, 'getMixins').and.callThrough(); |
| 22 | + spyOn(mixins, 'load').and.callThrough(); |
| 23 | + |
| 24 | + // Wait for all modules to be loaded so they don't interfere with testing. |
| 25 | + resolver(function () { |
| 26 | + done(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('does not affect modules without mixins', function (done) { |
| 31 | + var name = 'tests/assets/mixins/no-mixins', |
| 32 | + mixinName = 'tests/assets/mixins/no-mixins-ext'; |
| 33 | + |
| 34 | + mixins.hasMixins.and.returnValue(false); |
| 35 | + |
| 36 | + define(name, [], function () { |
| 37 | + return { |
| 38 | + value: 'original' |
| 39 | + }; |
| 40 | + }); |
| 41 | + |
| 42 | + define(mixinName, [], function () { |
| 43 | + return function (module) { |
| 44 | + module.value = 'changed'; |
| 45 | + |
| 46 | + return module; |
| 47 | + }; |
| 48 | + }); |
| 49 | + |
| 50 | + require([name], function (module) { |
| 51 | + expect(module.value).toBe('original'); |
| 52 | + |
| 53 | + done(); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('does not affect modules that are loaded with plugins', function (done) { |
| 58 | + var name = 'plugin!tests/assets/mixins/no-mixins', |
| 59 | + mixinName = 'tests/assets/mixins/no-mixins-ext'; |
| 60 | + |
| 61 | + mixins.hasMixins.and.returnValue(true); |
| 62 | + mixins.getMixins.and.returnValue([mixinName]); |
| 63 | + |
| 64 | + define('plugin', [], function () { |
| 65 | + return { |
| 66 | + load: function (module, req, onLoad) { |
| 67 | + req(module, onLoad); |
| 68 | + } |
| 69 | + }; |
| 70 | + }); |
| 71 | + |
| 72 | + define(name, [], function () { |
| 73 | + return { |
| 74 | + value: 'original' |
| 75 | + }; |
| 76 | + }); |
| 77 | + |
| 78 | + define(mixinName, [], function () { |
| 79 | + return function (module) { |
| 80 | + module.value = 'changed'; |
| 81 | + |
| 82 | + return module; |
| 83 | + }; |
| 84 | + }); |
| 85 | + |
| 86 | + require([name], function (module) { |
| 87 | + expect(module.value).toBe('original'); |
| 88 | + |
| 89 | + done(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('applies mixins for normal module with mixins', function (done) { |
| 94 | + var name = 'tests/assets/mixins/mixins-applied', |
| 95 | + mixinName = 'tests/assets/mixins/mixins-applied-ext'; |
| 96 | + |
| 97 | + mixins.hasMixins.and.returnValue(true); |
| 98 | + mixins.getMixins.and.returnValue([mixinName]); |
| 99 | + |
| 100 | + define(name, [], function () { |
| 101 | + return { |
| 102 | + value: 'original' |
| 103 | + }; |
| 104 | + }); |
| 105 | + |
| 106 | + define(mixinName, [], function () { |
| 107 | + return function (module) { |
| 108 | + module.value = 'changed'; |
| 109 | + |
| 110 | + return module; |
| 111 | + }; |
| 112 | + }); |
| 113 | + |
| 114 | + require([name], function (module) { |
| 115 | + expect(module.value).toBe('changed'); |
| 116 | + |
| 117 | + done(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('applies mixins for module that is a dependency', function (done) { |
| 122 | + var name = 'tests/assets/mixins/module-with-dependency', |
| 123 | + dependencyName = 'tests/assets/mixins/dependency-module', |
| 124 | + mixinName = 'tests/assets/mixins/dependency-module-ext'; |
| 125 | + |
| 126 | + mixins.hasMixins.and.returnValue(true); |
| 127 | + mixins.getMixins.and.returnValue([mixinName]); |
| 128 | + |
| 129 | + define(dependencyName, [], function () { |
| 130 | + return { |
| 131 | + value: 'original' |
| 132 | + }; |
| 133 | + }); |
| 134 | + |
| 135 | + define(name, [dependencyName], function (module) { |
| 136 | + expect(module.value).toBe('changed'); |
| 137 | + |
| 138 | + done(); |
| 139 | + |
| 140 | + return {}; |
| 141 | + }); |
| 142 | + |
| 143 | + define(mixinName, [], function () { |
| 144 | + return function (module) { |
| 145 | + module.value = 'changed'; |
| 146 | + |
| 147 | + return module; |
| 148 | + }; |
| 149 | + }); |
| 150 | + |
| 151 | + require([name], function () {}); |
| 152 | + }); |
| 153 | + |
| 154 | + it('applies mixins for module that is a relative dependency', function (done) { |
| 155 | + var name = 'tests/assets/mixins/module-with-relative-dependency', |
| 156 | + dependencyName = 'tests/assets/mixins/relative-module', |
| 157 | + mixinName = 'tests/assets/mixins/relative-module-ext'; |
| 158 | + |
| 159 | + mixins.hasMixins.and.returnValue(true); |
| 160 | + mixins.getMixins.and.returnValue([mixinName]); |
| 161 | + |
| 162 | + define(dependencyName, [], function () { |
| 163 | + return { |
| 164 | + value: 'original' |
| 165 | + }; |
| 166 | + }); |
| 167 | + |
| 168 | + define(name, ['./relative-module'], function (module) { |
| 169 | + expect(module.value).toBe('changed'); |
| 170 | + |
| 171 | + done(); |
| 172 | + |
| 173 | + return {}; |
| 174 | + }); |
| 175 | + |
| 176 | + define(mixinName, [], function () { |
| 177 | + return function (module) { |
| 178 | + module.value = 'changed'; |
| 179 | + |
| 180 | + return module; |
| 181 | + }; |
| 182 | + }); |
| 183 | + |
| 184 | + require([name], function () {}); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments