acrylblends 0.6.3 → 0.7.0
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.
- package/README.md +4 -3
- package/docs/framework/README.md +2 -2
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/guides/jsx.md +51 -1
- package/docs/framework/helpers/ssg.md +3 -6
- package/docs/framework/middleware/builtin/jsx-renderer.md +0 -3
- package/docs/framework/upstream.json +2 -2
- package/lib/accepts.d.ts +1 -1
- package/lib/acryl-base.d.ts +1 -1
- package/lib/adapter.d.ts +1 -1
- package/lib/{app-wFwpg-bt.d.ts → app-D8riatvv.d.ts} +1 -1
- package/lib/{base-ddiVmiE7.js → base-NSN_SNv-.js} +1 -0
- package/lib/basic-auth.d.ts +1 -1
- package/lib/bearer-auth.d.ts +1 -1
- package/lib/body-limit.d.ts +1 -1
- package/lib/cache.d.ts +1 -1
- package/lib/cache.js +4 -4
- package/lib/{client-BHTr318g.js → client-DDLXwhBo.js} +2 -125
- package/lib/client.d.ts +2 -2
- package/lib/client.js +2 -1
- package/lib/combine.d.ts +1 -1
- package/lib/{components-DYZdxJIJ.js → components-JXVm45kK.js} +1 -1
- package/lib/{components-Ce5FRyHU.js → components-cEW3ovK0.js} +3 -3
- package/lib/compress.d.ts +1 -1
- package/lib/conninfo.d.ts +2 -2
- package/lib/{context-DVQBjuns.d.ts → context-DS3fYyG-.d.ts} +1 -1
- package/lib/context-storage.d.ts +1 -1
- package/lib/cookie.d.ts +1 -1
- package/lib/cors.d.ts +1 -1
- package/lib/csrf.d.ts +1 -1
- package/lib/dev.d.ts +2 -2
- package/lib/etag.d.ts +1 -1
- package/lib/factory.d.ts +2 -2
- package/lib/{hooks-Bq5q9IMl.js → hooks-w72iLeFR.js} +6 -6
- package/lib/{index-CUTfU69R.d.ts → index-Bb7YJKCq.d.ts} +2 -2
- package/lib/{index-CsvB-F9j.d.ts → index-BvFh1Gfx.d.ts} +1 -1
- package/lib/{index-Cqcdluyn.d.ts → index-CTy4N7ng.d.ts} +2 -2
- package/lib/index.d.ts +4 -4
- package/lib/ip-restriction.d.ts +2 -2
- package/lib/jsx/dom/client.js +1 -1
- package/lib/jsx/dom/jsx-dev-runtime.js +1 -1
- package/lib/jsx/dom/jsx-runtime.js +1 -1
- package/lib/jsx/dom/server.js +2 -2
- package/lib/jsx/dom.js +4 -4
- package/lib/jsx/jsx-dev-runtime.js +1 -1
- package/lib/jsx/jsx-runtime.js +1 -1
- package/lib/jsx/streaming.js +1 -1
- package/lib/{jsx-dev-runtime-kAhR_4C7.js → jsx-dev-runtime-CWLRA2F3.js} +2 -2
- package/lib/jsx-renderer.d.ts +75 -0
- package/lib/jsx-renderer.js +99 -0
- package/lib/jsx.js +4 -4
- package/lib/jwk.d.ts +1 -1
- package/lib/jwt.d.ts +1 -1
- package/lib/language.d.ts +1 -1
- package/lib/logger.d.ts +1 -1
- package/lib/loopback-only.d.ts +1 -1
- package/lib/method-not-allowed.d.ts +2 -2
- package/lib/method-override.d.ts +2 -2
- package/lib/mount.d.ts +1 -1
- package/lib/node-server/conninfo.d.ts +1 -1
- package/lib/node-server/register.d.ts +1 -0
- package/lib/node-server/register.js +73 -0
- package/lib/node-server/serve-static.d.ts +1 -1
- package/lib/node-server/serve-static.js +1 -1
- package/lib/node-server.d.ts +1 -1
- package/lib/node-server.js +1 -1
- package/lib/powered-by.d.ts +1 -1
- package/lib/pretty-json.d.ts +1 -1
- package/lib/quick.d.ts +1 -1
- package/lib/request-id.d.ts +1 -1
- package/lib/request.d.ts +1 -1
- package/lib/route.d.ts +1 -1
- package/lib/schema-validator.d.ts +1 -1
- package/lib/secure-headers.d.ts +1 -1
- package/lib/{serve-static-Cw23shXv.js → serve-static-B14MF1Va.js} +1 -0
- package/lib/serve-static.d.ts +1 -1
- package/lib/ssg-BwjcA-Ge.js +389 -0
- package/lib/ssg.d.ts +156 -0
- package/lib/ssg.js +2 -0
- package/lib/streaming.d.ts +1 -1
- package/lib/testing.d.ts +3 -3
- package/lib/testing.js +1 -1
- package/lib/timeout.d.ts +1 -1
- package/lib/timing.d.ts +1 -1
- package/lib/tiny.d.ts +1 -1
- package/lib/trailing-slash.d.ts +1 -1
- package/lib/{types-nEL8VPDC.d.ts → types-Dv_nV2cv.d.ts} +1 -1
- package/lib/{types-CQiGmsWW.d.ts → types-zNxyEpG2.d.ts} +2 -2
- package/lib/types.d.ts +1 -1
- package/lib/utils/body.d.ts +1 -1
- package/lib/utils/ipaddr.d.ts +1 -1
- package/lib/utils-Cx61GFMC.js +125 -0
- package/lib/validator.d.ts +1 -1
- package/lib/ws.d.ts +1 -1
- package/package.json +13 -1
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/src/register.ts +86 -0
- package/plugins/acryl-node-server/upstream.json +5 -3
- package/plugins/acryl-router/src/core/helper/ssg/index.ts +14 -0
- package/plugins/acryl-router/src/core/helper/ssg/middleware.ts +83 -0
- package/plugins/acryl-router/src/core/helper/ssg/plugins.ts +72 -0
- package/plugins/acryl-router/src/core/helper/ssg/ssg.ts +471 -0
- package/plugins/acryl-router/src/core/helper/ssg/utils.ts +118 -0
- package/plugins/acryl-router/src/core/jsx/dom/render.ts +6 -6
- package/plugins/acryl-router/src/core/middleware/cache/index.ts +4 -4
- package/plugins/acryl-router/src/core/middleware/jsx-renderer/index.ts +162 -0
- package/plugins/acryl-router/src/helpers/index.ts +1 -0
- package/plugins/acryl-router/src/middleware/index.ts +1 -0
- package/plugins/acryl-router/upstream.json +2 -2
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/main.ts +4 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/main.ts +3 -1
- package/plugins/desktop-web/upstream.json +1 -1
- package/plugins/extensions/resolve.ts +4 -0
- package/shared/AGENTS.core.md +3 -0
- package/templates/agent/package.json +7 -3
- package/templates/agent/tsconfig.json +10 -2
- package/templates/blank/package.json +7 -3
- package/templates/blank/src/about.tsx +14 -0
- package/templates/blank/src/routes.ts +2 -0
- package/templates/blank/test/app.test.ts +15 -0
- package/templates/blank/tsconfig.json +3 -1
- package/templates/todo/package.json +7 -3
- package/templates/todo/tsconfig.json +3 -1
- package/templates/todo/upstream.json +1 -1
- package/plugins/acryl-inspect/tests/report.spec.ts +0 -69
- package/plugins/acryl-inspect/tests/service.spec.ts +0 -121
- package/plugins/acryl-node-server/tests/adapter/app.ts +0 -76
- package/plugins/acryl-node-server/tests/adapter/assets/.static/plain.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/favicon.ico +0 -0
- package/plugins/acryl-node-server/tests/adapter/assets/secret.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/acryl.html +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/admin/secret.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/data.json +0 -5
- package/plugins/acryl-node-server/tests/adapter/assets/static/extensionless +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/foo..bar.txt +0 -0
- package/plugins/acryl-node-server/tests/adapter/assets/static/index.html +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static/plain.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-context-path/plain.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.bin.br +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.br +0 -1
- package/plugins/acryl-node-server/tests/adapter/assets/static-with-precompressed/hello.txt.zst +0 -1
- package/plugins/acryl-node-server/tests/adapter/conninfo.test.ts +0 -34
- package/plugins/acryl-node-server/tests/adapter/content-length.test.ts +0 -248
- package/plugins/acryl-node-server/tests/adapter/early-hints.test.ts +0 -457
- package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-cert.pem +0 -23
- package/plugins/acryl-node-server/tests/adapter/fixtures/keys/agent1-key.pem +0 -27
- package/plugins/acryl-node-server/tests/adapter/headers.test.ts +0 -252
- package/plugins/acryl-node-server/tests/adapter/helpers/request.ts +0 -190
- package/plugins/acryl-node-server/tests/adapter/listener.test.ts +0 -727
- package/plugins/acryl-node-server/tests/adapter/request-client-disconnect.test.ts +0 -189
- package/plugins/acryl-node-server/tests/adapter/request.test.ts +0 -1230
- package/plugins/acryl-node-server/tests/adapter/response.test.ts +0 -285
- package/plugins/acryl-node-server/tests/adapter/serve-static.test.ts +0 -849
- package/plugins/acryl-node-server/tests/adapter/server-socket.test.ts +0 -790
- package/plugins/acryl-node-server/tests/adapter/server.test.ts +0 -1312
- package/plugins/acryl-node-server/tests/adapter/setup.ts +0 -16
- package/plugins/acryl-node-server/tests/adapter/url.test.ts +0 -76
- package/plugins/acryl-node-server/tests/adapter/utils/response.test.ts +0 -23
- package/plugins/acryl-node-server/tests/adapter/utils/stream.test.ts +0 -75
- package/plugins/acryl-node-server/tests/adapter/utils.test.ts +0 -235
- package/plugins/acryl-node-server/tests/adapter/websocket.test.ts +0 -260
- package/plugins/acryl-node-server/tests/server.spec.ts +0 -141
- package/plugins/acryl-router/src/core/app.test.ts +0 -3833
- package/plugins/acryl-router/src/core/client/client.test.ts +0 -2313
- package/plugins/acryl-router/src/core/client/types.test.ts +0 -149
- package/plugins/acryl-router/src/core/client/utils.test.ts +0 -405
- package/plugins/acryl-router/src/core/compose.test.ts +0 -751
- package/plugins/acryl-router/src/core/context.test.ts +0 -629
- package/plugins/acryl-router/src/core/helper/accepts/accepts.test.ts +0 -344
- package/plugins/acryl-router/src/core/helper/adapter/index.test.ts +0 -55
- package/plugins/acryl-router/src/core/helper/cookie/index.test.ts +0 -510
- package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +0 -506
- package/plugins/acryl-router/src/core/helper/css/index.test.tsx +0 -678
- package/plugins/acryl-router/src/core/helper/dev/index.test.ts +0 -181
- package/plugins/acryl-router/src/core/helper/factory/index.test.ts +0 -434
- package/plugins/acryl-router/src/core/helper/html/index.test.ts +0 -78
- package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +0 -356
- package/plugins/acryl-router/src/core/helper/route/index.test.ts +0 -259
- package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +0 -557
- package/plugins/acryl-router/src/core/helper/streaming/stream.test.ts +0 -128
- package/plugins/acryl-router/src/core/helper/streaming/text.test.ts +0 -34
- package/plugins/acryl-router/src/core/helper/testing/index.test.ts +0 -42
- package/plugins/acryl-router/src/core/helper/websocket/index.test.ts +0 -124
- package/plugins/acryl-router/src/core/http-exception.test.ts +0 -45
- package/plugins/acryl-router/src/core/jsx/base.test.tsx +0 -97
- package/plugins/acryl-router/src/core/jsx/children.test.ts +0 -52
- package/plugins/acryl-router/src/core/jsx/components.test.tsx +0 -936
- package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +0 -655
- package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +0 -128
- package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +0 -330
- package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +0 -184
- package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +0 -149
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +0 -172
- package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +0 -3681
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +0 -1344
- package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +0 -199
- package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +0 -859
- package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +0 -40
- package/plugins/acryl-router/src/core/jsx/index.test.tsx +0 -1744
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +0 -666
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +0 -85
- package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +0 -1281
- package/plugins/acryl-router/src/core/jsx/utils.test.ts +0 -366
- package/plugins/acryl-router/src/core/middleware/basic-auth/index.test.ts +0 -434
- package/plugins/acryl-router/src/core/middleware/bearer-auth/index.test.ts +0 -1018
- package/plugins/acryl-router/src/core/middleware/body-limit/index.test.ts +0 -273
- package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +0 -1211
- package/plugins/acryl-router/src/core/middleware/combine/index.test.ts +0 -355
- package/plugins/acryl-router/src/core/middleware/compress/index.test.ts +0 -535
- package/plugins/acryl-router/src/core/middleware/context-storage/index.test.ts +0 -43
- package/plugins/acryl-router/src/core/middleware/cors/index.test.ts +0 -532
- package/plugins/acryl-router/src/core/middleware/csrf/index.test.ts +0 -534
- package/plugins/acryl-router/src/core/middleware/etag/digest.test.ts +0 -21
- package/plugins/acryl-router/src/core/middleware/etag/index.test.ts +0 -503
- package/plugins/acryl-router/src/core/middleware/ip-restriction/index.test.ts +0 -217
- package/plugins/acryl-router/src/core/middleware/jwk/index.test.ts +0 -1135
- package/plugins/acryl-router/src/core/middleware/jwk/keys.test.json +0 -48
- package/plugins/acryl-router/src/core/middleware/jwt/index.test.ts +0 -802
- package/plugins/acryl-router/src/core/middleware/language/index.test.ts +0 -552
- package/plugins/acryl-router/src/core/middleware/logger/index.test.ts +0 -204
- package/plugins/acryl-router/src/core/middleware/method-not-allowed/index.test.ts +0 -292
- package/plugins/acryl-router/src/core/middleware/method-override/index.test.ts +0 -221
- package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +0 -334
- package/plugins/acryl-router/src/core/middleware/powered-by/index.test.ts +0 -37
- package/plugins/acryl-router/src/core/middleware/pretty-json/index.test.ts +0 -218
- package/plugins/acryl-router/src/core/middleware/request-id/index.test.ts +0 -168
- package/plugins/acryl-router/src/core/middleware/secure-headers/index.test.ts +0 -671
- package/plugins/acryl-router/src/core/middleware/serve-static/index.test.ts +0 -450
- package/plugins/acryl-router/src/core/middleware/serve-static/path.test.ts +0 -50
- package/plugins/acryl-router/src/core/middleware/timeout/index.test.ts +0 -70
- package/plugins/acryl-router/src/core/middleware/timing/index.test.ts +0 -310
- package/plugins/acryl-router/src/core/middleware/trailing-slash/index.test.ts +0 -381
- package/plugins/acryl-router/src/core/preset/quick.test.ts +0 -42
- package/plugins/acryl-router/src/core/preset/tiny.test.ts +0 -42
- package/plugins/acryl-router/src/core/request.test.ts +0 -751
- package/plugins/acryl-router/src/core/router/common.case.test.ts +0 -1132
- package/plugins/acryl-router/src/core/router/linear-router/router.test.ts +0 -80
- package/plugins/acryl-router/src/core/router/pattern-router/router.test.ts +0 -46
- package/plugins/acryl-router/src/core/router/reg-exp-router/prepared-router.test.ts +0 -154
- package/plugins/acryl-router/src/core/router/reg-exp-router/router.test.ts +0 -265
- package/plugins/acryl-router/src/core/router/smart-router/router.test.ts +0 -13
- package/plugins/acryl-router/src/core/router/trie-router/node.test.ts +0 -896
- package/plugins/acryl-router/src/core/router/trie-router/router.test.ts +0 -8
- package/plugins/acryl-router/src/core/types.test.ts +0 -4134
- package/plugins/acryl-router/src/core/utils/accept.test.ts +0 -299
- package/plugins/acryl-router/src/core/utils/basic-auth.test.ts +0 -56
- package/plugins/acryl-router/src/core/utils/body.test.ts +0 -496
- package/plugins/acryl-router/src/core/utils/buffer.test.ts +0 -147
- package/plugins/acryl-router/src/core/utils/color.test.ts +0 -23
- package/plugins/acryl-router/src/core/utils/concurrent.test.ts +0 -69
- package/plugins/acryl-router/src/core/utils/cookie.test.ts +0 -460
- package/plugins/acryl-router/src/core/utils/crypto.test.ts +0 -43
- package/plugins/acryl-router/src/core/utils/encode.test.ts +0 -61
- package/plugins/acryl-router/src/core/utils/filepath.test.ts +0 -78
- package/plugins/acryl-router/src/core/utils/html.test.ts +0 -16
- package/plugins/acryl-router/src/core/utils/ipaddr.test.ts +0 -149
- package/plugins/acryl-router/src/core/utils/jwt/jwa.test.ts +0 -86
- package/plugins/acryl-router/src/core/utils/jwt/jwt.test.ts +0 -1788
- package/plugins/acryl-router/src/core/utils/mime.test.ts +0 -64
- package/plugins/acryl-router/src/core/utils/stream.test.ts +0 -212
- package/plugins/acryl-router/src/core/utils/types.test.ts +0 -295
- package/plugins/acryl-router/src/core/utils/url.test.ts +0 -384
- package/plugins/acryl-router/src/core/validator/utils.test.ts +0 -82
- package/plugins/acryl-router/src/core/validator/validator.test.ts +0 -1488
- package/plugins/acryl-router/tests/docs-examples.spec.ts +0 -152
- package/plugins/acryl-router/tests/jsx.spec.tsx +0 -63
- package/plugins/acryl-router/tests/live-install.spec.ts +0 -64
- package/plugins/acryl-router/tests/loader.spec.ts +0 -72
- package/plugins/acryl-router/tests/loopback-only.spec.ts +0 -80
- package/plugins/acryl-router/tests/no-dom/consumer.ts +0 -11
- package/plugins/acryl-router/tests/no-dom/tsconfig.json +0 -13
- package/plugins/acryl-router/tests/service.spec.ts +0 -426
- package/plugins/acryl-router/tests/shadow-state.spec.ts +0 -79
- package/plugins/acryl-router-http/tests/adapter.spec.ts +0 -174
- package/plugins/acryl-router-http/tests/stream.spec.ts +0 -133
- package/plugins/acryl-router-http/tests/surfaces.spec.ts +0 -101
- package/plugins/acryl-state/src/react.ts +0 -11
- package/plugins/acryl-state/tests/cell.spec.ts +0 -71
- package/plugins/acryl-state/tests/react.spec.ts +0 -39
- package/plugins/acryl-state/tests/service.spec.ts +0 -132
|
@@ -1,859 +0,0 @@
|
|
|
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
|
-
})
|