ssr.stream.spec.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import Vue from '../../dist/vue.common.js'
  2. import { compileToFunctions } from '../../dist/compiler.common.js'
  3. import { renderToStream } from '../../dist/server-renderer.js'
  4. describe('SSR: renderToStream', () => {
  5. it('should render to a stream', done => {
  6. const stream = renderVmWithOptions({
  7. template: `
  8. <div>
  9. <p class="hi">yoyo</p>
  10. <div id="ho" :class="{ red: isRed }"></div>
  11. <span>{{ test }}</span>
  12. <test></test>
  13. </div>
  14. `,
  15. data: {
  16. test: 'hi',
  17. isRed: true
  18. },
  19. components: {
  20. test: {
  21. render: function () {
  22. return this.$createElement('div', { class: ['a'] }, 'hahahaha')
  23. }
  24. }
  25. }
  26. })
  27. let res = ''
  28. stream.on('data', chunk => {
  29. res += chunk
  30. })
  31. stream.on('end', () => {
  32. expect(res).toContain('<div><p class="hi">yoyo</p><div id="ho" class="red"></div><span>hi</span><div class="a">hahahaha</div></div>')
  33. done()
  34. })
  35. })
  36. })
  37. function renderVmWithOptions (options) {
  38. const res = compileToFunctions(options.template, {
  39. preserveWhitespace: false
  40. })
  41. Object.assign(options, res)
  42. delete options.template
  43. return renderToStream(new Vue(options))
  44. }