forked from preactjs/preact-render-to-string
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender-chunked.test.js
152 lines (129 loc) · 3.66 KB
/
render-chunked.test.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { h } from 'preact';
import { expect } from 'chai';
import { Suspense } from 'preact/compat';
import { useId } from 'preact/hooks';
import { renderToChunks } from '../../src/lib/chunked';
import { createSubtree, createInitScript } from '../../src/lib/client';
import { createSuspender } from '../utils';
import { VNODE, PARENT } from '../../src/lib/constants';
describe('renderToChunks', () => {
it('should render non-suspended JSX in one go', async () => {
const result = [];
await renderToChunks(<div class="foo">bar</div>, {
onWrite: (s) => result.push(s)
});
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 result = [];
const promise = renderToChunks(
<div>
<Suspense fallback="loading...">
<Suspender />
</Suspense>
</div>,
{ onWrite: (s) => result.push(s) }
);
suspended.resolve();
await promise;
expect(result).to.deep.equal([
'<div><!--preact-island:5-->loading...<!--/preact-island:5--></div>',
'<div hidden>',
createInitScript(),
createSubtree('5', '<p>it works</p>'),
'</div>'
]);
});
it('should abort pending suspensions with AbortSignal', async () => {
const { Suspender, suspended } = createSuspender();
const controller = new AbortController();
const result = [];
const promise = renderToChunks(
<div>
<Suspense fallback="loading...">
<Suspender />
</Suspense>
</div>,
{ onWrite: (s) => result.push(s), abortSignal: controller.signal }
);
controller.abort();
await promise;
suspended.resolve();
expect(result).to.deep.equal([
'<div><!--preact-island:10-->loading...<!--/preact-island:10--></div>',
'<div hidden>',
createInitScript(1),
'</div>'
]);
});
it('should encounter no circular references when rendering a suspense boundary subtree', async () => {
const { Suspender, suspended } = createSuspender();
const visited = new Set();
let circular = false;
function CircularReferenceCheck() {
let root = this[VNODE];
while (root !== null && root[PARENT] !== null) {
if (visited.has(root)) {
// Can't throw an error here, _catchError handler will also loop infinitely
circular = true;
break;
}
visited.add(root);
root = root[PARENT];
}
return <p>it works</p>;
}
const result = [];
const promise = renderToChunks(
<div>
<Suspense fallback="loading...">
<Suspender>
<CircularReferenceCheck />
</Suspender>
</Suspense>
</div>,
{ onWrite: (s) => result.push(s) }
);
suspended.resolve();
await promise;
if (circular) {
throw new Error('CircularReference');
}
expect(result).to.deep.equal([
'<div><!--preact-island:16-->loading...<!--/preact-island:16--></div>',
'<div hidden>',
createInitScript(1),
createSubtree('16', '<p>it works</p>'),
'</div>'
]);
});
it('should support using useId hooks inside a suspense boundary', async () => {
const { Suspender, suspended } = createSuspender();
function ComponentWithId() {
const id = useId();
return <p>id: {id}</p>;
}
const result = [];
const promise = renderToChunks(
<div>
<ComponentWithId />
<Suspense fallback="loading...">
<Suspender>
<ComponentWithId />
</Suspender>
</Suspense>
</div>,
{ onWrite: (s) => result.push(s) }
);
suspended.resolve();
await promise;
expect(result).to.deep.equal([
'<div><p>id: P0-0</p><!--preact-island:24-->loading...<!--/preact-island:24--></div>',
'<div hidden>',
createInitScript(1),
createSubtree('24', '<p>id: P0-0</p>'),
'</div>'
]);
});
});