2021-07-30 01:12:50 +08:00
|
|
|
/**
|
|
|
|
* @jest-environment node
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { createApp, h, defineAsyncComponent } from 'vue'
|
2021-08-07 00:41:40 +08:00
|
|
|
import { ReadableStream, TransformStream } from 'stream/web'
|
|
|
|
import { pipeToWebWritable, renderToWebStream } from '../src'
|
2021-07-30 01:12:50 +08:00
|
|
|
|
2021-08-07 00:41:40 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
global.ReadableStream = ReadableStream
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// @ts-ignore
|
|
|
|
delete global.ReadableStream
|
|
|
|
})
|
|
|
|
|
|
|
|
test('renderToWebStream', async () => {
|
2021-07-30 01:12:50 +08:00
|
|
|
const Async = defineAsyncComponent(() =>
|
|
|
|
Promise.resolve({
|
|
|
|
render: () => h('div', 'async')
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const App = {
|
|
|
|
render: () => [h('div', 'parent'), h(Async)]
|
|
|
|
}
|
|
|
|
|
2021-08-07 00:41:40 +08:00
|
|
|
const stream = renderToWebStream(createApp(App))
|
2021-07-30 01:12:50 +08:00
|
|
|
|
|
|
|
const reader = stream.getReader()
|
2021-08-07 00:41:40 +08:00
|
|
|
const decoder = new TextDecoder()
|
|
|
|
|
|
|
|
let res = ''
|
|
|
|
await reader.read().then(function read({ done, value }): any {
|
|
|
|
if (!done) {
|
|
|
|
res += decoder.decode(value)
|
|
|
|
return reader.read().then(read)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(res).toBe(`<!--[--><div>parent</div><div>async</div><!--]-->`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('pipeToWebWritable', async () => {
|
|
|
|
const Async = defineAsyncComponent(() =>
|
|
|
|
Promise.resolve({
|
|
|
|
render: () => h('div', 'async')
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const App = {
|
|
|
|
render: () => [h('div', 'parent'), h(Async)]
|
|
|
|
}
|
|
|
|
|
|
|
|
const { readable, writable } = new TransformStream()
|
|
|
|
pipeToWebWritable(createApp(App), {}, writable)
|
|
|
|
|
|
|
|
const reader = readable.getReader()
|
|
|
|
const decoder = new TextDecoder()
|
2021-07-30 01:12:50 +08:00
|
|
|
|
|
|
|
let res = ''
|
|
|
|
await reader.read().then(function read({ done, value }): any {
|
|
|
|
if (!done) {
|
2021-08-07 00:41:40 +08:00
|
|
|
res += decoder.decode(value)
|
2021-07-30 01:12:50 +08:00
|
|
|
return reader.read().then(read)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(res).toBe(`<!--[--><div>parent</div><div>async</div><!--]-->`)
|
|
|
|
})
|