@@ -106,49 +106,79 @@ test('postcss', done => {
106106 } )
107107} )
108108
109- // TODO
110- // test('css-modules', done => {
111- // function testWithIdent (localIdentName, regexToMatch, cb) {
112- // mockBundleAndRun({
113- // entry: 'css-modules.vue',
114- // vue: {
115- // cssModules: localIdentName && {
116- // localIdentName: localIdentName
117- // }
118- // }
119- // }, (window, module, raw, instance) => {
120- // // get local class name
121- // const className = instance.style.red
122- // expect(className).toMatch(regexToMatch)
109+ test ( 'css-modules' , async ( ) => {
110+ function testWithIdent ( localIdentName , regexToMatch ) {
111+ return new Promise ( ( resolve , reject ) => {
112+ const baseLoaders = [
113+ 'vue-style-loader' ,
114+ {
115+ loader : 'css-loader' ,
116+ options : {
117+ modules : true ,
118+ localIdentName
119+ }
120+ }
121+ ]
122+ mockBundleAndRun ( {
123+ entry : 'css-modules.vue' ,
124+ modify : config => {
125+ config . module . rules = [
126+ {
127+ test : / \. v u e $ / ,
128+ loader : 'vue-loader'
129+ } ,
130+ {
131+ test : / \. c s s $ / ,
132+ use : baseLoaders
133+ } ,
134+ {
135+ test : / \. s t y l u s $ / ,
136+ use : [
137+ ...baseLoaders ,
138+ 'stylus-loader'
139+ ]
140+ }
141+ ]
142+ }
143+ } , ( { window, instance, jsdomError, bundleError } ) => {
144+ if ( jsdomError ) return reject ( jsdomError )
145+ if ( bundleError ) return reject ( bundleError )
123146
124- // // class name in style
125- // let style = [].slice.call(window.document.querySelectorAll('style')).map((style) => {
126- // return style.textContent
127- // }).join('\n')
128- // style = normalizeNewline(style)
129- // expect(style).toContain('.' + className + ' {\n color: red;\n}')
147+ // get local class name
148+ const className = instance . style . red
149+ expect ( className ) . toMatch ( regexToMatch )
130150
131- // // animation name
132- // const match = style.match(/@keyframes\s+(\S+)\s+{/)
133- // expect(match).toHaveLength(2)
134- // const animationName = match[1]
135- // expect(animationName).not.toBe('fade' )
136- // expect(style).toContain('animation: ' + animationName + ' 1s; ')
151+ // class name in style
152+ let style = [ ] . slice . call ( window . document . querySelectorAll ( ' style' ) ) . map ( ( style ) => {
153+ return style . textContent
154+ } ) . join ( '\n' )
155+ style = normalizeNewline ( style )
156+ expect ( style ) . toContain ( '. ' + className + ' {\n color: red;\n} ' )
137157
138- // // default module + pre-processor + scoped
139- // const anotherClassName = instance.$style.red
140- // expect(anotherClassName).to.match(regexToMatch).not.toBe(className)
141- // const id = 'data-v-' + genId('css-modules.vue')
142- // expect(style).toContain('.' + anotherClassName + '[' + id + ']')
158+ // animation name
159+ const match = style . match ( / @ k e y f r a m e s \s + ( \S + ) \s + { / )
160+ expect ( match ) . toHaveLength ( 2 )
161+ const animationName = match [ 1 ]
162+ expect ( animationName ) . not . toBe ( 'fade' )
163+ expect ( style ) . toContain ( 'animation: ' + animationName + ' 1s;' )
143164
144- // cb()
145- // })
146- // }
147- // // default localIdentName
148- // testWithIdent(undefined, /^red_\w{8}/, () => {
149- // // specified localIdentName
150- // const ident = '[path][name]---[local]---[hash:base64:5]'
151- // const regex = /css-modules---red---\w{5}/
152- // testWithIdent(ident, regex, done)
153- // })
154- // })
165+ // default module + pre-processor + scoped
166+ const anotherClassName = instance . $style . red
167+ expect ( anotherClassName ) . toMatch ( regexToMatch )
168+ const id = 'data-v-' + genId ( 'css-modules.vue' )
169+ expect ( style ) . toContain ( '.' + anotherClassName + '[' + id + ']' )
170+
171+ resolve ( )
172+ } )
173+ } )
174+ }
175+
176+ // default ident
177+ await testWithIdent ( undefined , / ^ \w { 22 } / )
178+
179+ // custom ident
180+ await testWithIdent (
181+ '[path][name]---[local]---[hash:base64:5]' ,
182+ / c s s - m o d u l e s - - - r e d - - - \w { 5 } /
183+ )
184+ } )
0 commit comments