@@ -9,8 +9,7 @@ var rimraf = require('rimraf')
9
9
var genId = require ( '../lib/gen-id' )
10
10
var SourceMapConsumer = require ( 'source-map' ) . SourceMapConsumer
11
11
var ExtractTextPlugin = require ( "extract-text-webpack-plugin" )
12
- var compiler = require ( 'vue-template-compiler' )
13
- var beautify = require ( 'js-beautify' ) . js_beautify
12
+ var compiler = require ( '../lib/template-compiler' )
14
13
15
14
var loaderPath = 'expose?vueModule!' + path . resolve ( __dirname , '../' )
16
15
var mfs = new MemoryFS ( )
@@ -61,11 +60,26 @@ function test (options, assert) {
61
60
} )
62
61
}
63
62
64
- function assertRenderFn ( options , template ) {
65
- var compiled = compiler . compile ( template )
66
- expect ( options . render . toString ( ) . replace ( / \t / g, '' ) ) . to . equal ( 'function (){' +
67
- beautify ( compiled . render , { indent_size : 2 } ) +
68
- '}' )
63
+ function mockRender ( options , data ) {
64
+ return options . render . call ( Object . assign ( {
65
+ _h ( tag , data , children ) {
66
+ if ( Array . isArray ( data ) ) {
67
+ children = data
68
+ data = null
69
+ }
70
+ return {
71
+ tag : tag ,
72
+ data : data ,
73
+ children : children
74
+ }
75
+ } ,
76
+ _m ( index ) {
77
+ return options . staticRenderFns [ index ] . call ( this )
78
+ } ,
79
+ _s ( str ) {
80
+ return String ( str )
81
+ }
82
+ } , data ) )
69
83
}
70
84
71
85
function interopDefault ( module ) {
@@ -79,7 +93,14 @@ describe('vue-loader', function () {
79
93
test ( {
80
94
entry : './test/fixtures/basic.vue'
81
95
} , function ( window , module , rawModule ) {
82
- assertRenderFn ( module , '<h2 class="red">{{msg}}</h2>' )
96
+ var vnode = mockRender ( module , {
97
+ msg : 'hi'
98
+ } )
99
+ // <h2 class="red">{{msg}}</h2>
100
+ expect ( vnode . tag ) . to . equal ( 'h2' )
101
+ expect ( vnode . data . staticClass ) . to . equal ( 'red' )
102
+ expect ( vnode . children [ 0 ] ) . to . equal ( 'hi' )
103
+
83
104
expect ( module . data ( ) . msg ) . to . contain ( 'Hello from Component A!' )
84
105
var style = window . document . querySelector ( 'style' ) . textContent
85
106
expect ( style ) . to . contain ( 'comp-a h2 {\n color: #f00;\n}' )
@@ -91,13 +112,15 @@ describe('vue-loader', function () {
91
112
test ( {
92
113
entry : './test/fixtures/pre.vue'
93
114
} , function ( window , module ) {
94
- assertRenderFn ( module ,
95
- '<div>' +
96
- '<h1>This is the app</h1>' +
97
- '<comp-a></comp-a>' +
98
- '<comp-b></comp-b>' +
99
- '</div>'
100
- )
115
+ var vnode = mockRender ( module )
116
+ // div
117
+ // h1 This is the app
118
+ // comp-a
119
+ // comp-b
120
+ expect ( vnode . children [ 0 ] . tag ) . to . equal ( 'h1' )
121
+ expect ( vnode . children [ 1 ] . tag ) . to . equal ( 'comp-a' )
122
+ expect ( vnode . children [ 2 ] . tag ) . to . equal ( 'comp-b' )
123
+
101
124
expect ( module . data ( ) . msg ) . to . contain ( 'Hello from coffee!' )
102
125
var style = window . document . querySelector ( 'style' ) . textContent
103
126
expect ( style ) . to . contain ( 'body {\n font: 100% Helvetica, sans-serif;\n color: #999;\n}' )
@@ -111,14 +134,23 @@ describe('vue-loader', function () {
111
134
} , function ( window , module ) {
112
135
var id = 'data-v-' + genId ( require . resolve ( './fixtures/scoped-css.vue' ) )
113
136
expect ( module . _scopeId ) . to . equal ( id )
114
- assertRenderFn ( module ,
115
- '<div>' +
116
- '<div><h1>hi</h1></div>\n' +
117
- '<p class="abc def">hi</p>\n' +
118
- '<template v-if="ok"><p class="test">yo</p></template>\n' +
119
- '<svg><template><p></p></template></svg>' +
120
- '</div>'
121
- )
137
+
138
+ var vnode = mockRender ( module , {
139
+ ok : true
140
+ } )
141
+ // <div>
142
+ // <div><h1>hi</h1></div>
143
+ // <p class="abc def">hi</p>
144
+ // <template v-if="ok"><p class="test">yo</p></template>
145
+ // <svg><template><p></p></template></svg>
146
+ // </div>
147
+ expect ( vnode . children [ 0 ] . tag ) . to . equal ( 'div' )
148
+ expect ( vnode . children [ 1 ] ) . to . equal ( ' ' )
149
+ expect ( vnode . children [ 2 ] . tag ) . to . equal ( 'p' )
150
+ expect ( vnode . children [ 2 ] . data . staticClass ) . to . equal ( 'abc def' )
151
+ expect ( vnode . children [ 4 ] [ 0 ] . tag ) . to . equal ( 'p' )
152
+ expect ( vnode . children [ 4 ] [ 0 ] . data . staticClass ) . to . equal ( 'test' )
153
+
122
154
var style = window . document . querySelector ( 'style' ) . textContent
123
155
expect ( style ) . to . contain ( '.test[' + id + '] {\n color: yellow;\n}' )
124
156
expect ( style ) . to . contain ( '.test[' + id + ']:after {\n content: \'bye!\';\n}' )
@@ -144,7 +176,10 @@ describe('vue-loader', function () {
144
176
test ( {
145
177
entry : './test/fixtures/template-import.vue'
146
178
} , function ( window , module ) {
147
- assertRenderFn ( module , '<div><h1>hello</h1></div>' )
179
+ var vnode = mockRender ( module )
180
+ // '<div><h1>hello</h1></div>'
181
+ expect ( vnode . children [ 0 ] . tag ) . to . equal ( 'h1' )
182
+ expect ( vnode . children [ 0 ] . children [ 0 ] ) . to . equal ( 'hello' )
148
183
done ( )
149
184
} )
150
185
} )
@@ -225,8 +260,11 @@ describe('vue-loader', function () {
225
260
msg : 'Hello from mocked service!'
226
261
}
227
262
} ) )
228
- assertRenderFn ( module , '<div class="msg">{{ msg }}</div>' )
229
- expect ( module . data ( ) . msg ) . to . contain ( 'Hello from mocked service!' )
263
+ var vnode = mockRender ( module , module . data ( ) )
264
+ // <div class="msg">{{ msg }}</div>
265
+ expect ( vnode . tag ) . to . equal ( 'div' )
266
+ expect ( vnode . data . staticClass ) . to . equal ( 'msg' )
267
+ expect ( vnode . children [ 0 ] ) . to . equal ( 'Hello from mocked service!' )
230
268
done ( )
231
269
} )
232
270
} )
@@ -246,7 +284,16 @@ describe('vue-loader', function () {
246
284
]
247
285
}
248
286
} , function ( window , module ) {
249
- assertRenderFn ( module , '<img src="logo.c9e00e.png">\n<img src="logo.c9e00e.png">' )
287
+ var vnode = mockRender ( module )
288
+ // <div>
289
+ // <img src="logo.c9e00e.png">
290
+ // <img src="logo.c9e00e.png">
291
+ // </div>
292
+ expect ( vnode . children [ 0 ] . tag ) . to . equal ( 'img' )
293
+ expect ( vnode . children [ 0 ] . data . attrs . src ) . to . equal ( 'logo.c9e00e.png' )
294
+ expect ( vnode . children [ 2 ] . tag ) . to . equal ( 'img' )
295
+ expect ( vnode . children [ 2 ] . data . attrs . src ) . to . equal ( 'logo.c9e00e.png' )
296
+
250
297
var style = window . document . querySelector ( 'style' ) . textContent
251
298
expect ( style ) . to . contain ( 'html { background-image: url(logo.c9e00e.png);\n}' )
252
299
expect ( style ) . to . contain ( 'body { background-image: url(logo.c9e00e.png);\n}' )
@@ -270,4 +317,18 @@ describe('vue-loader', function () {
270
317
done ( )
271
318
} )
272
319
} )
320
+
321
+ it ( 'transpile template with babel' , function ( done ) {
322
+ test ( {
323
+ entry : './test/fixtures/babel.vue'
324
+ } , function ( window , module ) {
325
+ var vnode = mockRender ( module , {
326
+ a : 'hello'
327
+ } )
328
+ // <div :class="{[a]:true}"></div>
329
+ expect ( vnode . tag ) . to . equal ( 'div' )
330
+ expect ( vnode . data . class . hello ) . to . equal ( true )
331
+ done ( )
332
+ } )
333
+ } )
273
334
} )
0 commit comments