@@ -2,27 +2,43 @@ import path from 'path';
2
2
const loader = require ( '../index' ) . default ;
3
3
const { operator } = require ( '../index' ) ;
4
4
5
- describe ( 'js-to-sass -vars-loader' , ( ) => {
5
+ describe ( 'js-to-styles -vars-loader' , ( ) => {
6
6
7
7
describe ( 'module' , ( ) => {
8
8
const context = {
9
- context : path . resolve ( )
9
+ context : path . resolve ( ) ,
10
+ _module : {
11
+ resource : 'fakeResource.scss'
12
+ }
10
13
} ;
11
14
12
15
it ( 'exports a function' , ( ) => {
13
16
expect ( typeof loader ) . toEqual ( 'function' ) ;
14
17
} ) ;
15
18
16
- it ( 'calls operator.mergeVarsToContent with content and loader context' , ( ) => {
19
+ it ( 'calls getResource with context' , ( ) => {
20
+ spyOn ( operator , 'getResource' ) . and . callThrough ( ) ;
21
+ loader . call ( context , 'asdf' ) ;
22
+ expect ( operator . getResource ) . toHaveBeenCalledWith ( context ) ;
23
+
24
+ } ) ;
25
+
26
+ it ( 'calls getPreprocessorType with resource' , ( ) => {
27
+ spyOn ( operator , 'getPreprocessorType' ) ;
28
+ loader . call ( context , 'asdf' ) ;
29
+ expect ( operator . getPreprocessorType ) . toHaveBeenCalledWith ( { resource : context . _module . resource } ) ;
30
+ } ) ;
31
+
32
+ it ( 'calls operator.mergeVarsToContent with content and loader context, and preprocessor type' , ( ) => {
17
33
spyOn ( operator , 'mergeVarsToContent' ) ;
18
34
loader . call ( context , 'asdf' ) ;
19
- expect ( operator . mergeVarsToContent ) . toHaveBeenCalledWith ( 'asdf' , context ) ;
35
+ expect ( operator . mergeVarsToContent ) . toHaveBeenCalledWith ( 'asdf' , context , 'sass' ) ;
20
36
21
37
} ) ;
22
38
23
39
it ( 'returns the result of mergeVarsToContent' , ( ) => {
24
40
const content = 'require("./mocks/colors.js")\n' + '.someClass {\ncolor: $nice;\n}' ;
25
- const merged = operator . mergeVarsToContent ( content , context ) ;
41
+ const merged = operator . mergeVarsToContent ( content , context , 'sass' ) ;
26
42
const result = loader . call ( context , content ) ;
27
43
expect ( result ) . toEqual ( merged ) ;
28
44
} ) ;
@@ -92,25 +108,73 @@ describe('js-to-sass-vars-loader', () => {
92
108
} ) ;
93
109
} ) ;
94
110
111
+ describe ( 'transformToLessVars' , ( ) => {
112
+ it ( 'takes a hash object and transforms it to less variables' , ( ) => {
113
+ const colors = require ( '../mocks/colors.js' ) ;
114
+ expect ( operator . transformToLessVars ( colors ) ) . toEqual ( '@white: #fff;\n@black: #000;\n' ) ;
115
+ } ) ;
116
+ } ) ;
117
+
95
118
describe ( 'mergeVarsToContent' , ( ) => {
96
119
const context = {
97
120
context : path . resolve ( )
98
121
} ;
99
122
100
- it ( 'inserst vars to sass content' , ( ) => {
123
+ it ( 'inserts vars to styles content' , ( ) => {
101
124
const content = "require('./mocks/colors.js');\n" +
102
125
".someClass { color: #fff;}" ;
103
- const [ moduleData , sassContent ] = operator . divideContent ( content ) ;
126
+ const [ moduleData , stylesContent ] = operator . divideContent ( content ) ;
104
127
const modulePath = operator . getModulePath ( moduleData ) ;
105
128
const varData = operator . getVarData ( modulePath , context ) ;
106
- const sassVars = operator . transformToSassVars ( varData ) ;
129
+ const vars = operator . transformToStyleVars ( varData ) ;
107
130
108
- expect ( operator . mergeVarsToContent ( content , context ) ) . toEqual ( sassVars + sassContent ) ;
131
+ expect ( operator . mergeVarsToContent ( content , context ) ) . toEqual ( vars + stylesContent ) ;
109
132
} ) ;
110
133
111
134
it ( 'gives back content as is if there is no requre' , ( ) => {
112
135
const content = ".someClass { color: #fff;}" ;
113
136
expect ( operator . mergeVarsToContent ( content , context ) ) . toEqual ( content ) ;
114
137
} ) ;
115
138
} ) ;
139
+
140
+ describe ( 'getResource' , ( ) => {
141
+ it ( 'gets module.resource' , ( ) => {
142
+ const context = {
143
+ _module : {
144
+ resource : 'fakeResource'
145
+ }
146
+ } ;
147
+
148
+ expect ( operator . getResource ( context ) ) . toEqual ( context . _module . resource ) ;
149
+ } ) ;
150
+ } ) ;
151
+
152
+ describe ( 'getPreprocessorType' , ( ) => {
153
+ it ( 'should recognise sass resource' , ( ) => {
154
+ expect ( operator . getPreprocessorType ( { resource : '/path/to/resource.scss' } ) ) . toEqual ( 'sass' ) ;
155
+ } ) ;
156
+
157
+ it ( 'should recognise less resource' , ( ) => {
158
+ expect ( operator . getPreprocessorType ( { resource : '/path/to/resource.less' } ) ) . toEqual ( 'less' ) ;
159
+ } ) ;
160
+
161
+ it ( 'throw error if proprocessor type is unknown' , ( ) => {
162
+ const caller = ( ) => {
163
+ operator . getPreprocessorType ( { resource : 'unknown.extension' } ) ;
164
+ } ;
165
+ expect ( caller ) . toThrow ( ) ;
166
+ } ) ;
167
+ } ) ;
168
+
169
+ describe ( 'transformToStyleVars' , ( ) => {
170
+ it ( 'calls the proper transformer by type' , ( ) => {
171
+ spyOn ( operator , 'transformToSassVars' ) ;
172
+ spyOn ( operator , 'transformToLessVars' ) ;
173
+ operator . transformToStyleVars ( { type : 'sass' , varData : { } } ) ;
174
+ expect ( operator . transformToSassVars ) . toHaveBeenCalled ( ) ;
175
+
176
+ operator . transformToStyleVars ( { type : 'less' , varData : { } } ) ;
177
+ expect ( operator . transformToLessVars ) . toHaveBeenCalled ( ) ;
178
+ } ) ;
179
+ } ) ;
116
180
} ) ;
0 commit comments