4
4
*/
5
5
6
6
/* eslint max-nested-callbacks: 0 */
7
+ // jscs:disable jsDoc
8
+
7
9
require . config ( {
8
10
paths : {
9
11
'mixins' : 'mage/requirejs/mixins'
@@ -13,136 +15,173 @@ require.config({
13
15
define ( [ 'rjsResolver' , 'mixins' ] , function ( resolver , mixins ) {
14
16
'use strict' ;
15
17
16
- var context = {
17
- config : { }
18
- } ;
19
-
20
18
describe ( 'mixins module' , function ( ) {
21
19
beforeEach ( function ( done ) {
20
+ spyOn ( mixins , 'hasMixins' ) . and . callThrough ( ) ;
21
+ spyOn ( mixins , 'getMixins' ) . and . callThrough ( ) ;
22
+ spyOn ( mixins , 'load' ) . and . callThrough ( ) ;
23
+
22
24
// Wait for all modules to be loaded so they don't interfere with testing.
23
25
resolver ( function ( ) {
24
26
done ( ) ;
25
27
} ) ;
26
28
} ) ;
27
29
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' ;
33
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' ;
34
+ mixins . hasMixins . and . returnValue ( false ) ;
37
35
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' ;
40
45
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
+ } ;
47
48
} ) ;
48
49
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' ) ;
51
52
52
- expect ( mixins . processNames ( 'plugin!module' , context ) ) . toBe ( 'plugin!module' ) ;
53
+ done ( ) ;
53
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' ;
54
60
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
+ } ) ;
57
71
58
- expect ( mixins . processNames ( 'module' , context ) ) . toBe ( 'module' ) ;
72
+ define ( name , [ ] , function ( ) {
73
+ return {
74
+ value : 'original'
75
+ } ;
59
76
} ) ;
60
77
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' ;
63
81
64
- expect ( mixins . processNames ( [ 'module' ] , context ) ) . toEqual ( [ 'module' ] ) ;
82
+ return module ;
83
+ } ;
65
84
} ) ;
66
85
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' ) ;
69
88
70
- expect ( mixins . processNames ( 'module' , context ) ) . toBe ( 'mixins!module' ) ;
89
+ done ( ) ;
71
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' ;
72
96
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 ] ) ;
75
99
76
- expect ( mixins . processNames ( './module' , context ) ) . toBe ( 'mixins!./module' ) ;
100
+ define ( name , [ ] , function ( ) {
101
+ return {
102
+ value : 'original'
103
+ } ;
77
104
} ) ;
78
105
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' ;
81
109
82
- expect ( mixins . processNames ( [ './module' ] , context ) ) . toEqual ( [ 'mixins!./module' ] ) ;
110
+ return module ;
111
+ } ;
83
112
} ) ;
84
113
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' ) ;
87
116
88
- expect ( mixins . processNames ( [ 'module' ] , context ) ) . toEqual ( [ 'mixins!module' ] ) ;
117
+ done ( ) ;
89
118
} ) ;
90
119
} ) ;
91
120
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' ;
98
125
99
- define ( name , [ ] , function ( ) { } ) ;
126
+ mixins . hasMixins . and . returnValue ( true ) ;
127
+ mixins . getMixins . and . returnValue ( [ mixinName ] ) ;
100
128
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
+ } ;
105
133
} ) ;
106
134
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' ) ;
109
137
110
- spyOn ( mixins , 'hasMixins' ) . and . returnValue ( true ) ;
111
- spyOn ( mixins , 'load' ) . and . callThrough ( ) ;
138
+ done ( ) ;
112
139
113
- define ( name , [ ] , function ( ) { } ) ;
140
+ return { } ;
141
+ } ) ;
114
142
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
+ } ;
119
149
} ) ;
120
150
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
+ } ) ;
124
153
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' ;
128
158
129
- define ( name , [ ] , function ( ) {
130
- return { value : 'original' } ;
131
- } ) ;
159
+ mixins . hasMixins . and . returnValue ( true ) ;
160
+ mixins . getMixins . and . returnValue ( [ mixinName ] ) ;
132
161
133
- define ( mixinName , [ ] , function ( ) {
134
- return function ( module ) {
135
- module . value = 'changed' ;
162
+ define ( dependencyName , [ ] , function ( ) {
163
+ return {
164
+ value : 'original'
165
+ } ;
166
+ } ) ;
136
167
137
- return module ;
138
- } ;
139
- } ) ;
168
+ define ( name , [ './relative-module' ] , function ( module ) {
169
+ expect ( module . value ) . toBe ( 'changed' ) ;
140
170
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
+ } ;
145
182
} ) ;
183
+
184
+ require ( [ name ] , function ( ) { } ) ;
146
185
} ) ;
147
186
} ) ;
148
187
} ) ;
0 commit comments