11import path from 'path' ;
2+ import fs from 'fs' ;
23const loader = require ( '../index' ) . default ;
34jest . mock ( 'decache' ) ;
45const { operator } = require ( '../index' ) ;
@@ -53,43 +54,7 @@ describe('js-to-styles-vars-loader', () => {
5354 } ) ;
5455 } ) ;
5556
56- describe ( 'divideContent' , ( ) => {
57- it ( 'divides the require (if it exists) from the content' , ( ) => {
58- const content = "require('colors.js');\n" +
59- ".someClass { color: #fff;}" ;
60- expect ( operator . divideContent ( content ) [ 0 ] ) . toEqual ( "require('colors.js');" ) ;
61- expect ( operator . divideContent ( content ) [ 1 ] ) . toEqual ( "\n.someClass { color: #fff;}" ) ;
62- } ) ;
63-
64- it ( 'gives back content if there is no require in content' , ( ) => {
65- const content = ".someClass { color: #fff;}" ;
66- expect ( operator . divideContent ( content ) [ 0 ] ) . toEqual ( "" ) ;
67- expect ( operator . divideContent ( content ) [ 1 ] ) . toEqual ( content ) ;
68- } ) ;
69-
70- it ( 'handles more requires when divide' , ( ) => {
71- const content = "require('colors.js');\n" +
72- "require('sizes.js');\n" +
73- ".someClass { color: #fff;}" ;
74- expect ( operator . divideContent ( content ) [ 0 ] ) . toEqual ( "require('colors.js');\n" + "require('sizes.js');" ) ;
75- expect ( operator . divideContent ( content ) [ 1 ] ) . toEqual ( "\n.someClass { color: #fff;}" ) ;
76- } ) ;
7757
78- it ( 'handles the form of request("asdf").someProp' , ( ) => {
79- const content = "require('corners.js').typeOne;\n" + ".someClass { color: #fff;}" ;
80- expect ( operator . divideContent ( content ) [ 0 ] ) . toEqual ( "require('corners.js').typeOne;" ) ;
81- } ) ;
82- } ) ;
83-
84- describe ( 'getModulePath' , ( ) => {
85- it ( 'extracts module paths and methodName into an array' , ( ) => {
86- expect ( operator . getModulePath ( 'require("./mocks/colors.js");\n' ) ) . toEqual ( [ { path : "./mocks/colors.js" } ] ) ;
87-
88- expect ( operator . getModulePath ( 'require("./mocks/colors.js");\n' + 'require("./mocks/sizes.js");' ) ) . toEqual ( [ { path : "./mocks/colors.js" } , { path :"./mocks/sizes.js" } ] ) ;
89-
90- expect ( operator . getModulePath ( 'require("./mocks/corners.js").typeTwo;\n' ) ) . toEqual ( [ { path : "./mocks/corners.js" , methodName : 'typeTwo' } ] ) ;
91- } ) ;
92- } ) ;
9358
9459 describe ( 'getVarData' , ( ) => {
9560 const context = {
@@ -98,25 +63,17 @@ describe('js-to-styles-vars-loader', () => {
9863 } ;
9964
10065 it ( 'gets variable data by modulePath with context' , ( ) => {
101- const varData = operator . getVarData ( [ { path : './mocks/colors.js' } ] , context ) ;
66+ const varData = operator . getVarData ( path . join ( context . context , './mocks/colors.js' ) ) ;
10267 expect ( varData ) . toEqual ( { white : '#fff' , black : '#000' } ) ;
10368 } ) ;
10469
105- it ( 'merges module data if there are more requests' , ( ) => {
106- const varData = operator . getVarData ( [ { path :'./mocks/colors.js' } , { path :'./mocks/sizes.js' } ] , context ) ;
107- expect ( varData ) . toEqual ( { white : '#fff' , black : '#000' , small : '10px' , large : '50px' } ) ;
108- } ) ;
109-
110- it ( 'handles methodName if it is given' , ( ) => {
111- const varData = operator . getVarData ( [ { path :'./mocks/corners.js' , methodName : 'typeOne' } ] , context ) ;
70+ it ( 'uses value from property' , ( ) => {
71+ const varData = operator . getVarData ( path . join ( context . context , './mocks/corners.js' ) , 'typeOne' ) ;
11272 expect ( varData ) . toEqual ( { tiny : '1%' , medium : '3%' } ) ;
11373 } ) ;
114-
115- it ( 'call context.addDependecy with modulePath' , ( ) => {
116- spyOn ( context , 'addDependency' ) ;
117- const relativePath = './mocks/corners.js' ;
118- operator . getVarData ( [ { path : relativePath , methodName : 'typeOne' } ] , context ) ;
119- expect ( context . addDependency ) . toHaveBeenCalledWith ( path . resolve ( relativePath ) ) ;
74+ it ( 'uses value from nested property' , ( ) => {
75+ const varData = operator . getVarData ( path . join ( context . context , './mocks/corners.js' ) , 'deep.nested' ) ;
76+ expect ( varData ) . toEqual ( { color : '#f00' } ) ;
12077 } ) ;
12178 } ) ;
12279
@@ -139,22 +96,39 @@ describe('js-to-styles-vars-loader', () => {
13996 context : path . resolve ( ) ,
14097 addDependency ( ) { }
14198 } ;
99+ const content = "require('./mocks/colors.js');\n" +
100+ ".someClass { color: #fff;}" ;
101+
102+ const trimmer = ( str ) => {
103+ return ( str . split ( "\n" ) . filter ( a => a ) . map ( s => s . trim ( ) ) . join ( " " ) ) . trim ( )
104+ } ;
142105
143106 it ( 'inserts vars to styles content' , ( ) => {
144- const content = "require('./mocks/colors.js');\n" +
145- ".someClass { color: #fff;}" ;
146- const [ moduleData , stylesContent ] = operator . divideContent ( content ) ;
147- const modulePath = operator . getModulePath ( moduleData ) ;
148- const varData = operator . getVarData ( modulePath , context ) ;
149- const vars = operator . transformToStyleVars ( { type : 'less' , varData } ) ;
107+ operator . mergeVarsToContent ( content , context , 'less' )
150108
151- expect ( operator . mergeVarsToContent ( content , context , 'less' ) ) . toEqual ( vars + stylesContent ) ;
109+ expect ( trimmer ( operator . mergeVarsToContent ( content , context , 'less' ) ) ) . toEqual ( trimmer ( `
110+ @white: #fff; @black: #000; ; .someClass { color: #fff;}
111+ ` ) ) ;
112+ } ) ;
113+
114+ it ( 'call context.addDependecy' , ( ) => {
115+ spyOn ( context , 'addDependency' ) ;
116+ const dependencyPath = path . join ( context . context , './mocks/colors.js' ) ;
117+ operator . mergeVarsToContent ( content , context , 'less' )
118+ expect ( context . addDependency ) . toHaveBeenCalledWith ( path . resolve ( dependencyPath ) ) ;
152119 } ) ;
153120
154121 it ( 'gives back content as is if there is no requre' , ( ) => {
155122 const content = ".someClass { color: #fff;}" ;
156123 expect ( operator . mergeVarsToContent ( content , context ) ) . toEqual ( content ) ;
157124 } ) ;
125+
126+ it ( "inserts variables inside style blocks and does not fail if the last 'require' is inside a block" , ( ) => {
127+ const content = fs . readFileSync ( path . resolve ( './mocks/case1.less' ) , 'utf8' ) ;
128+ const expectedContent = fs . readFileSync ( path . resolve ( './mocks/case1_expected.less' ) , 'utf8' ) ;
129+ const merged = operator . mergeVarsToContent ( content , { ...context , context : path . resolve ( './mocks/' ) } , 'less' ) ;
130+ expect ( merged . trim ( ) ) . toEqual ( expectedContent . trim ( ) ) ;
131+ } )
158132 } ) ;
159133
160134 describe ( 'getResource' , ( ) => {
0 commit comments