|
1 | 1 | import { h } from 'preact';
|
2 | 2 | import { expect } from 'chai';
|
3 | 3 | import { Suspense } from 'preact/compat';
|
| 4 | +import { useId } from 'preact/hooks'; |
4 | 5 | import { renderToChunks } from '../../src/lib/chunked';
|
5 | 6 | import { createSubtree, createInitScript } from '../../src/lib/client';
|
6 | 7 | import { createSuspender } from '../utils';
|
| 8 | +import { VNODE, PARENT } from '../../src/lib/constants'; |
7 | 9 |
|
8 | 10 | describe('renderToChunks', () => {
|
9 | 11 | it('should render non-suspended JSX in one go', async () => {
|
@@ -66,4 +68,85 @@ describe('renderToChunks', () => {
|
66 | 68 | '</div>'
|
67 | 69 | ]);
|
68 | 70 | });
|
| 71 | + |
| 72 | + it('should encounter no circular references when rendering a suspense boundary subtree', async () => { |
| 73 | + const { Suspender, suspended } = createSuspender(); |
| 74 | + |
| 75 | + const visited = new Set(); |
| 76 | + let circular = false; |
| 77 | + |
| 78 | + function CircularReferenceCheck() { |
| 79 | + let root = this[VNODE]; |
| 80 | + while (root !== null && root[PARENT] !== null) { |
| 81 | + if (visited.has(root)) { |
| 82 | + // Can't throw an error here, _catchError handler will also loop infinitely |
| 83 | + circular = true; |
| 84 | + break; |
| 85 | + } |
| 86 | + visited.add(root); |
| 87 | + root = root[PARENT]; |
| 88 | + } |
| 89 | + return <p>it works</p>; |
| 90 | + } |
| 91 | + |
| 92 | + const result = []; |
| 93 | + const promise = renderToChunks( |
| 94 | + <div> |
| 95 | + <Suspense fallback="loading..."> |
| 96 | + <Suspender> |
| 97 | + <CircularReferenceCheck /> |
| 98 | + </Suspender> |
| 99 | + </Suspense> |
| 100 | + </div>, |
| 101 | + { onWrite: (s) => result.push(s) } |
| 102 | + ); |
| 103 | + |
| 104 | + suspended.resolve(); |
| 105 | + await promise; |
| 106 | + |
| 107 | + if (circular) { |
| 108 | + throw new Error('CircularReference'); |
| 109 | + } |
| 110 | + |
| 111 | + expect(result).to.deep.equal([ |
| 112 | + '<div><!--preact-island:16-->loading...<!--/preact-island:16--></div>', |
| 113 | + '<div hidden>', |
| 114 | + createInitScript(1), |
| 115 | + createSubtree('16', '<p>it works</p>'), |
| 116 | + '</div>' |
| 117 | + ]); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should support using useId hooks inside a suspense boundary', async () => { |
| 121 | + const { Suspender, suspended } = createSuspender(); |
| 122 | + |
| 123 | + function ComponentWithId() { |
| 124 | + const id = useId(); |
| 125 | + return <p>id: {id}</p>; |
| 126 | + } |
| 127 | + |
| 128 | + const result = []; |
| 129 | + const promise = renderToChunks( |
| 130 | + <div> |
| 131 | + <ComponentWithId /> |
| 132 | + <Suspense fallback="loading..."> |
| 133 | + <Suspender> |
| 134 | + <ComponentWithId /> |
| 135 | + </Suspender> |
| 136 | + </Suspense> |
| 137 | + </div>, |
| 138 | + { onWrite: (s) => result.push(s) } |
| 139 | + ); |
| 140 | + |
| 141 | + suspended.resolve(); |
| 142 | + await promise; |
| 143 | + |
| 144 | + expect(result).to.deep.equal([ |
| 145 | + '<div><p>id: P0-0</p><!--preact-island:24-->loading...<!--/preact-island:24--></div>', |
| 146 | + '<div hidden>', |
| 147 | + createInitScript(1), |
| 148 | + createSubtree('24', '<p>id: P0-0</p>'), |
| 149 | + '</div>' |
| 150 | + ]); |
| 151 | + }); |
69 | 152 | });
|
0 commit comments