acrylblends 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/README.md +4 -1
  2. package/docs/framework/README.md +3 -2
  3. package/docs/framework/UPSTREAM.md +1 -1
  4. package/docs/framework/cordis/acryl-in-a-cordis-app.md +6 -0
  5. package/docs/framework/getting-started/basic.md +1 -0
  6. package/docs/framework/getting-started/desktop.md +41 -0
  7. package/docs/framework/guides/jsx-dom.md +0 -3
  8. package/docs/framework/guides/jsx.md +0 -3
  9. package/docs/framework/helpers/css.md +0 -3
  10. package/docs/framework/upstream.json +3 -3
  11. package/lib/accepts.d.ts +1 -1
  12. package/lib/acryl-base.d.ts +1 -1
  13. package/lib/adapter.d.ts +1 -1
  14. package/lib/{app-BdrVHBlJ.d.ts → app-wFwpg-bt.d.ts} +1 -1
  15. package/lib/base-BpQy2Rk7.d.ts +783 -0
  16. package/lib/base-ddiVmiE7.js +721 -0
  17. package/lib/basic-auth.d.ts +1 -1
  18. package/lib/bearer-auth.d.ts +1 -1
  19. package/lib/body-limit.d.ts +1 -1
  20. package/lib/cache.d.ts +1 -1
  21. package/lib/client.d.ts +2 -2
  22. package/lib/combine.d.ts +1 -1
  23. package/lib/common-D8Z4JRCw.d.ts +46 -0
  24. package/lib/components-Ce5FRyHU.js +268 -0
  25. package/lib/components-DYZdxJIJ.js +24 -0
  26. package/lib/compress.d.ts +1 -1
  27. package/lib/conninfo.d.ts +2 -2
  28. package/lib/constants-BR9jM9BC.js +9 -0
  29. package/lib/{context-B5Y1lLha.d.ts → context-DVQBjuns.d.ts} +1 -1
  30. package/lib/context-DxFdY_un.d.ts +250 -0
  31. package/lib/context-storage.d.ts +1 -1
  32. package/lib/{cookie-D9_nQbsM.d.ts → cookie-CCelEqky.d.ts} +1 -1
  33. package/lib/cookie.d.ts +3 -3
  34. package/lib/cors.d.ts +1 -1
  35. package/lib/csrf.d.ts +1 -1
  36. package/lib/css-D2jb7X8b.js +320 -0
  37. package/lib/css.d.ts +82 -0
  38. package/lib/css.js +134 -0
  39. package/lib/dev.d.ts +2 -2
  40. package/lib/etag.d.ts +1 -1
  41. package/lib/factory.d.ts +2 -2
  42. package/lib/hooks-Bq5q9IMl.js +762 -0
  43. package/lib/html-SllAe0YT.d.ts +46 -0
  44. package/lib/html.d.ts +1 -1
  45. package/lib/index-C850MeBA.d.ts +94 -0
  46. package/lib/{index-m12zcmph.d.ts → index-CUTfU69R.d.ts} +3 -3
  47. package/lib/{index-DtEoak1i.d.ts → index-Cqcdluyn.d.ts} +2 -2
  48. package/lib/{index-Kqou8Fjh.d.ts → index-CsvB-F9j.d.ts} +2 -2
  49. package/lib/index.d.ts +4 -4
  50. package/lib/ip-restriction.d.ts +2 -2
  51. package/lib/jsx/dom/client.d.ts +30 -0
  52. package/lib/jsx/dom/client.js +49 -0
  53. package/lib/jsx/dom/css.d.ts +84 -0
  54. package/lib/jsx/dom/css.js +2 -0
  55. package/lib/jsx/dom/jsx-dev-runtime.d.ts +3 -0
  56. package/lib/jsx/dom/jsx-dev-runtime.js +2 -0
  57. package/lib/jsx/dom/jsx-runtime.d.ts +3 -0
  58. package/lib/jsx/dom/jsx-runtime.js +2 -0
  59. package/lib/jsx/dom/server.d.ts +105 -0
  60. package/lib/jsx/dom/server.js +37 -0
  61. package/lib/jsx/dom.d.ts +5 -0
  62. package/lib/jsx/dom.js +74 -0
  63. package/lib/jsx/jsx-dev-runtime.d.ts +6 -0
  64. package/lib/jsx/jsx-dev-runtime.js +14 -0
  65. package/lib/jsx/jsx-runtime.d.ts +10 -0
  66. package/lib/jsx/jsx-runtime.js +30 -0
  67. package/lib/jsx/streaming.d.ts +2 -0
  68. package/lib/jsx/streaming.js +2 -0
  69. package/lib/jsx-dev-runtime-B22O-Npk.d.ts +7 -0
  70. package/lib/jsx-dev-runtime-kAhR_4C7.js +354 -0
  71. package/lib/jsx-runtime-BVVgDSdq.d.ts +1 -0
  72. package/lib/jsx.d.ts +3 -0
  73. package/lib/jsx.js +47 -0
  74. package/lib/jwk.d.ts +4 -4
  75. package/lib/{jwt-BA65QhnJ.d.ts → jwt-BARhotDz.d.ts} +1 -1
  76. package/lib/jwt.d.ts +4 -4
  77. package/lib/language.d.ts +1 -1
  78. package/lib/logger.d.ts +1 -1
  79. package/lib/loopback-only.d.ts +1 -1
  80. package/lib/method-not-allowed.d.ts +2 -2
  81. package/lib/method-override.d.ts +2 -2
  82. package/lib/mount.d.ts +1 -1
  83. package/lib/node-server/conninfo.d.ts +1 -1
  84. package/lib/node-server/serve-static.d.ts +1 -1
  85. package/lib/node-server/serve-static.js +1 -1
  86. package/lib/node-server.d.ts +1 -1
  87. package/lib/node-server.js +1 -1
  88. package/lib/powered-by.d.ts +1 -1
  89. package/lib/pretty-json.d.ts +1 -1
  90. package/lib/proxy.d.ts +1 -1
  91. package/lib/quick.d.ts +1 -1
  92. package/lib/request-id.d.ts +1 -1
  93. package/lib/request.d.ts +1 -1
  94. package/lib/route.d.ts +1 -1
  95. package/lib/schema-validator.d.ts +1 -1
  96. package/lib/secure-headers.d.ts +1 -1
  97. package/lib/{serve-static-Dw_m6KxR.js → serve-static-Cw23shXv.js} +1 -0
  98. package/lib/serve-static.d.ts +1 -1
  99. package/lib/streaming.d.ts +1 -1
  100. package/lib/testing.d.ts +4 -4
  101. package/lib/timeout.d.ts +1 -1
  102. package/lib/timing.d.ts +1 -1
  103. package/lib/tiny.d.ts +1 -1
  104. package/lib/trailing-slash.d.ts +1 -1
  105. package/lib/{types-Bd29gh2o.d.ts → types-CQiGmsWW.d.ts} +3 -3
  106. package/lib/{types-D9DEXMjE.d.ts → types-Lqx5BxQF.d.ts} +8 -1
  107. package/lib/{types-ZM9gIiKh.d.ts → types-nEL8VPDC.d.ts} +1 -1
  108. package/lib/types.d.ts +1 -1
  109. package/lib/utils/body.d.ts +1 -1
  110. package/lib/utils/cookie.d.ts +1 -1
  111. package/lib/utils/crypto.d.ts +1 -1
  112. package/lib/utils/html.d.ts +1 -45
  113. package/lib/utils/ipaddr.d.ts +1 -1
  114. package/lib/utils/jwt.d.ts +1 -1
  115. package/lib/utils/types.d.ts +2 -2
  116. package/lib/validator.d.ts +2 -2
  117. package/lib/ws.d.ts +1 -1
  118. package/package.json +45 -1
  119. package/plugins/acryl-inspect/upstream.json +1 -1
  120. package/plugins/acryl-node-server/upstream.json +1 -1
  121. package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +506 -0
  122. package/plugins/acryl-router/src/core/helper/css/common.ts +337 -0
  123. package/plugins/acryl-router/src/core/helper/css/index.test.tsx +678 -0
  124. package/plugins/acryl-router/src/core/helper/css/index.ts +262 -0
  125. package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +557 -0
  126. package/plugins/acryl-router/src/core/jsx/base.test.tsx +97 -0
  127. package/plugins/acryl-router/src/core/jsx/base.ts +490 -0
  128. package/plugins/acryl-router/src/core/jsx/children.test.ts +52 -0
  129. package/plugins/acryl-router/src/core/jsx/children.ts +20 -0
  130. package/plugins/acryl-router/src/core/jsx/components.test.tsx +936 -0
  131. package/plugins/acryl-router/src/core/jsx/components.ts +257 -0
  132. package/plugins/acryl-router/src/core/jsx/constants.ts +6 -0
  133. package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +655 -0
  134. package/plugins/acryl-router/src/core/jsx/context.ts +266 -0
  135. package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +128 -0
  136. package/plugins/acryl-router/src/core/jsx/dom/client.ts +89 -0
  137. package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +330 -0
  138. package/plugins/acryl-router/src/core/jsx/dom/components.ts +39 -0
  139. package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +184 -0
  140. package/plugins/acryl-router/src/core/jsx/dom/context.ts +52 -0
  141. package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +149 -0
  142. package/plugins/acryl-router/src/core/jsx/dom/css.ts +263 -0
  143. package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +172 -0
  144. package/plugins/acryl-router/src/core/jsx/dom/hooks/index.ts +91 -0
  145. package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +3681 -0
  146. package/plugins/acryl-router/src/core/jsx/dom/index.ts +170 -0
  147. package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +1344 -0
  148. package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.ts +438 -0
  149. package/plugins/acryl-router/src/core/jsx/dom/jsx-dev-runtime.ts +23 -0
  150. package/plugins/acryl-router/src/core/jsx/dom/jsx-runtime.ts +8 -0
  151. package/plugins/acryl-router/src/core/jsx/dom/render.ts +879 -0
  152. package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +199 -0
  153. package/plugins/acryl-router/src/core/jsx/dom/server.ts +76 -0
  154. package/plugins/acryl-router/src/core/jsx/dom/utils.ts +7 -0
  155. package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +859 -0
  156. package/plugins/acryl-router/src/core/jsx/hooks/index.ts +437 -0
  157. package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +40 -0
  158. package/plugins/acryl-router/src/core/jsx/index.test.tsx +1744 -0
  159. package/plugins/acryl-router/src/core/jsx/index.ts +115 -0
  160. package/plugins/acryl-router/src/core/jsx/intrinsic-element/common.ts +23 -0
  161. package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +666 -0
  162. package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.ts +210 -0
  163. package/plugins/acryl-router/src/core/jsx/intrinsic-elements.ts +938 -0
  164. package/plugins/acryl-router/src/core/jsx/jsx-dev-runtime.ts +26 -0
  165. package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +85 -0
  166. package/plugins/acryl-router/src/core/jsx/jsx-runtime.ts +51 -0
  167. package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +1281 -0
  168. package/plugins/acryl-router/src/core/jsx/streaming.ts +231 -0
  169. package/plugins/acryl-router/src/core/jsx/types.ts +41 -0
  170. package/plugins/acryl-router/src/core/jsx/utils.test.ts +366 -0
  171. package/plugins/acryl-router/src/core/jsx/utils.ts +202 -0
  172. package/plugins/acryl-router/src/core/utils/types.ts +6 -0
  173. package/plugins/acryl-router/src/helpers/index.ts +1 -0
  174. package/plugins/acryl-router/tests/jsx.spec.tsx +63 -0
  175. package/plugins/acryl-router/upstream.json +3 -3
  176. package/plugins/acryl-router-http/upstream.json +1 -1
  177. package/plugins/acryl-state/upstream.json +1 -1
  178. package/plugins/desktop/upstream.json +1 -1
  179. package/plugins/desktop-web/upstream.json +1 -1
  180. package/plugins/save/plugin.json +1 -1
  181. package/templates/todo/upstream.json +1 -1
@@ -0,0 +1,859 @@
1
+ /** @jsxImportSource ../ */
2
+ import { JSDOM } from 'jsdom'
3
+ import {
4
+ createRef,
5
+ forwardRef,
6
+ startTransition,
7
+ startViewTransition,
8
+ use,
9
+ useDebugValue,
10
+ useDeferredValue,
11
+ useId,
12
+ useImperativeHandle,
13
+ useLayoutEffect,
14
+ useReducer,
15
+ useState,
16
+ useSyncExternalStore,
17
+ useTransition,
18
+ useViewTransition,
19
+ } from './index.ts'
20
+ // run tests by old style jsx default
21
+ // acryl-router/jsx/jsx-runtime and acryl-router/jsx/dom/jsx-runtime are tested in their respective settings
22
+ import { ErrorBoundary, Suspense, render } from '../dom/index.ts'
23
+
24
+ describe('Hooks', () => {
25
+ beforeAll(() => {
26
+ global.requestAnimationFrame = (cb) => setTimeout(cb)
27
+ })
28
+
29
+ let dom: JSDOM
30
+ let root: HTMLElement
31
+ beforeEach(() => {
32
+ dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
33
+ runScripts: 'dangerously',
34
+ })
35
+ global.document = dom.window.document
36
+ global.HTMLElement = dom.window.HTMLElement
37
+ global.SVGElement = dom.window.SVGElement
38
+ global.Text = dom.window.Text
39
+ root = document.getElementById('root') as HTMLElement
40
+ })
41
+
42
+ describe('useReducer()', () => {
43
+ it('simple', async () => {
44
+ const reducer = (state: number, action: number) => state + action
45
+ const functions: Function[] = []
46
+ const App = () => {
47
+ const [state, dispatch] = useReducer(reducer, 0)
48
+ functions.push(dispatch)
49
+ return (
50
+ <div>
51
+ <button onClick={() => dispatch(1)}>{state}</button>
52
+ </div>
53
+ )
54
+ }
55
+ render(<App />, root)
56
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
57
+ root.querySelector('button')?.click()
58
+ await Promise.resolve()
59
+ expect(root.innerHTML).toBe('<div><button>1</button></div>')
60
+ expect(functions[0]).toBe(functions[1])
61
+ })
62
+ })
63
+
64
+ describe('startTransition()', () => {
65
+ it('no error', async () => {
66
+ const App = () => {
67
+ const [count, setCount] = useState(0)
68
+ return (
69
+ <Suspense fallback={<div>Loading...</div>}>
70
+ <div>
71
+ <button
72
+ onClick={() => {
73
+ startTransition(() => {
74
+ setCount((c) => c + 1)
75
+ })
76
+ }}
77
+ >
78
+ {count}
79
+ </button>
80
+ </div>
81
+ </Suspense>
82
+ )
83
+ }
84
+ render(<App />, root)
85
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
86
+ root.querySelector('button')?.click()
87
+ await Promise.resolve()
88
+ expect(root.innerHTML).toBe('<div><button>1</button></div>')
89
+ })
90
+
91
+ it('got an error', async () => {
92
+ let resolve: () => void
93
+ const promise = new Promise<void>((r) => (resolve = r))
94
+
95
+ const Counter = ({ count }: { count: number }) => {
96
+ use(promise)
97
+ return <div>{count}</div>
98
+ }
99
+
100
+ const App = () => {
101
+ const [count, setCount] = useState(0)
102
+ return (
103
+ <Suspense fallback={<div>Loading...</div>}>
104
+ <div>
105
+ <button
106
+ onClick={() => {
107
+ startTransition(() => {
108
+ setCount((c) => c + 1)
109
+ })
110
+ }}
111
+ >
112
+ {count ? <Counter count={count} /> : count}
113
+ </button>
114
+ </div>
115
+ </Suspense>
116
+ )
117
+ }
118
+ render(<App />, root)
119
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
120
+ root.querySelector('button')?.click()
121
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
122
+ resolve!()
123
+ await new Promise((r) => setTimeout(r))
124
+ expect(root.innerHTML).toBe('<div><button><div>1</div></button></div>')
125
+ })
126
+ })
127
+
128
+ describe('useTransition()', () => {
129
+ it('pending', async () => {
130
+ let called = 0
131
+ const App = () => {
132
+ const [count, setCount] = useState(0)
133
+ const [isPending, startTransition] = useTransition()
134
+ called++
135
+
136
+ return (
137
+ <div>
138
+ <button
139
+ onClick={() => {
140
+ startTransition(() => {
141
+ setCount((c) => c + 1)
142
+ })
143
+ }}
144
+ >
145
+ {isPending ? 'Pending...' : count}
146
+ </button>
147
+ </div>
148
+ )
149
+ }
150
+ render(<App />, root)
151
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
152
+ root.querySelector('button')?.click()
153
+ await Promise.resolve()
154
+ expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')
155
+ expect(called).toBe(2)
156
+ await new Promise((r) => setTimeout(r))
157
+ await new Promise((r) => setTimeout(r))
158
+ await new Promise((r) => setTimeout(r))
159
+ expect(root.innerHTML).toBe('<div><button>1</button></div>')
160
+ expect(called).toBe(3)
161
+ })
162
+
163
+ it('pending', async () => {
164
+ let resolve: (() => void) | undefined
165
+ const promise = new Promise<void>((r) => (resolve = r))
166
+ let called = 0
167
+ const App = () => {
168
+ const [isPending, startTransition] = useTransition()
169
+ called++
170
+
171
+ return (
172
+ <div>
173
+ <button
174
+ onClick={() => {
175
+ startTransition(async () => await promise)
176
+ }}
177
+ >
178
+ {isPending ? 'Pending...' : 'Click me'}
179
+ </button>
180
+ </div>
181
+ )
182
+ }
183
+ render(<App />, root)
184
+ expect(root.innerHTML).toBe('<div><button>Click me</button></div>')
185
+ root.querySelector('button')?.click()
186
+ await Promise.resolve()
187
+ expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')
188
+ expect(called).toBe(2)
189
+ resolve!()
190
+ await new Promise((r) => setTimeout(r))
191
+ await new Promise((r) => setTimeout(r))
192
+ await new Promise((r) => setTimeout(r))
193
+ expect(root.innerHTML).toBe('<div><button>Click me</button></div>')
194
+ expect(called).toBe(3)
195
+ })
196
+
197
+ it('pending - error', async () => {
198
+ let reject: (() => void) | undefined
199
+ const promise = new Promise<void>((_, r) => (reject = r))
200
+ let called = 0
201
+ const Component = () => {
202
+ const [isPending, startTransition] = useTransition()
203
+ called++
204
+
205
+ return (
206
+ <div>
207
+ <button
208
+ onClick={() => {
209
+ startTransition(async () => await promise)
210
+ }}
211
+ >
212
+ {isPending ? 'Pending...' : 'Click me'}
213
+ </button>
214
+ </div>
215
+ )
216
+ }
217
+ const App = () => (
218
+ <ErrorBoundary fallback={<div>Error</div>}>
219
+ <Component />
220
+ </ErrorBoundary>
221
+ )
222
+ render(<App />, root)
223
+ expect(root.innerHTML).toBe('<div><button>Click me</button></div>')
224
+ root.querySelector('button')?.click()
225
+ await Promise.resolve()
226
+ expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')
227
+ expect(called).toBe(2)
228
+ reject!()
229
+ await new Promise((r) => setTimeout(r))
230
+ await new Promise((r) => setTimeout(r))
231
+ await new Promise((r) => setTimeout(r))
232
+ expect(root.innerHTML).toBe('<div>Error</div>')
233
+ expect(called).toBe(2)
234
+ })
235
+
236
+ it('multiple setState at once', async () => {
237
+ let called = 0
238
+ const App = () => {
239
+ const [count1, setCount1] = useState(0)
240
+ const [count2, setCount2] = useState(0)
241
+ const [isPending, startTransition] = useTransition()
242
+ called++
243
+
244
+ return (
245
+ <div>
246
+ <button
247
+ onClick={() => {
248
+ startTransition(() => {
249
+ setCount1((c) => c + 1)
250
+ setCount2((c) => c + 2)
251
+ })
252
+ }}
253
+ >
254
+ {isPending ? 'Pending...' : count1 + count2}
255
+ </button>
256
+ </div>
257
+ )
258
+ }
259
+ render(<App />, root)
260
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
261
+ root.querySelector('button')?.click()
262
+ await Promise.resolve()
263
+ expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')
264
+ expect(called).toBe(2)
265
+ await new Promise((r) => setTimeout(r))
266
+ await new Promise((r) => setTimeout(r))
267
+ await new Promise((r) => setTimeout(r))
268
+ expect(root.innerHTML).toBe('<div><button>3</button></div>')
269
+ expect(called).toBe(3)
270
+ })
271
+
272
+ it('multiple startTransaction at once', async () => {
273
+ let called = 0
274
+ const App = () => {
275
+ const [count1, setCount1] = useState(0)
276
+ const [count2, setCount2] = useState(0)
277
+ const [isPending, startTransition] = useTransition()
278
+ called++
279
+
280
+ return (
281
+ <div>
282
+ <button
283
+ onClick={() => {
284
+ startTransition(() => {
285
+ setCount1((c) => c + 1)
286
+ })
287
+ startTransition(() => {
288
+ setCount2((c) => c + 2)
289
+ })
290
+ }}
291
+ >
292
+ {isPending ? 'Pending...' : count1 + count2}
293
+ </button>
294
+ </div>
295
+ )
296
+ }
297
+ render(<App />, root)
298
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
299
+ expect(called).toBe(1)
300
+ root.querySelector('button')?.click()
301
+ await Promise.resolve()
302
+ expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')
303
+ expect(called).toBe(2)
304
+ await new Promise((r) => setTimeout(r))
305
+ await new Promise((r) => setTimeout(r))
306
+ await new Promise((r) => setTimeout(r))
307
+ expect(root.innerHTML).toBe('<div><button>3</button></div>')
308
+ expect(called).toBe(3) // + isPending=true + isPending=false
309
+ })
310
+ })
311
+
312
+ describe('useDeferredValue()', () => {
313
+ it('deferred', async () => {
314
+ const promiseMap = {} as Record<number, Promise<number>>
315
+ const getPromise = (count: number) => {
316
+ return (promiseMap[count] ||= new Promise((r) => setTimeout(() => r(count + 1))))
317
+ }
318
+ const ShowCount = ({ count }: { count: number }) => {
319
+ if (count === 0) {
320
+ return <div>0</div>
321
+ }
322
+
323
+ const c = use(getPromise(count))
324
+ return <div>{c}</div>
325
+ }
326
+
327
+ const App = () => {
328
+ const [count, setCount] = useState(0)
329
+ const c = useDeferredValue(count)
330
+ return (
331
+ <>
332
+ <div>
333
+ <button onClick={() => setCount((c) => c + 1)}>+1</button>
334
+ </div>
335
+ <Suspense fallback={<div>Loading...</div>}>
336
+ <ShowCount count={c} />
337
+ </Suspense>
338
+ </>
339
+ )
340
+ }
341
+ render(<App />, root)
342
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')
343
+ root.querySelector('button')?.click()
344
+ await Promise.resolve()
345
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')
346
+ await new Promise((r) => setTimeout(r))
347
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')
348
+ await new Promise((r) => setTimeout(r))
349
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')
350
+ await new Promise((r) => setTimeout(r))
351
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>0</div>')
352
+ await new Promise((r) => setTimeout(r))
353
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>2</div>')
354
+ })
355
+
356
+ it('initial value', async () => {
357
+ const promiseMap = {} as Record<number, Promise<number>>
358
+ const getPromise = (count: number) => {
359
+ return (promiseMap[count] ||= new Promise((r) => setTimeout(() => r(count + 1))))
360
+ }
361
+ const ShowCount = ({ count }: { count: number }) => {
362
+ if (count === 0 || count === 99) {
363
+ return <div>{count}</div>
364
+ }
365
+
366
+ const c = use(getPromise(count))
367
+ return <div>{c}</div>
368
+ }
369
+
370
+ const App = () => {
371
+ const [count, setCount] = useState(1)
372
+ const c = useDeferredValue(count, 99)
373
+ return (
374
+ <>
375
+ <div>
376
+ <button onClick={() => setCount((c) => c + 1)}>+1</button>
377
+ </div>
378
+ <Suspense fallback={<div>Loading...</div>}>
379
+ <ShowCount count={c} />
380
+ </Suspense>
381
+ </>
382
+ )
383
+ }
384
+ render(<App />, root)
385
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>99</div>')
386
+ await new Promise((r) => setTimeout(r))
387
+ await new Promise((r) => setTimeout(r))
388
+ await new Promise((r) => setTimeout(r))
389
+ await new Promise((r) => setTimeout(r))
390
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>2</div>')
391
+ root.querySelector('button')?.click()
392
+ await new Promise((r) => setTimeout(r))
393
+ await new Promise((r) => setTimeout(r))
394
+ await new Promise((r) => setTimeout(r))
395
+ await new Promise((r) => setTimeout(r))
396
+ expect(root.innerHTML).toBe('<div><button>+1</button></div><div>3</div>')
397
+ })
398
+ })
399
+
400
+ describe('startViewTransition()', () => {
401
+ afterEach(() => {
402
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
403
+ delete (dom.window.document as any).startViewTransition
404
+ })
405
+
406
+ it('supported browser', async () => {
407
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
408
+ ;(dom.window.document as any).startViewTransition = vi.fn((cb: Function) => {
409
+ Promise.resolve().then(() => cb())
410
+ return { finished: Promise.resolve() }
411
+ })
412
+
413
+ const App = () => {
414
+ const [count, setCount] = useState(0)
415
+ return (
416
+ <Suspense fallback={<div>Loading...</div>}>
417
+ <div>
418
+ <button
419
+ onClick={() => {
420
+ startViewTransition(() => {
421
+ setCount((c) => c + 1)
422
+ })
423
+ }}
424
+ >
425
+ {count}
426
+ </button>
427
+ </div>
428
+ </Suspense>
429
+ )
430
+ }
431
+ render(<App />, root)
432
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
433
+ root.querySelector('button')?.click()
434
+ await Promise.resolve()
435
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
436
+ await Promise.resolve() // updated in microtask
437
+ expect(root.innerHTML).toBe('<div><button>1</button></div>')
438
+ })
439
+
440
+ it('unsupported browser', async () => {
441
+ const App = () => {
442
+ const [count, setCount] = useState(0)
443
+ return (
444
+ <Suspense fallback={<div>Loading...</div>}>
445
+ <div>
446
+ <button
447
+ onClick={() => {
448
+ startViewTransition(() => {
449
+ setCount((c) => c + 1)
450
+ })
451
+ }}
452
+ >
453
+ {count}
454
+ </button>
455
+ </div>
456
+ </Suspense>
457
+ )
458
+ }
459
+ render(<App />, root)
460
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
461
+ root.querySelector('button')?.click()
462
+ await Promise.resolve()
463
+ expect(root.innerHTML).toBe('<div><button>1</button></div>')
464
+ })
465
+
466
+ it('with useTransition()', async () => {
467
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
468
+ ;(dom.window.document as any).startViewTransition = vi.fn((cb: Function) => {
469
+ Promise.resolve().then(() => cb())
470
+ return { finished: Promise.resolve() }
471
+ })
472
+
473
+ let called = 0
474
+ const App = () => {
475
+ const [count, setCount] = useState(0)
476
+ const [isPending, startTransition] = useTransition()
477
+ called++
478
+
479
+ return (
480
+ <div>
481
+ <button
482
+ onClick={() => {
483
+ startViewTransition(() => {
484
+ startTransition(() => {
485
+ setCount((c) => c + 1)
486
+ })
487
+ })
488
+ }}
489
+ >
490
+ {isPending ? 'Pending...' : count}
491
+ </button>
492
+ </div>
493
+ )
494
+ }
495
+ render(<App />, root)
496
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
497
+ root.querySelector('button')?.click()
498
+ await new Promise((r) => setTimeout(r))
499
+ expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')
500
+ expect(called).toBe(2)
501
+ await new Promise((r) => setTimeout(r))
502
+ await new Promise((r) => setTimeout(r))
503
+ expect(root.innerHTML).toBe('<div><button>1</button></div>')
504
+ expect(called).toBe(3)
505
+ })
506
+ })
507
+
508
+ describe('useViewTransition()', () => {
509
+ afterEach(() => {
510
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
511
+ delete (dom.window.document as any).startViewTransition
512
+ })
513
+
514
+ it('supported browser', async () => {
515
+ let resolved: (() => void) | undefined
516
+ const promise = new Promise<void>((r) => (resolved = r))
517
+ let called = 0
518
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
519
+ ;(global.document as any).startViewTransition = vi.fn((cb: Function) => {
520
+ Promise.resolve().then(() => cb())
521
+ return { finished: promise }
522
+ })
523
+
524
+ const App = () => {
525
+ const [count, setCount] = useState(0)
526
+ const [isUpdating, startViewTransition] = useViewTransition()
527
+ called++
528
+
529
+ return (
530
+ <div>
531
+ <button
532
+ onClick={() => {
533
+ startViewTransition(() => {
534
+ setCount((c) => c + 1)
535
+ })
536
+ }}
537
+ >
538
+ {isUpdating ? 'Pending...' : count}
539
+ </button>
540
+ </div>
541
+ )
542
+ }
543
+ render(<App />, root)
544
+ expect(root.innerHTML).toBe('<div><button>0</button></div>')
545
+ root.querySelector('button')?.click()
546
+ await new Promise((r) => setTimeout(r))
547
+ expect(root.innerHTML).toBe('<div><button>Pending...</button></div>')
548
+ expect(called).toBe(2)
549
+ resolved?.()
550
+ await new Promise((r) => setTimeout(r))
551
+ expect(root.innerHTML).toBe('<div><button>1</button></div>')
552
+ expect(called).toBe(3)
553
+ })
554
+ })
555
+
556
+ describe('useId()', () => {
557
+ let dom: JSDOM
558
+ let root: HTMLElement
559
+ beforeEach(() => {
560
+ dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
561
+ runScripts: 'dangerously',
562
+ })
563
+ global.document = dom.window.document
564
+ global.HTMLElement = dom.window.HTMLElement
565
+ global.Text = dom.window.Text
566
+ root = document.getElementById('root') as HTMLElement
567
+ })
568
+
569
+ it('simple', () => {
570
+ const App = () => {
571
+ const id = useId()
572
+ return <div id={id} />
573
+ }
574
+ render(<App />, root)
575
+ expect(root.innerHTML).toBe('<div id=":r0:"></div>')
576
+ })
577
+
578
+ it('memoized', async () => {
579
+ let setCount: (c: number) => void = () => {}
580
+ const App = () => {
581
+ const id = useId()
582
+ const [count, _setCount] = useState(0)
583
+ setCount = _setCount
584
+ return <div id={id}>{count}</div>
585
+ }
586
+ render(<App />, root)
587
+ expect(root.innerHTML).toBe('<div id=":r1:">0</div>')
588
+ setCount(1)
589
+ await Promise.resolve()
590
+ expect(root.innerHTML).toBe('<div id=":r1:">1</div>')
591
+ })
592
+ })
593
+
594
+ describe('useDebugValue()', () => {
595
+ it('simple', () => {
596
+ const spy = vi.fn()
597
+ const App = () => {
598
+ useDebugValue('hello', spy)
599
+ return <div />
600
+ }
601
+ render(<App />, root)
602
+ expect(root.innerHTML).toBe('<div></div>')
603
+ expect(spy).not.toBeCalled()
604
+ })
605
+ })
606
+
607
+ describe('createRef()', () => {
608
+ it('simple', () => {
609
+ const ref: { current: HTMLElement | null } = createRef<HTMLDivElement>()
610
+ const App = () => {
611
+ return <div ref={ref} />
612
+ }
613
+ render(<App />, root)
614
+ expect(root.innerHTML).toBe('<div></div>')
615
+ expect(ref.current).toBeInstanceOf(HTMLElement)
616
+ })
617
+ })
618
+
619
+ describe('forwardRef()', () => {
620
+ it('simple', () => {
621
+ const ref: { current: HTMLElement | null } = createRef<HTMLDivElement>()
622
+ const App = forwardRef((props, ref) => {
623
+ return <div {...props} ref={ref} />
624
+ })
625
+ render(<App ref={ref} />, root)
626
+ expect(root.innerHTML).toBe('<div></div>')
627
+ expect(ref.current).toBeInstanceOf(HTMLElement)
628
+ })
629
+
630
+ it('can run without ref', () => {
631
+ const App = forwardRef((props) => {
632
+ return <div {...props} />
633
+ })
634
+ render(<App />, root)
635
+ expect(root.innerHTML).toBe('<div></div>')
636
+ })
637
+ })
638
+
639
+ describe('useImperativeHandle()', () => {
640
+ it('simple', async () => {
641
+ const ref: { current: { focus: () => void } | null } = createRef()
642
+ const SubApp = () => {
643
+ useImperativeHandle(
644
+ ref,
645
+ () => ({
646
+ focus: () => {
647
+ console.log('focus')
648
+ },
649
+ }),
650
+ []
651
+ )
652
+ return <div />
653
+ }
654
+ const App = () => {
655
+ const [show, setShow] = useState(true)
656
+ return (
657
+ <>
658
+ {show && <SubApp />}
659
+ <button onClick={() => setShow((s) => !s)}>toggle</button>
660
+ </>
661
+ )
662
+ }
663
+ render(<App />, root)
664
+ expect(ref.current).toBe(null)
665
+ await new Promise((r) => setTimeout(r))
666
+ expect(ref.current).toEqual({ focus: expect.any(Function) })
667
+ root.querySelector('button')?.click()
668
+ await new Promise((r) => setTimeout(r))
669
+ expect(ref.current).toBe(null)
670
+ })
671
+ })
672
+
673
+ describe('useSyncExternalStore()', () => {
674
+ it('simple', async () => {
675
+ let count = 0
676
+ const unsubscribe = vi.fn()
677
+ const subscribe = vi.fn(() => unsubscribe)
678
+ const getSnapshot = vi.fn(() => count)
679
+ const SubApp = () => {
680
+ const count = useSyncExternalStore(subscribe, getSnapshot)
681
+ return <div>{count}</div>
682
+ }
683
+ const App = () => {
684
+ const [show, setShow] = useState(true)
685
+ return (
686
+ <>
687
+ {show && <SubApp />}
688
+ <button onClick={() => setShow((s) => !s)}>toggle</button>
689
+ </>
690
+ )
691
+ }
692
+ render(<App />, root)
693
+ expect(root.innerHTML).toBe('<div>0</div><button>toggle</button>')
694
+ await new Promise((r) => setTimeout(r))
695
+ root.querySelector('button')?.click()
696
+ await new Promise((r) => setTimeout(r))
697
+ expect(root.innerHTML).toBe('<button>toggle</button>')
698
+ expect(unsubscribe).toBeCalled()
699
+ count = 1
700
+ root.querySelector('button')?.click()
701
+ await new Promise((r) => setTimeout(r))
702
+ expect(root.innerHTML).toBe('<div>1</div><button>toggle</button>')
703
+ })
704
+
705
+ it('updates the snapshot when subscribe changes', async () => {
706
+ const unsubscribeA = vi.fn()
707
+ const subscribeA = vi.fn(() => unsubscribeA)
708
+ const unsubscribeB = vi.fn()
709
+ const subscribeB = vi.fn(() => unsubscribeB)
710
+ const stores = [
711
+ { subscribe: subscribeA, getSnapshot: () => 'a1' },
712
+ { subscribe: subscribeB, getSnapshot: () => 'b0' },
713
+ ]
714
+
715
+ const App = () => {
716
+ const [index, setIndex] = useState(0)
717
+ const store = stores[index]
718
+ const value = useSyncExternalStore(store.subscribe, store.getSnapshot)
719
+ return <button onClick={() => setIndex(1)}>{value}</button>
720
+ }
721
+
722
+ render(<App />, root)
723
+ await new Promise((r) => setTimeout(r))
724
+ expect(root.innerHTML).toBe('<button>a1</button>')
725
+ expect(subscribeA).toBeCalledTimes(1)
726
+
727
+ root.querySelector('button')?.click()
728
+ await Promise.resolve()
729
+ expect(root.innerHTML).toBe('<button>b0</button>')
730
+ // the old store stays subscribed until the new subscription is set up at effect flush
731
+ expect(unsubscribeA).not.toBeCalled()
732
+ await new Promise((r) => setTimeout(r))
733
+ expect(unsubscribeA).toBeCalledTimes(1)
734
+ expect(subscribeB).toBeCalledTimes(1)
735
+ expect(unsubscribeB).not.toBeCalled()
736
+ })
737
+
738
+ it('updates the snapshot when getSnapshot changes', async () => {
739
+ const state: Record<string, string> = { a: 'a0', b: 'b0' }
740
+ const listeners = new Set<() => void>()
741
+ const subscribe = vi.fn((listener: () => void) => {
742
+ listeners.add(listener)
743
+ return () => listeners.delete(listener)
744
+ })
745
+
746
+ const App = () => {
747
+ const [key, setKey] = useState('a')
748
+ const value = useSyncExternalStore(subscribe, () => state[key])
749
+ return <button onClick={() => setKey('b')}>{value}</button>
750
+ }
751
+
752
+ render(<App />, root)
753
+ await new Promise((r) => setTimeout(r))
754
+ expect(root.innerHTML).toBe('<button>a0</button>')
755
+ expect(subscribe).toBeCalledTimes(1)
756
+
757
+ root.querySelector('button')?.click()
758
+ await new Promise((r) => setTimeout(r))
759
+ expect(root.innerHTML).toBe('<button>b0</button>')
760
+ expect(subscribe).toBeCalledTimes(1)
761
+
762
+ state.b = 'b1'
763
+ listeners.forEach((listener) => listener())
764
+ await new Promise((r) => setTimeout(r))
765
+ expect(root.innerHTML).toBe('<button>b1</button>')
766
+ })
767
+
768
+ it('uses the latest getSnapshot when subscribing after a layout effect update', async () => {
769
+ const state = { a: 'a0', b: 'b0' }
770
+ const subscribe = vi.fn((_listener: () => void) => vi.fn())
771
+
772
+ const App = () => {
773
+ const [key, setKey] = useState<keyof typeof state>('a')
774
+ const value = useSyncExternalStore(subscribe, () => state[key])
775
+
776
+ useLayoutEffect(() => {
777
+ setKey('b')
778
+ queueMicrotask(() => {
779
+ state.b = 'b1'
780
+ })
781
+ }, [])
782
+
783
+ return <div>{value}</div>
784
+ }
785
+
786
+ render(<App />, root)
787
+ expect(root.innerHTML).toBe('<div>a0</div>')
788
+
789
+ await new Promise((r) => setTimeout(r))
790
+ expect(root.innerHTML).toBe('<div>b1</div>')
791
+ })
792
+
793
+ it('does not loop when subscribe changes on every render', async () => {
794
+ const unsubscribes: ReturnType<typeof vi.fn>[] = []
795
+ const subscribe = vi.fn((_listener: () => void) => {
796
+ const unsubscribe = vi.fn()
797
+ unsubscribes.push(unsubscribe)
798
+ return unsubscribe
799
+ })
800
+ const getSnapshot = () => 'snapshot'
801
+ let renderCount = 0
802
+
803
+ const App = () => {
804
+ const [count, setCount] = useState(0)
805
+ renderCount++
806
+ const value = useSyncExternalStore((listener) => subscribe(listener), getSnapshot)
807
+ return <button onClick={() => setCount((count) => count + 1)}>{`${value}:${count}`}</button>
808
+ }
809
+
810
+ render(<App />, root)
811
+ await new Promise((r) => setTimeout(r))
812
+ expect(root.innerHTML).toBe('<button>snapshot:0</button>')
813
+ expect(renderCount).toBe(1)
814
+ expect(subscribe).toBeCalledTimes(1)
815
+
816
+ root.querySelector('button')?.click()
817
+ await Promise.resolve()
818
+ await new Promise((r) => setTimeout(r))
819
+ expect(root.innerHTML).toBe('<button>snapshot:1</button>')
820
+ expect(renderCount).toBe(2)
821
+ expect(subscribe).toBeCalledTimes(2)
822
+ expect(unsubscribes[0]).toBeCalledTimes(1)
823
+ expect(unsubscribes[1]).not.toBeCalled()
824
+ })
825
+
826
+ it('with getServerSnapshot', async () => {
827
+ let count = 0
828
+ const unsubscribe = vi.fn()
829
+ const subscribe = vi.fn(() => unsubscribe)
830
+ const getSnapshot = vi.fn(() => count)
831
+ const getServerSnapshot = vi.fn(() => 100)
832
+ const SubApp = () => {
833
+ const count = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
834
+ return <div>{count}</div>
835
+ }
836
+ const App = () => {
837
+ const [show, setShow] = useState(true)
838
+ return (
839
+ <>
840
+ {show && <SubApp />}
841
+ <button onClick={() => setShow((s) => !s)}>toggle</button>
842
+ </>
843
+ )
844
+ }
845
+ render(<App />, root)
846
+ expect(root.innerHTML).toBe('<div>100</div><button>toggle</button>')
847
+ await new Promise((r) => setTimeout(r))
848
+ expect(root.innerHTML).toBe('<div>0</div><button>toggle</button>')
849
+ root.querySelector('button')?.click()
850
+ await new Promise((r) => setTimeout(r))
851
+ expect(root.innerHTML).toBe('<button>toggle</button>')
852
+ expect(unsubscribe).toBeCalled()
853
+ count = 1
854
+ root.querySelector('button')?.click()
855
+ await new Promise((r) => setTimeout(r))
856
+ expect(root.innerHTML).toBe('<div>1</div><button>toggle</button>')
857
+ })
858
+ })
859
+ })