Skip to content

Commit 8eb66b5

Browse files
author
Mateusz Krzeszowiak
committed
Test more of a public api then internal behaviour
1 parent 21620c6 commit 8eb66b5

File tree

1 file changed

+119
-80
lines changed

1 file changed

+119
-80
lines changed

dev/tests/js/jasmine/tests/lib/mage/requirejs/mixins.test.js

Lines changed: 119 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
/* eslint max-nested-callbacks: 0 */
7+
// jscs:disable jsDoc
8+
79
require.config({
810
paths: {
911
'mixins': 'mage/requirejs/mixins'
@@ -13,136 +15,173 @@ require.config({
1315
define(['rjsResolver', 'mixins'], function (resolver, mixins) {
1416
'use strict';
1517

16-
var context = {
17-
config: {}
18-
};
19-
2018
describe('mixins module', function () {
2119
beforeEach(function (done) {
20+
spyOn(mixins, 'hasMixins').and.callThrough();
21+
spyOn(mixins, 'getMixins').and.callThrough();
22+
spyOn(mixins, 'load').and.callThrough();
23+
2224
// Wait for all modules to be loaded so they don't interfere with testing.
2325
resolver(function () {
2426
done();
2527
});
2628
});
2729

28-
describe('processNames method', function () {
29-
beforeEach(function () {
30-
spyOn(mixins, 'processNames').and.callThrough();
31-
spyOn(mixins, 'hasMixins').and.callThrough();
32-
});
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';
3333

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';
34+
mixins.hasMixins.and.returnValue(false);
3735

38-
define(dependencyName, [], function () {});
39-
define(name, [dependencyName], function () {});
36+
define(name, [], function () {
37+
return {
38+
value: 'original'
39+
};
40+
});
41+
42+
define(mixinName, [], function () {
43+
return function (module) {
44+
module.value = 'changed';
4045

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-
});
46+
return module;
47+
};
4748
});
4849

49-
it('keeps name intact when it already contains another plugin', function () {
50-
mixins.hasMixins.and.returnValue(true);
50+
require([name], function (module) {
51+
expect(module.value).toBe('original');
5152

52-
expect(mixins.processNames('plugin!module', context)).toBe('plugin!module');
53+
done();
5354
});
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';
5460

55-
it('keeps name intact when it has no mixins', function () {
56-
mixins.hasMixins.and.returnValue(false);
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+
});
5771

58-
expect(mixins.processNames('module', context)).toBe('module');
72+
define(name, [], function () {
73+
return {
74+
value: 'original'
75+
};
5976
});
6077

61-
it('keeps names intact when they have no mixins', function () {
62-
mixins.hasMixins.and.returnValue(false);
78+
define(mixinName, [], function () {
79+
return function (module) {
80+
module.value = 'changed';
6381

64-
expect(mixins.processNames(['module'], context)).toEqual(['module']);
82+
return module;
83+
};
6584
});
6685

67-
it('adds prefix to name when it has mixins', function () {
68-
mixins.hasMixins.and.returnValue(true);
86+
require([name], function (module) {
87+
expect(module.value).toBe('original');
6988

70-
expect(mixins.processNames('module', context)).toBe('mixins!module');
89+
done();
7190
});
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';
7296

73-
it('adds prefix to name when it contains a relative path', function () {
74-
mixins.hasMixins.and.returnValue(false);
97+
mixins.hasMixins.and.returnValue(true);
98+
mixins.getMixins.and.returnValue([mixinName]);
7599

76-
expect(mixins.processNames('./module', context)).toBe('mixins!./module');
100+
define(name, [], function () {
101+
return {
102+
value: 'original'
103+
};
77104
});
78105

79-
it('adds prefix to names when they contain a relative path', function () {
80-
mixins.hasMixins.and.returnValue(false);
106+
define(mixinName, [], function () {
107+
return function (module) {
108+
module.value = 'changed';
81109

82-
expect(mixins.processNames(['./module'], context)).toEqual(['mixins!./module']);
110+
return module;
111+
};
83112
});
84113

85-
it('adds prefix to names when they have mixins', function () {
86-
mixins.hasMixins.and.returnValue(true);
114+
require([name], function (module) {
115+
expect(module.value).toBe('changed');
87116

88-
expect(mixins.processNames(['module'], context)).toEqual(['mixins!module']);
117+
done();
89118
});
90119
});
91120

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();
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';
98125

99-
define(name, [], function () {});
126+
mixins.hasMixins.and.returnValue(true);
127+
mixins.getMixins.and.returnValue([mixinName]);
100128

101-
require([name], function () {
102-
expect(mixins.load.calls.any()).toBe(false);
103-
done();
104-
});
129+
define(dependencyName, [], function () {
130+
return {
131+
value: 'original'
132+
};
105133
});
106134

107-
it('is called when module has mixins', function (done) {
108-
var name = 'tests/assets/mixins/load-called';
135+
define(name, [dependencyName], function (module) {
136+
expect(module.value).toBe('changed');
109137

110-
spyOn(mixins, 'hasMixins').and.returnValue(true);
111-
spyOn(mixins, 'load').and.callThrough();
138+
done();
112139

113-
define(name, [], function () {});
140+
return {};
141+
});
114142

115-
require([name], function () {
116-
expect(mixins.load.calls.mostRecent().args[0]).toEqual(name);
117-
done();
118-
});
143+
define(mixinName, [], function () {
144+
return function (module) {
145+
module.value = 'changed';
146+
147+
return module;
148+
};
119149
});
120150

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';
151+
require([name], function () {});
152+
});
124153

125-
spyOn(mixins, 'hasMixins').and.returnValue(true);
126-
spyOn(mixins, 'load').and.callThrough();
127-
spyOn(mixins, 'getMixins').and.returnValue([mixinName]);
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';
128158

129-
define(name, [], function () {
130-
return { value: 'original' };
131-
});
159+
mixins.hasMixins.and.returnValue(true);
160+
mixins.getMixins.and.returnValue([mixinName]);
132161

133-
define(mixinName, [], function () {
134-
return function(module) {
135-
module.value = 'changed';
162+
define(dependencyName, [], function () {
163+
return {
164+
value: 'original'
165+
};
166+
});
136167

137-
return module;
138-
};
139-
});
168+
define(name, ['./relative-module'], function (module) {
169+
expect(module.value).toBe('changed');
140170

141-
require([name], function (module) {
142-
expect(module.value).toBe('changed');
143-
done();
144-
});
171+
done();
172+
173+
return {};
174+
});
175+
176+
define(mixinName, [], function () {
177+
return function (module) {
178+
module.value = 'changed';
179+
180+
return module;
181+
};
145182
});
183+
184+
require([name], function () {});
146185
});
147186
});
148187
});

0 commit comments

Comments
 (0)