Skip to content

Commit 21620c6

Browse files
author
Mateusz Krzeszowiak
committed
Add tests
1 parent 540b134 commit 21620c6

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* eslint max-nested-callbacks: 0 */
7+
require.config({
8+
paths: {
9+
'mixins': 'mage/requirejs/mixins'
10+
}
11+
});
12+
13+
define(['rjsResolver', 'mixins'], function (resolver, mixins) {
14+
'use strict';
15+
16+
var context = {
17+
config: {}
18+
};
19+
20+
describe('mixins module', function () {
21+
beforeEach(function (done) {
22+
// Wait for all modules to be loaded so they don't interfere with testing.
23+
resolver(function () {
24+
done();
25+
});
26+
});
27+
28+
describe('processNames method', function () {
29+
beforeEach(function () {
30+
spyOn(mixins, 'processNames').and.callThrough();
31+
spyOn(mixins, 'hasMixins').and.callThrough();
32+
});
33+
34+
it('gets called when module is both required and defined', function (done) {
35+
var name = 'tests/assets/mixins/defined-module',
36+
dependencyName = 'tests/assets/mixins/defined-module-dependency';
37+
38+
define(dependencyName, [], function () {});
39+
define(name, [dependencyName], function () {});
40+
41+
require([name], function () {
42+
expect(mixins.processNames.calls.argsFor(0)[0]).toEqual([]);
43+
expect(mixins.processNames.calls.argsFor(1)[0]).toEqual([dependencyName]);
44+
expect(mixins.processNames.calls.argsFor(2)[0]).toEqual([name]);
45+
done();
46+
});
47+
});
48+
49+
it('keeps name intact when it already contains another plugin', function () {
50+
mixins.hasMixins.and.returnValue(true);
51+
52+
expect(mixins.processNames('plugin!module', context)).toBe('plugin!module');
53+
});
54+
55+
it('keeps name intact when it has no mixins', function () {
56+
mixins.hasMixins.and.returnValue(false);
57+
58+
expect(mixins.processNames('module', context)).toBe('module');
59+
});
60+
61+
it('keeps names intact when they have no mixins', function () {
62+
mixins.hasMixins.and.returnValue(false);
63+
64+
expect(mixins.processNames(['module'], context)).toEqual(['module']);
65+
});
66+
67+
it('adds prefix to name when it has mixins', function () {
68+
mixins.hasMixins.and.returnValue(true);
69+
70+
expect(mixins.processNames('module', context)).toBe('mixins!module');
71+
});
72+
73+
it('adds prefix to name when it contains a relative path', function () {
74+
mixins.hasMixins.and.returnValue(false);
75+
76+
expect(mixins.processNames('./module', context)).toBe('mixins!./module');
77+
});
78+
79+
it('adds prefix to names when they contain a relative path', function () {
80+
mixins.hasMixins.and.returnValue(false);
81+
82+
expect(mixins.processNames(['./module'], context)).toEqual(['mixins!./module']);
83+
});
84+
85+
it('adds prefix to names when they have mixins', function () {
86+
mixins.hasMixins.and.returnValue(true);
87+
88+
expect(mixins.processNames(['module'], context)).toEqual(['mixins!module']);
89+
});
90+
});
91+
92+
describe('load method', function () {
93+
it('is not called when module has mixins', function (done) {
94+
var name = 'tests/assets/mixins/load-not-called';
95+
96+
spyOn(mixins, 'hasMixins').and.returnValue(false);
97+
spyOn(mixins, 'load').and.callThrough();
98+
99+
define(name, [], function () {});
100+
101+
require([name], function () {
102+
expect(mixins.load.calls.any()).toBe(false);
103+
done();
104+
});
105+
});
106+
107+
it('is called when module has mixins', function (done) {
108+
var name = 'tests/assets/mixins/load-called';
109+
110+
spyOn(mixins, 'hasMixins').and.returnValue(true);
111+
spyOn(mixins, 'load').and.callThrough();
112+
113+
define(name, [], function () {});
114+
115+
require([name], function () {
116+
expect(mixins.load.calls.mostRecent().args[0]).toEqual(name);
117+
done();
118+
});
119+
});
120+
121+
it('applies mixins for loaded module', function (done) {
122+
var name = 'tests/assets/mixins/mixins-applied',
123+
mixinName = 'tests/assets/mixins/mixins-applied-ext';
124+
125+
spyOn(mixins, 'hasMixins').and.returnValue(true);
126+
spyOn(mixins, 'load').and.callThrough();
127+
spyOn(mixins, 'getMixins').and.returnValue([mixinName]);
128+
129+
define(name, [], function () {
130+
return { value: 'original' };
131+
});
132+
133+
define(mixinName, [], function () {
134+
return function(module) {
135+
module.value = 'changed';
136+
137+
return module;
138+
};
139+
});
140+
141+
require([name], function (module) {
142+
expect(module.value).toBe('changed');
143+
done();
144+
});
145+
});
146+
});
147+
});
148+
});

lib/web/mage/requirejs/mixins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ define('mixins', [
7171
* from a module name ignoring the fact that it may be bundled.
7272
*
7373
* @param {String} name - Name, path or alias of a module.
74-
* @param {Object} config - Context's configuartion.
74+
* @param {Object} config - Context's configuration.
7575
* @returns {String}
7676
*/
7777
function getPath(name, config) {

0 commit comments

Comments
 (0)