@@ -8,30 +8,136 @@ var loaderUtils = require('loader-utils');
8
8
var useable = require ( "../useable" ) ;
9
9
10
10
describe ( "useable tests" , function ( ) {
11
- var sandbox = sinon . sandbox . create ( ) ;
12
- var getOptions ;
11
+ describe ( 'hmr' , function ( ) {
12
+ var sandbox = sinon . sandbox . create ( ) ;
13
+ var getOptions ;
13
14
14
- beforeEach ( ( ) => {
15
- // Mock loaderUtils to override options
16
- getOptions = sandbox . stub ( loaderUtils , 'getOptions' ) ;
17
- } ) ;
15
+ beforeEach ( ( ) => {
16
+ // Mock loaderUtils to override options
17
+ getOptions = sandbox . stub ( loaderUtils , 'getOptions' ) ;
18
+ } ) ;
18
19
19
- afterEach ( ( ) => {
20
- sandbox . restore ( ) ;
21
- } ) ;
20
+ afterEach ( ( ) => {
21
+ sandbox . restore ( ) ;
22
+ } ) ;
22
23
23
- it ( "should output HMR code by default" , function ( ) {
24
- assert . equal ( / ( m o d u l e \. h o t ) / g. test ( useable . pitch ( ) ) , true ) ;
25
- } ) ;
24
+ it ( "should output HMR code by default" , function ( ) {
25
+ assert . equal ( / ( m o d u l e \. h o t ) / g. test ( useable . pitch ( ) ) , true ) ;
26
+ } ) ;
26
27
27
- it ( "should NOT output HMR code when options.hmr is false" , function ( ) {
28
- getOptions . returns ( { hmr : false } ) ;
29
- assert . equal ( / ( m o d u l e \. h o t ) / g. test ( useable . pitch ( ) ) , false ) ;
28
+ it ( "should NOT output HMR code when options.hmr is false" , function ( ) {
29
+ getOptions . returns ( { hmr : false } ) ;
30
+ assert . equal ( / ( m o d u l e \. h o t ) / g. test ( useable . pitch ( ) ) , false ) ;
31
+ } ) ;
32
+
33
+ it ( "should output HMR code when options.hmr is true" , function ( ) {
34
+ getOptions . returns ( { hmr : true } ) ;
35
+ assert . equal ( / ( m o d u l e \. h o t ) / g. test ( useable . pitch ( ) ) , true ) ;
36
+ } ) ;
30
37
} ) ;
31
38
32
- it ( "should output HMR code when options.hmr is true" , function ( ) {
33
- getOptions . returns ( { hmr : true } ) ;
34
- assert . equal ( / ( m o d u l e \. h o t ) / g. test ( useable . pitch ( ) ) , true ) ;
39
+ describe ( 'insert into' , function ( ) {
40
+ var path = require ( "path" ) ;
41
+
42
+ var utils = require ( "./utils" ) ,
43
+ runCompilerTest = utils . runCompilerTest ;
44
+
45
+ var fs ;
46
+
47
+ var requiredCss = ".required { color: blue }" ,
48
+ requiredCssTwo = ".requiredTwo { color: cyan }" ,
49
+ localScopedCss = ":local(.className) { background: red; }" ,
50
+ localComposingCss = `
51
+ :local(.composingClass) {
52
+ composes: className from './localScoped.css';
53
+ color: blue;
54
+ }
55
+ ` ,
56
+ requiredStyle = `<style type="text/css">${ requiredCss } </style>` ,
57
+ existingStyle = `<style id="existing-style">.existing { color: yellow }</style>` ,
58
+ checkValue = '<div class="check">check</div>' ,
59
+ rootDir = path . resolve ( __dirname + "/../" ) + "/" ,
60
+ jsdomHtml = [
61
+ "<html>" ,
62
+ "<head id='head'>" ,
63
+ existingStyle ,
64
+ "</head>" ,
65
+ "<body>" ,
66
+ "<div class='target'>" ,
67
+ checkValue ,
68
+ "</div>" ,
69
+ "<iframe class='iframeTarget'/>" ,
70
+ "</body>" ,
71
+ "</html>"
72
+ ] . join ( "\n" ) ,
73
+ requiredJS = [
74
+ "var el = document.createElement('div');" ,
75
+ "el.id = \"test-shadow\";" ,
76
+ "document.body.appendChild(el)" ,
77
+ "var css = require('./style.css');" ,
78
+ "css.use();" ,
79
+ ] . join ( "\n" ) ;
80
+
81
+ var styleLoaderOptions = { } ;
82
+ var cssRule = { } ;
83
+
84
+ var defaultCssRule = {
85
+ test : / \. c s s ? $ / ,
86
+ use : [
87
+ {
88
+ loader : "style-loader/useable" ,
89
+ options : styleLoaderOptions
90
+ } ,
91
+ "css-loader"
92
+ ]
93
+ } ;
94
+
95
+ var webpackConfig = {
96
+ entry : "./main.js" ,
97
+ output : {
98
+ filename : "bundle.js"
99
+ } ,
100
+ module : {
101
+ rules : [ cssRule ]
102
+ }
103
+ } ;
104
+
105
+ var setupWebpackConfig = function ( ) {
106
+ fs = utils . setup ( webpackConfig , jsdomHtml ) ;
107
+
108
+ // Create a tiny file system. rootDir is used because loaders are referring to absolute paths.
109
+ fs . mkdirpSync ( rootDir ) ;
110
+ fs . writeFileSync ( rootDir + "main.js" , requiredJS ) ;
111
+ fs . writeFileSync ( rootDir + "style.css" , requiredCss ) ;
112
+ fs . writeFileSync ( rootDir + "styleTwo.css" , requiredCssTwo ) ;
113
+ fs . writeFileSync ( rootDir + "localScoped.css" , localScopedCss ) ;
114
+ fs . writeFileSync ( rootDir + "localComposing.css" , localComposingCss ) ;
115
+ } ;
116
+
117
+ beforeEach ( function ( ) {
118
+ // Reset all style-loader options
119
+ for ( var member in styleLoaderOptions ) {
120
+ delete styleLoaderOptions [ member ] ;
121
+ }
122
+
123
+ for ( var member in defaultCssRule ) {
124
+ cssRule [ member ] = defaultCssRule [ member ] ;
125
+ }
126
+
127
+ setupWebpackConfig ( ) ;
128
+ } ) ; // before each
129
+
130
+ it ( "insert into iframe" , function ( done ) {
131
+ let selector = "iframe.iframeTarget" ;
132
+ styleLoaderOptions . insertInto = selector ;
133
+
134
+ let expected = requiredStyle ;
135
+
136
+ runCompilerTest ( expected , done , function ( ) {
137
+ return this . document . querySelector ( selector ) . contentDocument . head . innerHTML ;
138
+ } , selector ) ;
139
+ } ) ; // it insert into
140
+
35
141
} ) ;
36
142
37
143
} ) ;
0 commit comments