forked from preactjs/preact-render-to-string
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream-node.test.js
More file actions
76 lines (67 loc) · 1.84 KB
/
stream-node.test.js
File metadata and controls
76 lines (67 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { PassThrough } from 'node:stream';
import { h } from 'preact';
import { expect } from 'chai';
import { Suspense } from 'preact/compat';
import { createSubtree, createInitScript } from '../../src/lib/client';
import { renderToPipeableStream } from '../../src/stream-node';
import { Deferred } from '../../src/lib/util';
import { createSuspender } from '../utils';
function streamToString(stream) {
const decoder = new TextDecoder();
const def = new Deferred();
stream.on('data', (chunk) => {
chunks.push(decoder.decode(chunk));
});
stream.on('error', (err) => def.reject(err));
stream.on('end', () => def.resolve(chunks));
const chunks = [];
return def;
}
/**
* @param {ReadableStream} input
*/
function createSink() {
const stream = new PassThrough();
const def = streamToString(stream);
return {
promise: def.promise,
stream
};
}
describe('renderToPipeableStream', () => {
it('should render non-suspended JSX in one go', async () => {
const sink = createSink();
const { pipe } = renderToPipeableStream(<div class="foo">bar</div>, {
onAllReady: () => {
pipe(sink.stream);
}
});
const result = await sink.promise;
expect(result).to.deep.equal(['<div class="foo">bar</div>']);
});
it('should render fallback + attach loaded subtree on suspend', async () => {
const { Suspender, suspended } = createSuspender();
const sink = createSink();
const { pipe } = renderToPipeableStream(
<div>
<Suspense fallback="loading...">
<Suspender />
</Suspense>
</div>,
{
onShellReady: () => {
pipe(sink.stream);
}
}
);
suspended.resolve();
const result = await sink.promise;
expect(result).to.deep.equal([
'<div><!--preact-island:33-->loading...<!--/preact-island:33--></div>',
'<div hidden>',
createInitScript(),
createSubtree('33', '<p>it works</p>'),
'</div>'
]);
});
});