| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  | import Vue from '../../dist/vue.common.js' | 
					
						
							| 
									
										
										
										
											2016-05-08 12:14:44 +08:00
										 |  |  | import { compileToFunctions } from '../../dist/compiler.js' | 
					
						
							| 
									
										
										
										
											2016-04-26 12:42:49 +08:00
										 |  |  | import createRenderer from '../../dist/server-renderer.js' | 
					
						
							|  |  |  | const { renderToStream } = createRenderer() | 
					
						
							| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('SSR: renderToStream', () => { | 
					
						
							|  |  |  |   it('should render to a stream', done => { | 
					
						
							|  |  |  |     const stream = renderVmWithOptions({ | 
					
						
							|  |  |  |       template: `
 | 
					
						
							|  |  |  |         <div> | 
					
						
							|  |  |  |           <p class="hi">yoyo</p> | 
					
						
							| 
									
										
										
										
											2016-05-04 04:42:14 +08:00
										 |  |  |           <div id="ho" :class="[testClass, { red: isRed }]"></div> | 
					
						
							| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  |           <span>{{ test }}</span> | 
					
						
							| 
									
										
										
										
											2016-04-26 09:29:58 +08:00
										 |  |  |           <input :value="test"> | 
					
						
							| 
									
										
										
										
											2016-05-04 04:42:14 +08:00
										 |  |  |           <b-comp></b-comp> | 
					
						
							|  |  |  |           <c-comp></c-comp> | 
					
						
							| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  |         </div> | 
					
						
							|  |  |  |       `,
 | 
					
						
							|  |  |  |       data: { | 
					
						
							|  |  |  |         test: 'hi', | 
					
						
							| 
									
										
										
										
											2016-05-04 04:42:14 +08:00
										 |  |  |         isRed: true, | 
					
						
							|  |  |  |         testClass: 'a' | 
					
						
							| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  |       }, | 
					
						
							|  |  |  |       components: { | 
					
						
							| 
									
										
										
										
											2016-05-04 04:42:14 +08:00
										 |  |  |         bComp (resolve) { | 
					
						
							|  |  |  |           return resolve({ | 
					
						
							|  |  |  |             render () { | 
					
						
							|  |  |  |               return this.$createElement('test-async-2') | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             components: { | 
					
						
							|  |  |  |               testAsync2 (resolve) { | 
					
						
							|  |  |  |                 return resolve({ | 
					
						
							|  |  |  |                   created () { this.$parent.$parent.testClass = 'b' }, | 
					
						
							|  |  |  |                   render () { | 
					
						
							|  |  |  |                     return this.$createElement('div', { class: [this.$parent.$parent.testClass] }, 'test') | 
					
						
							|  |  |  |                   } | 
					
						
							|  |  |  |                 }) | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         cComp: { | 
					
						
							|  |  |  |           render () { | 
					
						
							|  |  |  |             return this.$createElement('div', { class: [this.$parent.testClass] }, 'test') | 
					
						
							| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     let res = '' | 
					
						
							|  |  |  |     stream.on('data', chunk => { | 
					
						
							|  |  |  |       res += chunk | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     stream.on('end', () => { | 
					
						
							| 
									
										
										
										
											2016-04-26 09:29:58 +08:00
										 |  |  |       expect(res).toContain( | 
					
						
							|  |  |  |         '<div server-rendered="true">' + | 
					
						
							| 
									
										
										
										
											2016-05-04 04:42:14 +08:00
										 |  |  |         '<p class="hi">yoyo</p>' + | 
					
						
							|  |  |  |         '<div id="ho" class="a red"></div>' + | 
					
						
							|  |  |  |         '<span>hi</span>' + | 
					
						
							|  |  |  |         '<input value="hi">' + | 
					
						
							|  |  |  |         '<div class="b">test</div>' + | 
					
						
							|  |  |  |         '<div class="b">test</div>' + | 
					
						
							| 
									
										
										
										
											2016-04-26 09:29:58 +08:00
										 |  |  |         '</div>' | 
					
						
							|  |  |  |       ) | 
					
						
							| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  |       done() | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-05-12 14:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should catch error', done => { | 
					
						
							|  |  |  |     const stream = renderToStream(new Vue({ | 
					
						
							|  |  |  |       render () { | 
					
						
							|  |  |  |         throw new Error('oops') | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     })) | 
					
						
							|  |  |  |     stream.on('error', err => { | 
					
						
							|  |  |  |       expect(err.toString()).toMatch(/oops/) | 
					
						
							|  |  |  |       done() | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     stream.on('data', _ => _) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-04-26 07:10:33 +08:00
										 |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function renderVmWithOptions (options) { | 
					
						
							|  |  |  |   const res = compileToFunctions(options.template, { | 
					
						
							|  |  |  |     preserveWhitespace: false | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   Object.assign(options, res) | 
					
						
							|  |  |  |   delete options.template | 
					
						
							|  |  |  |   return renderToStream(new Vue(options)) | 
					
						
							|  |  |  | } |