acrylblends 0.6.1 → 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.
- package/README.md +11 -6
- package/docs/framework/README.md +3 -2
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/api/presets.md +0 -3
- package/docs/framework/cordis/acryl-in-a-cordis-app.md +9 -0
- package/docs/framework/getting-started/basic.md +5 -4
- package/docs/framework/getting-started/desktop.md +41 -0
- package/docs/framework/guides/jsx-dom.md +0 -3
- package/docs/framework/guides/jsx.md +1 -4
- package/docs/framework/guides/middleware.md +1 -1
- package/docs/framework/helpers/cookie.md +1 -1
- package/docs/framework/helpers/css.md +0 -3
- package/docs/framework/helpers/dev.md +0 -3
- package/docs/framework/helpers/jwt.md +1 -1
- package/docs/framework/helpers/proxy.md +0 -3
- package/docs/framework/middleware/builtin/basic-auth.md +6 -6
- package/docs/framework/middleware/builtin/cache.md +0 -3
- package/docs/framework/middleware/builtin/combine.md +1 -1
- package/docs/framework/middleware/builtin/ip-restriction.md +1 -1
- package/docs/framework/middleware/builtin/logger.md +1 -1
- package/docs/framework/middleware/builtin/mount.md +0 -3
- package/docs/framework/upstream.json +3 -3
- 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-CmAYW1Al.d.ts → app-wFwpg-bt.d.ts} +1 -1
- package/lib/base-BpQy2Rk7.d.ts +783 -0
- package/lib/base-ddiVmiE7.js +721 -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 +43 -0
- package/lib/cache.js +169 -0
- package/lib/client.d.ts +2 -2
- package/lib/combine.d.ts +1 -1
- package/lib/common-D8Z4JRCw.d.ts +46 -0
- package/lib/components-Ce5FRyHU.js +268 -0
- package/lib/components-DYZdxJIJ.js +24 -0
- package/lib/compress.d.ts +1 -1
- package/lib/conninfo.d.ts +2 -2
- package/lib/constants-BR9jM9BC.js +9 -0
- package/lib/{context-Bm7rI-oK.d.ts → context-DVQBjuns.d.ts} +1 -1
- package/lib/context-DxFdY_un.d.ts +250 -0
- package/lib/context-storage.d.ts +1 -1
- package/lib/{cookie-ClMaKDQf.d.ts → cookie-CCelEqky.d.ts} +1 -1
- package/lib/cookie.d.ts +3 -3
- package/lib/cors.d.ts +1 -1
- package/lib/csrf.d.ts +1 -1
- package/lib/css-D2jb7X8b.js +320 -0
- package/lib/css.d.ts +82 -0
- package/lib/css.js +134 -0
- package/lib/dev.d.ts +19 -0
- package/lib/dev.js +50 -0
- package/lib/etag.d.ts +1 -1
- package/lib/factory.d.ts +2 -2
- package/lib/hooks-Bq5q9IMl.js +762 -0
- package/lib/html-SllAe0YT.d.ts +46 -0
- package/lib/html.d.ts +1 -1
- package/lib/index-C850MeBA.d.ts +94 -0
- package/lib/{index-oOClm_Xk.d.ts → index-CUTfU69R.d.ts} +3 -3
- package/lib/{index-B-3F4wt2.d.ts → index-Cqcdluyn.d.ts} +2 -2
- package/lib/{index-UTBmM3GX.d.ts → index-CsvB-F9j.d.ts} +2 -2
- package/lib/index.d.ts +4 -4
- package/lib/ip-restriction.d.ts +2 -2
- package/lib/jsx/dom/client.d.ts +30 -0
- package/lib/jsx/dom/client.js +49 -0
- package/lib/jsx/dom/css.d.ts +84 -0
- package/lib/jsx/dom/css.js +2 -0
- package/lib/jsx/dom/jsx-dev-runtime.d.ts +3 -0
- package/lib/jsx/dom/jsx-dev-runtime.js +2 -0
- package/lib/jsx/dom/jsx-runtime.d.ts +3 -0
- package/lib/jsx/dom/jsx-runtime.js +2 -0
- package/lib/jsx/dom/server.d.ts +105 -0
- package/lib/jsx/dom/server.js +37 -0
- package/lib/jsx/dom.d.ts +5 -0
- package/lib/jsx/dom.js +74 -0
- package/lib/jsx/jsx-dev-runtime.d.ts +6 -0
- package/lib/jsx/jsx-dev-runtime.js +14 -0
- package/lib/jsx/jsx-runtime.d.ts +10 -0
- package/lib/jsx/jsx-runtime.js +30 -0
- package/lib/jsx/streaming.d.ts +2 -0
- package/lib/jsx/streaming.js +2 -0
- package/lib/jsx-dev-runtime-B22O-Npk.d.ts +7 -0
- package/lib/jsx-dev-runtime-kAhR_4C7.js +354 -0
- package/lib/jsx-runtime-BVVgDSdq.d.ts +1 -0
- package/lib/jsx.d.ts +3 -0
- package/lib/jsx.js +47 -0
- package/lib/jwk.d.ts +4 -4
- package/lib/{jwt-Dg493Tai.d.ts → jwt-BARhotDz.d.ts} +1 -1
- package/lib/jwt.d.ts +4 -4
- package/lib/language.d.ts +1 -1
- package/lib/linear-router-C3FPib6h.js +96 -0
- 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 +44 -0
- package/lib/mount.js +67 -0
- package/lib/node-server/conninfo.d.ts +1 -1
- 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/pattern-router-HZxjDGEQ.js +41 -0
- package/lib/powered-by.d.ts +1 -1
- package/lib/pretty-json.d.ts +1 -1
- package/lib/proxy.d.ts +72 -0
- package/lib/proxy.js +121 -0
- package/lib/quick.d.ts +8 -0
- package/lib/quick.js +17 -0
- package/lib/request-id.d.ts +1 -1
- package/lib/request.d.ts +1 -1
- package/lib/route.d.ts +1 -1
- package/lib/router/linear-router.js +1 -95
- package/lib/router/pattern-router.js +1 -40
- package/lib/schema-validator.d.ts +1 -1
- package/lib/secure-headers.d.ts +1 -1
- package/lib/{serve-static-Dw_m6KxR.js → serve-static-Cw23shXv.js} +1 -0
- package/lib/serve-static.d.ts +1 -1
- package/lib/streaming.d.ts +1 -1
- package/lib/testing.d.ts +4 -4
- package/lib/timeout.d.ts +1 -1
- package/lib/timing.d.ts +1 -1
- package/lib/tiny.d.ts +8 -0
- package/lib/tiny.js +15 -0
- package/lib/trailing-slash.d.ts +1 -1
- package/lib/{types-CHesD1KG.d.ts → types-CQiGmsWW.d.ts} +3 -3
- package/lib/{types-CwrQjlaM.d.ts → types-Lqx5BxQF.d.ts} +10 -1
- package/lib/{types-CDO6I3B0.d.ts → types-nEL8VPDC.d.ts} +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/utils/body.d.ts +1 -1
- package/lib/utils/cookie.d.ts +1 -1
- package/lib/utils/crypto.d.ts +1 -1
- package/lib/utils/html.d.ts +1 -45
- package/lib/utils/ipaddr.d.ts +1 -1
- package/lib/utils/jwt.d.ts +1 -1
- package/lib/utils/types.d.ts +2 -2
- package/lib/validator.d.ts +2 -2
- package/lib/ws.d.ts +1 -1
- package/package.json +69 -1
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/upstream.json +1 -1
- package/plugins/acryl-router/src/core/helper/css/common.case.test.tsx +506 -0
- package/plugins/acryl-router/src/core/helper/css/common.ts +337 -0
- package/plugins/acryl-router/src/core/helper/css/index.test.tsx +678 -0
- package/plugins/acryl-router/src/core/helper/css/index.ts +262 -0
- package/plugins/acryl-router/src/core/helper/dev/index.test.ts +181 -0
- package/plugins/acryl-router/src/core/helper/dev/index.ts +79 -0
- package/plugins/acryl-router/src/core/helper/proxy/index.test.ts +356 -0
- package/plugins/acryl-router/src/core/helper/proxy/index.ts +203 -0
- package/plugins/acryl-router/src/core/helper/streaming/sse.test.tsx +557 -0
- package/plugins/acryl-router/src/core/jsx/base.test.tsx +97 -0
- package/plugins/acryl-router/src/core/jsx/base.ts +490 -0
- package/plugins/acryl-router/src/core/jsx/children.test.ts +52 -0
- package/plugins/acryl-router/src/core/jsx/children.ts +20 -0
- package/plugins/acryl-router/src/core/jsx/components.test.tsx +936 -0
- package/plugins/acryl-router/src/core/jsx/components.ts +257 -0
- package/plugins/acryl-router/src/core/jsx/constants.ts +6 -0
- package/plugins/acryl-router/src/core/jsx/context-isolation.test.ts +655 -0
- package/plugins/acryl-router/src/core/jsx/context.ts +266 -0
- package/plugins/acryl-router/src/core/jsx/dom/client.test.tsx +128 -0
- package/plugins/acryl-router/src/core/jsx/dom/client.ts +89 -0
- package/plugins/acryl-router/src/core/jsx/dom/components.test.tsx +330 -0
- package/plugins/acryl-router/src/core/jsx/dom/components.ts +39 -0
- package/plugins/acryl-router/src/core/jsx/dom/context.test.tsx +184 -0
- package/plugins/acryl-router/src/core/jsx/dom/context.ts +52 -0
- package/plugins/acryl-router/src/core/jsx/dom/css.test.tsx +149 -0
- package/plugins/acryl-router/src/core/jsx/dom/css.ts +263 -0
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.test.tsx +172 -0
- package/plugins/acryl-router/src/core/jsx/dom/hooks/index.ts +91 -0
- package/plugins/acryl-router/src/core/jsx/dom/index.test.tsx +3681 -0
- package/plugins/acryl-router/src/core/jsx/dom/index.ts +170 -0
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.test.tsx +1344 -0
- package/plugins/acryl-router/src/core/jsx/dom/intrinsic-element/components.ts +438 -0
- package/plugins/acryl-router/src/core/jsx/dom/jsx-dev-runtime.ts +23 -0
- package/plugins/acryl-router/src/core/jsx/dom/jsx-runtime.ts +8 -0
- package/plugins/acryl-router/src/core/jsx/dom/render.ts +879 -0
- package/plugins/acryl-router/src/core/jsx/dom/server.test.tsx +199 -0
- package/plugins/acryl-router/src/core/jsx/dom/server.ts +76 -0
- package/plugins/acryl-router/src/core/jsx/dom/utils.ts +7 -0
- package/plugins/acryl-router/src/core/jsx/hooks/dom.test.tsx +859 -0
- package/plugins/acryl-router/src/core/jsx/hooks/index.ts +437 -0
- package/plugins/acryl-router/src/core/jsx/hooks/string.test.tsx +40 -0
- package/plugins/acryl-router/src/core/jsx/index.test.tsx +1744 -0
- package/plugins/acryl-router/src/core/jsx/index.ts +115 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/common.ts +23 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.test.tsx +666 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-element/components.ts +210 -0
- package/plugins/acryl-router/src/core/jsx/intrinsic-elements.ts +938 -0
- package/plugins/acryl-router/src/core/jsx/jsx-dev-runtime.ts +26 -0
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.test.tsx +85 -0
- package/plugins/acryl-router/src/core/jsx/jsx-runtime.ts +51 -0
- package/plugins/acryl-router/src/core/jsx/streaming.test.tsx +1281 -0
- package/plugins/acryl-router/src/core/jsx/streaming.ts +231 -0
- package/plugins/acryl-router/src/core/jsx/types.ts +41 -0
- package/plugins/acryl-router/src/core/jsx/utils.test.ts +366 -0
- package/plugins/acryl-router/src/core/jsx/utils.ts +202 -0
- package/plugins/acryl-router/src/core/middleware/cache/index.test.ts +1211 -0
- package/plugins/acryl-router/src/core/middleware/cache/index.ts +323 -0
- package/plugins/acryl-router/src/core/middleware/mount/index.test.ts +334 -0
- package/plugins/acryl-router/src/core/middleware/mount/index.ts +108 -0
- package/plugins/acryl-router/src/core/preset/quick.test.ts +42 -0
- package/plugins/acryl-router/src/core/preset/quick.ts +24 -0
- package/plugins/acryl-router/src/core/preset/tiny.test.ts +42 -0
- package/plugins/acryl-router/src/core/preset/tiny.ts +20 -0
- package/plugins/acryl-router/src/core/utils/types.ts +9 -0
- package/plugins/acryl-router/src/helpers/index.ts +3 -0
- package/plugins/acryl-router/src/middleware/index.ts +2 -0
- package/plugins/acryl-router/src/service.ts +22 -5
- package/plugins/acryl-router/tests/jsx.spec.tsx +63 -0
- package/plugins/acryl-router/upstream.json +3 -3
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/upstream.json +1 -1
- package/plugins/save/plugin.json +1 -1
- package/templates/todo/upstream.json +1 -1
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/** @jsxImportSource ../ */
|
|
2
|
+
import { JSDOM } from 'jsdom'
|
|
3
|
+
import { ErrorBoundary as ErrorBoundaryDom, Suspense as SuspenseDom, render } from './index.ts' // for dom
|
|
4
|
+
import { ErrorBoundary as ErrorBoundaryCommon, Suspense as SuspenseCommon } from '../index.ts' // for common
|
|
5
|
+
// run tests by old style jsx default
|
|
6
|
+
// acryl-router/jsx/jsx-runtime and acryl-router/jsx/dom/jsx-runtime are tested in their respective settings
|
|
7
|
+
import { use, useState } from '../hooks/index.ts'
|
|
8
|
+
|
|
9
|
+
runner('Common', SuspenseCommon, ErrorBoundaryCommon)
|
|
10
|
+
runner('DOM', SuspenseDom, ErrorBoundaryDom)
|
|
11
|
+
|
|
12
|
+
function runner(
|
|
13
|
+
name: string,
|
|
14
|
+
Suspense: typeof SuspenseDom,
|
|
15
|
+
ErrorBoundary: typeof ErrorBoundaryDom
|
|
16
|
+
) {
|
|
17
|
+
describe(name, () => {
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
global.requestAnimationFrame = (cb) => setTimeout(cb)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
describe('Suspense', () => {
|
|
23
|
+
let dom: JSDOM
|
|
24
|
+
let root: HTMLElement
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
|
|
27
|
+
runScripts: 'dangerously',
|
|
28
|
+
})
|
|
29
|
+
global.document = dom.window.document
|
|
30
|
+
global.HTMLElement = dom.window.HTMLElement
|
|
31
|
+
global.Text = dom.window.Text
|
|
32
|
+
root = document.getElementById('root') as HTMLElement
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('has no lazy load content', async () => {
|
|
36
|
+
const App = <Suspense fallback={<div>Loading...</div>}>Hello</Suspense>
|
|
37
|
+
render(App, root)
|
|
38
|
+
expect(root.innerHTML).toBe('Hello')
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('with use()', async () => {
|
|
42
|
+
let resolve: (value: number) => void = () => {}
|
|
43
|
+
const promise = new Promise<number>((_resolve) => (resolve = _resolve))
|
|
44
|
+
const Content = () => {
|
|
45
|
+
const num = use(promise)
|
|
46
|
+
return <p>{num}</p>
|
|
47
|
+
}
|
|
48
|
+
const Component = () => {
|
|
49
|
+
return (
|
|
50
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
51
|
+
<Content />
|
|
52
|
+
</Suspense>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
const App = <Component />
|
|
56
|
+
render(App, root)
|
|
57
|
+
expect(root.innerHTML).toBe('<div>Loading...</div>')
|
|
58
|
+
resolve(1)
|
|
59
|
+
await new Promise((resolve) => setTimeout(resolve))
|
|
60
|
+
expect(root.innerHTML).toBe('<p>1</p>')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('with use() update', async () => {
|
|
64
|
+
const counterMap: Record<number, Promise<number>> = {}
|
|
65
|
+
const getCounter = (count: number) => (counterMap[count] ||= Promise.resolve(count + 1))
|
|
66
|
+
const Content = ({ count }: { count: number }) => {
|
|
67
|
+
const num = use(getCounter(count))
|
|
68
|
+
return (
|
|
69
|
+
<>
|
|
70
|
+
<div>{num}</div>
|
|
71
|
+
</>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
const Component = () => {
|
|
75
|
+
const [count, setCount] = useState(0)
|
|
76
|
+
return (
|
|
77
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
78
|
+
<Content count={count} />
|
|
79
|
+
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
80
|
+
</Suspense>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
const App = <Component />
|
|
84
|
+
render(App, root)
|
|
85
|
+
expect(root.innerHTML).toBe('<div>Loading...</div>')
|
|
86
|
+
await Promise.resolve()
|
|
87
|
+
await Promise.resolve()
|
|
88
|
+
expect(root.innerHTML).toBe('<div>1</div><button>Increment</button>')
|
|
89
|
+
root.querySelector('button')?.click()
|
|
90
|
+
await Promise.resolve()
|
|
91
|
+
expect(root.innerHTML).toBe('<div>Loading...</div>')
|
|
92
|
+
await Promise.resolve()
|
|
93
|
+
await Promise.resolve()
|
|
94
|
+
expect(root.innerHTML).toBe('<div>2</div><button>Increment</button>')
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('with use() nested', async () => {
|
|
98
|
+
let resolve: (value: number) => void = () => {}
|
|
99
|
+
const promise = new Promise<number>((_resolve) => (resolve = _resolve))
|
|
100
|
+
const Content = () => {
|
|
101
|
+
const num = use(promise)
|
|
102
|
+
return <p>{num}</p>
|
|
103
|
+
}
|
|
104
|
+
let resolve2: (value: number) => void = () => {}
|
|
105
|
+
const promise2 = new Promise<number>((_resolve) => (resolve2 = _resolve))
|
|
106
|
+
const Content2 = () => {
|
|
107
|
+
const num = use(promise2)
|
|
108
|
+
return <p>{num}</p>
|
|
109
|
+
}
|
|
110
|
+
const Component = () => {
|
|
111
|
+
return (
|
|
112
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
113
|
+
<Content />
|
|
114
|
+
<Suspense fallback={<div>More...</div>}>
|
|
115
|
+
<Content2 />
|
|
116
|
+
</Suspense>
|
|
117
|
+
</Suspense>
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
const App = <Component />
|
|
121
|
+
render(App, root)
|
|
122
|
+
expect(root.innerHTML).toBe('<div>Loading...</div>')
|
|
123
|
+
resolve(1)
|
|
124
|
+
await new Promise((resolve) => setTimeout(resolve))
|
|
125
|
+
expect(root.innerHTML).toBe('<p>1</p><div>More...</div>')
|
|
126
|
+
resolve2(2)
|
|
127
|
+
await new Promise((resolve) => setTimeout(resolve))
|
|
128
|
+
expect(root.innerHTML).toBe('<p>1</p><p>2</p>')
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('race condition', async () => {
|
|
132
|
+
let resolve: (value: number) => void = () => {}
|
|
133
|
+
const promise = new Promise<number>((_resolve) => (resolve = _resolve))
|
|
134
|
+
const Content = () => {
|
|
135
|
+
const num = use(promise)
|
|
136
|
+
return <p>{num}</p>
|
|
137
|
+
}
|
|
138
|
+
const Component = () => {
|
|
139
|
+
const [show, setShow] = useState(false)
|
|
140
|
+
return (
|
|
141
|
+
<div>
|
|
142
|
+
<button onClick={() => setShow((s) => !s)}>{show ? 'Hide' : 'Show'}</button>
|
|
143
|
+
{show && (
|
|
144
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
145
|
+
<Content />
|
|
146
|
+
</Suspense>
|
|
147
|
+
)}
|
|
148
|
+
</div>
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
const App = <Component />
|
|
152
|
+
render(App, root)
|
|
153
|
+
expect(root.innerHTML).toBe('<div><button>Show</button></div>')
|
|
154
|
+
root.querySelector('button')?.click()
|
|
155
|
+
await Promise.resolve()
|
|
156
|
+
expect(root.innerHTML).toBe('<div><button>Hide</button><div>Loading...</div></div>')
|
|
157
|
+
root.querySelector('button')?.click()
|
|
158
|
+
await Promise.resolve()
|
|
159
|
+
expect(root.innerHTML).toBe('<div><button>Show</button></div>')
|
|
160
|
+
root.querySelector('button')?.click()
|
|
161
|
+
await Promise.resolve()
|
|
162
|
+
expect(root.innerHTML).toBe('<div><button>Hide</button><div>Loading...</div></div>')
|
|
163
|
+
resolve(2)
|
|
164
|
+
await Promise.resolve()
|
|
165
|
+
await Promise.resolve()
|
|
166
|
+
expect(root.innerHTML).toBe('<div><button>Hide</button><p>2</p></div>')
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('Suspense at child', async () => {
|
|
170
|
+
let resolve: (value: number) => void = () => {}
|
|
171
|
+
const promise = new Promise<number>((_resolve) => (resolve = _resolve))
|
|
172
|
+
const Content = () => {
|
|
173
|
+
const num = use(promise)
|
|
174
|
+
return <p>{num}</p>
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const Component = () => {
|
|
178
|
+
return (
|
|
179
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
180
|
+
<Content />
|
|
181
|
+
</Suspense>
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
const App = () => {
|
|
185
|
+
const [show, setShow] = useState(false)
|
|
186
|
+
return (
|
|
187
|
+
<div>
|
|
188
|
+
{show && <Component />}
|
|
189
|
+
<button onClick={() => setShow(true)}>Show</button>
|
|
190
|
+
</div>
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
render(<App />, root)
|
|
194
|
+
expect(root.innerHTML).toBe('<div><button>Show</button></div>')
|
|
195
|
+
root.querySelector('button')?.click()
|
|
196
|
+
await Promise.resolve()
|
|
197
|
+
expect(root.innerHTML).toBe('<div><div>Loading...</div><button>Show</button></div>')
|
|
198
|
+
resolve(2)
|
|
199
|
+
await Promise.resolve()
|
|
200
|
+
await Promise.resolve()
|
|
201
|
+
expect(root.innerHTML).toBe('<div><p>2</p><button>Show</button></div>')
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('Suspense at child counter', async () => {
|
|
205
|
+
const promiseMap: Record<number, Promise<number>> = {}
|
|
206
|
+
const Counter = () => {
|
|
207
|
+
const [count, setCount] = useState(0)
|
|
208
|
+
const promise = (promiseMap[count] ||= Promise.resolve(count))
|
|
209
|
+
const value = use(promise)
|
|
210
|
+
return (
|
|
211
|
+
<>
|
|
212
|
+
<p>{value}</p>
|
|
213
|
+
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
214
|
+
</>
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
const Component = () => {
|
|
218
|
+
return (
|
|
219
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
220
|
+
<Counter />
|
|
221
|
+
</Suspense>
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
const App = () => {
|
|
225
|
+
return (
|
|
226
|
+
<div>
|
|
227
|
+
<Component />
|
|
228
|
+
</div>
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
render(<App />, root)
|
|
232
|
+
expect(root.innerHTML).toBe('<div><div>Loading...</div></div>')
|
|
233
|
+
await Promise.resolve()
|
|
234
|
+
await Promise.resolve()
|
|
235
|
+
expect(root.innerHTML).toBe('<div><p>0</p><button>Increment</button></div>')
|
|
236
|
+
root.querySelector('button')?.click()
|
|
237
|
+
await Promise.resolve()
|
|
238
|
+
expect(root.innerHTML).toBe('<div><div>Loading...</div></div>')
|
|
239
|
+
await Promise.resolve()
|
|
240
|
+
await Promise.resolve()
|
|
241
|
+
expect(root.innerHTML).toBe('<div><p>1</p><button>Increment</button></div>')
|
|
242
|
+
})
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
describe('ErrorBoundary', () => {
|
|
246
|
+
let dom: JSDOM
|
|
247
|
+
let root: HTMLElement
|
|
248
|
+
beforeEach(() => {
|
|
249
|
+
dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
|
|
250
|
+
runScripts: 'dangerously',
|
|
251
|
+
})
|
|
252
|
+
global.document = dom.window.document
|
|
253
|
+
global.HTMLElement = dom.window.HTMLElement
|
|
254
|
+
global.Text = dom.window.Text
|
|
255
|
+
root = document.getElementById('root') as HTMLElement
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
it('has no error', async () => {
|
|
259
|
+
const App = (
|
|
260
|
+
<ErrorBoundary fallback={<div>Error</div>}>
|
|
261
|
+
<div>OK</div>
|
|
262
|
+
</ErrorBoundary>
|
|
263
|
+
)
|
|
264
|
+
render(App, root)
|
|
265
|
+
expect(root.innerHTML).toBe('<div>OK</div>')
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
it('has error', async () => {
|
|
269
|
+
const Component = () => {
|
|
270
|
+
throw new Error('error')
|
|
271
|
+
}
|
|
272
|
+
const App = (
|
|
273
|
+
<ErrorBoundary fallback={<div>Error</div>}>
|
|
274
|
+
<Component />
|
|
275
|
+
</ErrorBoundary>
|
|
276
|
+
)
|
|
277
|
+
render(App, root)
|
|
278
|
+
expect(root.innerHTML).toBe('<div>Error</div>')
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
it('has no error with Suspense', async () => {
|
|
282
|
+
let resolve: (value: number) => void = () => {}
|
|
283
|
+
const promise = new Promise<number>((_resolve) => (resolve = _resolve))
|
|
284
|
+
const Content = () => {
|
|
285
|
+
const num = use(promise)
|
|
286
|
+
return <p>{num}</p>
|
|
287
|
+
}
|
|
288
|
+
const Component = () => {
|
|
289
|
+
return (
|
|
290
|
+
<ErrorBoundary fallback={<div>Error</div>}>
|
|
291
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
292
|
+
<Content />
|
|
293
|
+
</Suspense>
|
|
294
|
+
</ErrorBoundary>
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
const App = <Component />
|
|
298
|
+
render(App, root)
|
|
299
|
+
expect(root.innerHTML).toBe('<div>Loading...</div>')
|
|
300
|
+
resolve(1)
|
|
301
|
+
await new Promise((resolve) => setTimeout(resolve))
|
|
302
|
+
expect(root.innerHTML).toBe('<p>1</p>')
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
it('has error with Suspense', async () => {
|
|
306
|
+
let resolve: (value: number) => void = () => {}
|
|
307
|
+
const promise = new Promise<number>((_resolve) => (resolve = _resolve))
|
|
308
|
+
const Content = () => {
|
|
309
|
+
use(promise)
|
|
310
|
+
throw new Error('error')
|
|
311
|
+
}
|
|
312
|
+
const Component = () => {
|
|
313
|
+
return (
|
|
314
|
+
<ErrorBoundary fallback={<div>Error</div>}>
|
|
315
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
316
|
+
<Content />
|
|
317
|
+
</Suspense>
|
|
318
|
+
</ErrorBoundary>
|
|
319
|
+
)
|
|
320
|
+
}
|
|
321
|
+
const App = <Component />
|
|
322
|
+
render(App, root)
|
|
323
|
+
expect(root.innerHTML).toBe('<div>Loading...</div>')
|
|
324
|
+
resolve(1)
|
|
325
|
+
await new Promise((resolve) => setTimeout(resolve))
|
|
326
|
+
expect(root.innerHTML).toBe('<div>Error</div>')
|
|
327
|
+
})
|
|
328
|
+
})
|
|
329
|
+
})
|
|
330
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Child, FC, PropsWithChildren } from '../index.ts'
|
|
2
|
+
import type { ErrorHandler, FallbackRender } from '../components.ts'
|
|
3
|
+
import { DOM_ERROR_HANDLER } from '../constants.ts'
|
|
4
|
+
import { Fragment } from './jsx-runtime.ts'
|
|
5
|
+
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
7
|
+
export const ErrorBoundary: FC<
|
|
8
|
+
PropsWithChildren<{
|
|
9
|
+
fallback?: Child
|
|
10
|
+
fallbackRender?: FallbackRender
|
|
11
|
+
onError?: ErrorHandler
|
|
12
|
+
}>
|
|
13
|
+
> = (({ children, fallback, fallbackRender, onError }: any) => {
|
|
14
|
+
const res = Fragment({ children })
|
|
15
|
+
;(res as any)[DOM_ERROR_HANDLER] = (err: any) => {
|
|
16
|
+
if (err instanceof Promise) {
|
|
17
|
+
throw err
|
|
18
|
+
}
|
|
19
|
+
onError?.(err)
|
|
20
|
+
return fallbackRender?.(err) || fallback
|
|
21
|
+
}
|
|
22
|
+
return res
|
|
23
|
+
}) as any
|
|
24
|
+
|
|
25
|
+
export const Suspense: FC<PropsWithChildren<{ fallback: any }>> = (({
|
|
26
|
+
children,
|
|
27
|
+
fallback,
|
|
28
|
+
}: any) => {
|
|
29
|
+
const res = Fragment({ children })
|
|
30
|
+
;(res as any)[DOM_ERROR_HANDLER] = (err: any, retry: () => void) => {
|
|
31
|
+
if (!(err instanceof Promise)) {
|
|
32
|
+
throw err
|
|
33
|
+
}
|
|
34
|
+
err.finally(retry)
|
|
35
|
+
return fallback
|
|
36
|
+
}
|
|
37
|
+
return res
|
|
38
|
+
}) as any
|
|
39
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/** @jsxImportSource ../ */
|
|
2
|
+
import { JSDOM } from 'jsdom'
|
|
3
|
+
// run tests by old style jsx default
|
|
4
|
+
// acryl-router/jsx/jsx-runtime and acryl-router/jsx/dom/jsx-runtime are tested in their respective settings
|
|
5
|
+
import { createContext as createContextDom, render, useContext as useContextDom, useState } from './index.ts' // for dom
|
|
6
|
+
import {
|
|
7
|
+
Suspense,
|
|
8
|
+
createContext as createContextCommon,
|
|
9
|
+
use,
|
|
10
|
+
useContext as useContextCommon,
|
|
11
|
+
} from '../index.ts' // for common
|
|
12
|
+
|
|
13
|
+
runner('Common', createContextCommon, useContextCommon)
|
|
14
|
+
runner('DOM', createContextDom, useContextDom)
|
|
15
|
+
|
|
16
|
+
function runner(
|
|
17
|
+
name: string,
|
|
18
|
+
createContext: typeof createContextCommon,
|
|
19
|
+
useContext: typeof useContextCommon
|
|
20
|
+
) {
|
|
21
|
+
describe(name, () => {
|
|
22
|
+
beforeAll(() => {
|
|
23
|
+
global.requestAnimationFrame = (cb) => setTimeout(cb)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('Context', () => {
|
|
27
|
+
let dom: JSDOM
|
|
28
|
+
let root: HTMLElement
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
dom = new JSDOM('<html><body><div id="root"></div></body></html>', {
|
|
31
|
+
runScripts: 'dangerously',
|
|
32
|
+
})
|
|
33
|
+
global.document = dom.window.document
|
|
34
|
+
global.HTMLElement = dom.window.HTMLElement
|
|
35
|
+
global.Text = dom.window.Text
|
|
36
|
+
root = document.getElementById('root') as HTMLElement
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('simple context', async () => {
|
|
40
|
+
const Context = createContext(0)
|
|
41
|
+
const Content = () => {
|
|
42
|
+
const num = useContext(Context)
|
|
43
|
+
return <p>{num}</p>
|
|
44
|
+
}
|
|
45
|
+
const Component = () => {
|
|
46
|
+
return (
|
|
47
|
+
<Context.Provider value={1}>
|
|
48
|
+
<Content />
|
|
49
|
+
</Context.Provider>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
const App = <Component />
|
|
53
|
+
render(App, root)
|
|
54
|
+
expect(root.innerHTML).toBe('<p>1</p>')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('<Context> as a provider ', async () => {
|
|
58
|
+
const Context = createContext(0)
|
|
59
|
+
const Content = () => {
|
|
60
|
+
const num = useContext(Context)
|
|
61
|
+
return <p>{num}</p>
|
|
62
|
+
}
|
|
63
|
+
const Component = () => {
|
|
64
|
+
return (
|
|
65
|
+
<Context value={1}>
|
|
66
|
+
<Content />
|
|
67
|
+
</Context>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
const App = <Component />
|
|
71
|
+
render(App, root)
|
|
72
|
+
expect(root.innerHTML).toBe('<p>1</p>')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('simple context with state', async () => {
|
|
76
|
+
const Context = createContext(0)
|
|
77
|
+
const Content = () => {
|
|
78
|
+
const [count, setCount] = useState(0)
|
|
79
|
+
const num = useContext(Context)
|
|
80
|
+
return (
|
|
81
|
+
<>
|
|
82
|
+
<p>
|
|
83
|
+
{num} - {count}
|
|
84
|
+
</p>
|
|
85
|
+
<button onClick={() => setCount(count + 1)}>+</button>
|
|
86
|
+
</>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
const Component = () => {
|
|
90
|
+
return (
|
|
91
|
+
<Context.Provider value={1}>
|
|
92
|
+
<Content />
|
|
93
|
+
</Context.Provider>
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
const App = <Component />
|
|
97
|
+
render(App, root)
|
|
98
|
+
expect(root.innerHTML).toBe('<p>1 - 0</p><button>+</button>')
|
|
99
|
+
root.querySelector('button')?.click()
|
|
100
|
+
await Promise.resolve()
|
|
101
|
+
expect(root.innerHTML).toBe('<p>1 - 1</p><button>+</button>')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('multiple provider', async () => {
|
|
105
|
+
const Context = createContext(0)
|
|
106
|
+
const Content = () => {
|
|
107
|
+
const num = useContext(Context)
|
|
108
|
+
return <p>{num}</p>
|
|
109
|
+
}
|
|
110
|
+
const Component = () => {
|
|
111
|
+
return (
|
|
112
|
+
<>
|
|
113
|
+
<Context.Provider value={1}>
|
|
114
|
+
<Content />
|
|
115
|
+
</Context.Provider>
|
|
116
|
+
<Context.Provider value={2}>
|
|
117
|
+
<Content />
|
|
118
|
+
</Context.Provider>
|
|
119
|
+
</>
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
const App = <Component />
|
|
123
|
+
render(App, root)
|
|
124
|
+
expect(root.innerHTML).toBe('<p>1</p><p>2</p>')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('nested provider', async () => {
|
|
128
|
+
const Context = createContext(0)
|
|
129
|
+
const Content = () => {
|
|
130
|
+
const num = useContext(Context)
|
|
131
|
+
return <p>{num}</p>
|
|
132
|
+
}
|
|
133
|
+
const Component = () => {
|
|
134
|
+
return (
|
|
135
|
+
<>
|
|
136
|
+
<Context.Provider value={1}>
|
|
137
|
+
<Content />
|
|
138
|
+
<Context.Provider value={3}>
|
|
139
|
+
<Content />
|
|
140
|
+
</Context.Provider>
|
|
141
|
+
<Content />
|
|
142
|
+
</Context.Provider>
|
|
143
|
+
</>
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
const App = <Component />
|
|
147
|
+
render(App, root)
|
|
148
|
+
expect(root.innerHTML).toBe('<p>1</p><p>3</p><p>1</p>')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('inside Suspense', async () => {
|
|
152
|
+
const promise = Promise.resolve(2)
|
|
153
|
+
const AsyncComponent = () => {
|
|
154
|
+
const num = use(promise)
|
|
155
|
+
return <p>{num}</p>
|
|
156
|
+
}
|
|
157
|
+
const Context = createContext(0)
|
|
158
|
+
const Content = () => {
|
|
159
|
+
const num = useContext(Context)
|
|
160
|
+
return <p>{num}</p>
|
|
161
|
+
}
|
|
162
|
+
const Component = () => {
|
|
163
|
+
return (
|
|
164
|
+
<>
|
|
165
|
+
<Context.Provider value={1}>
|
|
166
|
+
<Content />
|
|
167
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
168
|
+
<Context.Provider value={3}>
|
|
169
|
+
<Content />
|
|
170
|
+
<AsyncComponent />
|
|
171
|
+
</Context.Provider>
|
|
172
|
+
</Suspense>
|
|
173
|
+
<Content />
|
|
174
|
+
</Context.Provider>
|
|
175
|
+
</>
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
const App = <Component />
|
|
179
|
+
render(App, root)
|
|
180
|
+
expect(root.innerHTML).toBe('<p>1</p><div>Loading...</div><p>1</p>')
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Child } from '../base.ts'
|
|
2
|
+
import { DOM_ERROR_HANDLER } from '../constants.ts'
|
|
3
|
+
import type { Context } from '../context.ts'
|
|
4
|
+
import { globalContexts } from '../context.ts'
|
|
5
|
+
import { setInternalTagFlag } from './utils.ts'
|
|
6
|
+
|
|
7
|
+
export const createContextProviderFunction =
|
|
8
|
+
<T>(values: T[]): Function =>
|
|
9
|
+
({ value, children }: { value: T; children: Child[] }) => {
|
|
10
|
+
if (!children) {
|
|
11
|
+
return undefined
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
const props: { children: any } = {
|
|
16
|
+
children: [
|
|
17
|
+
{
|
|
18
|
+
tag: setInternalTagFlag(() => {
|
|
19
|
+
values.push(value)
|
|
20
|
+
}),
|
|
21
|
+
props: {},
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
}
|
|
25
|
+
if (Array.isArray(children)) {
|
|
26
|
+
props.children.push(...children.flat())
|
|
27
|
+
} else {
|
|
28
|
+
props.children.push(children)
|
|
29
|
+
}
|
|
30
|
+
props.children.push({
|
|
31
|
+
tag: setInternalTagFlag(() => {
|
|
32
|
+
values.pop()
|
|
33
|
+
}),
|
|
34
|
+
props: {},
|
|
35
|
+
})
|
|
36
|
+
const res = { tag: '', props, type: '' }
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
;(res as any)[DOM_ERROR_HANDLER] = (err: unknown) => {
|
|
39
|
+
values.pop()
|
|
40
|
+
throw err
|
|
41
|
+
}
|
|
42
|
+
return res
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const createContext = <T>(defaultValue: T): Context<T> => {
|
|
46
|
+
const values = [defaultValue]
|
|
47
|
+
const context: Context<T> = createContextProviderFunction(values) as Context<T>
|
|
48
|
+
context.values = values
|
|
49
|
+
context.Provider = context
|
|
50
|
+
globalContexts.push(context as Context<unknown>)
|
|
51
|
+
return context
|
|
52
|
+
}
|