@krate/runtime 0.5.0-rc.1 → 0.5.0-rc.10
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/dist/action-context.js +1 -1
- package/dist/actions.d.ts +7 -0
- package/dist/actions.js +34 -10
- package/dist/after.d.ts +28 -0
- package/dist/after.js +48 -0
- package/dist/assets-static.d.ts +10 -1
- package/dist/assets-static.js +10 -2
- package/dist/base-path.d.ts +7 -0
- package/dist/base-path.js +20 -0
- package/dist/boundary.js +1 -1
- package/dist/builtin-tags.d.ts +12 -0
- package/dist/builtin-tags.js +42 -0
- package/dist/cache-adapter.js +1 -1
- package/dist/cache-scope.d.ts +8 -0
- package/dist/cache-scope.js +14 -1
- package/dist/cloudflare-workers.d.ts +2 -0
- package/dist/cloudflare-workers.js +27 -0
- package/dist/context.d.ts +5 -0
- package/dist/context.js +28 -17
- package/dist/dev-overlay.d.ts +54 -3
- package/dist/dev-overlay.js +470 -77
- package/dist/dom.js +18 -3
- package/dist/effects.js +2 -2
- package/dist/flight-props.d.ts +7 -0
- package/dist/flight-props.js +105 -0
- package/dist/handler-html.js +4 -4
- package/dist/handler-routing.d.ts +2 -23
- package/dist/handler-routing.js +9 -190
- package/dist/handler-segments.d.ts +6 -0
- package/dist/handler-segments.js +18 -4
- package/dist/head-tags.d.ts +9 -0
- package/dist/head-tags.js +93 -6
- package/dist/highlight-bind.js +1 -1
- package/dist/highlight.d.ts +3 -2
- package/dist/highlight.js +17 -12
- package/dist/hints.d.ts +25 -2
- package/dist/hints.js +147 -74
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/island-swap.d.ts +47 -0
- package/dist/island-swap.js +635 -0
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-types.d.ts +99 -3
- package/dist/krate-core.js +1 -1
- package/dist/krate-dev.js +385 -66
- package/dist/krate-f-actions.js +1 -1
- package/dist/krate-f-classes.js +1 -1
- package/dist/krate-f-highlight.js +1 -1
- package/dist/krate-f-list.js +1 -1
- package/dist/krate-f-navigation.js +1 -1
- package/dist/krate-f-router-lite.js +1 -1
- package/dist/krate-f-router.js +1 -1
- package/dist/krate-features.json +454 -0
- package/dist/krate-hydrate-lite.js +5 -5
- package/dist/krate-hydrate.js +5 -5
- package/dist/krate-island-swap.js +1 -0
- package/dist/krate-runtime.js +5 -5
- package/dist/list.d.ts +14 -4
- package/dist/list.js +4 -5
- package/dist/metadata.js +5 -2
- package/dist/navigation-react.d.ts +13 -0
- package/dist/navigation-react.js +86 -0
- package/dist/navigation.d.ts +19 -0
- package/dist/navigation.js +35 -0
- package/dist/node-server.js +3 -3
- package/dist/og.js +1 -1
- package/dist/platform.d.ts +52 -0
- package/dist/platform.js +57 -0
- package/dist/react-compat.d.ts +39 -0
- package/dist/react-compat.js +359 -0
- package/dist/react-server.d.ts +63 -0
- package/dist/react-server.js +125 -0
- package/dist/region-wire.d.ts +10 -0
- package/dist/region-wire.js +154 -0
- package/dist/request-context.js +1 -1
- package/dist/request-extras.d.ts +4 -0
- package/dist/request-extras.js +51 -0
- package/dist/resp.js +1 -1
- package/dist/route-source.d.ts +39 -0
- package/dist/route-source.js +532 -0
- package/dist/router-lite.js +3 -9
- package/dist/router.d.ts +1 -1
- package/dist/router.js +628 -98
- package/dist/server-handler.d.ts +69 -1
- package/dist/server-handler.js +515 -96
- package/dist/server-renderer.js +16 -7
- package/dist/server.d.ts +73 -16
- package/dist/server.js +619 -102
- package/dist/sha256.js +9 -9
- package/dist/signal.js +0 -6
- package/dist/site-config.d.ts +15 -0
- package/dist/site-config.js +28 -0
- package/dist/slot.d.ts +12 -0
- package/dist/slot.js +23 -0
- package/dist/ssr-core.d.ts +2 -0
- package/dist/ssr-core.js +12 -4
- package/dist/testing.d.ts +62 -0
- package/dist/testing.js +90 -0
- package/dist/use-id.js +1 -1
- package/package.json +28 -3
- package/dist/action-context.test.d.ts +0 -1
- package/dist/action-context.test.js +0 -72
- package/dist/actions.test.d.ts +0 -1
- package/dist/actions.test.js +0 -477
- package/dist/cache-adapter.test.d.ts +0 -1
- package/dist/cache-adapter.test.js +0 -244
- package/dist/cache.test.d.ts +0 -1
- package/dist/cache.test.js +0 -233
- package/dist/class-component.test.d.ts +0 -1
- package/dist/class-component.test.js +0 -462
- package/dist/class-helpers.test.d.ts +0 -1
- package/dist/class-helpers.test.js +0 -169
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -57
- package/dist/derived.test.d.ts +0 -1
- package/dist/derived.test.js +0 -140
- package/dist/dev-overlay.test.d.ts +0 -1
- package/dist/dev-overlay.test.js +0 -165
- package/dist/discrete.test.d.ts +0 -1
- package/dist/discrete.test.js +0 -100
- package/dist/dom.test.d.ts +0 -1
- package/dist/dom.test.js +0 -39
- package/dist/effect-errors.test.d.ts +0 -1
- package/dist/effect-errors.test.js +0 -199
- package/dist/effects.test.d.ts +0 -1
- package/dist/effects.test.js +0 -485
- package/dist/events.test.d.ts +0 -1
- package/dist/events.test.js +0 -447
- package/dist/handler-html.test.d.ts +0 -1
- package/dist/handler-html.test.js +0 -256
- package/dist/handler-routing.test.d.ts +0 -1
- package/dist/handler-routing.test.js +0 -99
- package/dist/handler-segments.test.d.ts +0 -1
- package/dist/handler-segments.test.js +0 -217
- package/dist/head-tags.test.d.ts +0 -1
- package/dist/head-tags.test.js +0 -131
- package/dist/highlight-bind.test.d.ts +0 -1
- package/dist/highlight-bind.test.js +0 -124
- package/dist/highlight.test.d.ts +0 -1
- package/dist/highlight.test.js +0 -56
- package/dist/lanes.test.d.ts +0 -1
- package/dist/lanes.test.js +0 -59
- package/dist/list.test.d.ts +0 -1
- package/dist/list.test.js +0 -435
- package/dist/metadata.test.d.ts +0 -1
- package/dist/metadata.test.js +0 -167
- package/dist/navigation.test.d.ts +0 -1
- package/dist/navigation.test.js +0 -130
- package/dist/node-server.test.d.ts +0 -1
- package/dist/node-server.test.js +0 -492
- package/dist/optimistic-lanes.test.d.ts +0 -1
- package/dist/optimistic-lanes.test.js +0 -45
- package/dist/patterns.test.d.ts +0 -1
- package/dist/patterns.test.js +0 -125
- package/dist/portal.test.d.ts +0 -1
- package/dist/portal.test.js +0 -99
- package/dist/react-effects.test.d.ts +0 -1
- package/dist/react-effects.test.js +0 -133
- package/dist/react-suspense.test.d.ts +0 -1
- package/dist/react-suspense.test.js +0 -194
- package/dist/react.test.d.ts +0 -1
- package/dist/react.test.js +0 -60
- package/dist/react19.test.d.ts +0 -1
- package/dist/react19.test.js +0 -176
- package/dist/reactivity-core.test.d.ts +0 -1
- package/dist/reactivity-core.test.js +0 -152
- package/dist/reconcile.test.d.ts +0 -1
- package/dist/reconcile.test.js +0 -110
- package/dist/refs.test.d.ts +0 -1
- package/dist/refs.test.js +0 -257
- package/dist/router-layout.test.d.ts +0 -1
- package/dist/router-layout.test.js +0 -249
- package/dist/router-nav.test.d.ts +0 -1
- package/dist/router-nav.test.js +0 -559
- package/dist/router-slots.test.d.ts +0 -1
- package/dist/router-slots.test.js +0 -144
- package/dist/router-speculation.test.d.ts +0 -1
- package/dist/router-speculation.test.js +0 -25
- package/dist/router-styles.test.d.ts +0 -1
- package/dist/router-styles.test.js +0 -98
- package/dist/router.test.d.ts +0 -1
- package/dist/router.test.js +0 -36
- package/dist/server-async.test.d.ts +0 -1
- package/dist/server-async.test.js +0 -216
- package/dist/server-handler.test.d.ts +0 -1
- package/dist/server-handler.test.js +0 -535
- package/dist/server-highlight.test.d.ts +0 -1
- package/dist/server-highlight.test.js +0 -32
- package/dist/server-renderer.test.d.ts +0 -1
- package/dist/server-renderer.test.js +0 -266
- package/dist/server.test.d.ts +0 -1
- package/dist/server.test.js +0 -88
- package/dist/sha256.test.d.ts +0 -1
- package/dist/sha256.test.js +0 -13
- package/dist/signal-lanes.test.d.ts +0 -1
- package/dist/signal-lanes.test.js +0 -32
- package/dist/signal.test.d.ts +0 -1
- package/dist/signal.test.js +0 -68
- package/dist/slots.test.d.ts +0 -1
- package/dist/slots.test.js +0 -187
- package/dist/snapshots.test.d.ts +0 -1
- package/dist/snapshots.test.js +0 -208
- package/dist/ssr-core-behavior.test.d.ts +0 -1
- package/dist/ssr-core-behavior.test.js +0 -620
- package/dist/ssr-core.test.d.ts +0 -1
- package/dist/ssr-core.test.js +0 -324
- package/dist/static-memory.test.d.ts +0 -1
- package/dist/static-memory.test.js +0 -26
- package/dist/storage.test.d.ts +0 -1
- package/dist/storage.test.js +0 -46
- package/dist/suspense-actions.test.d.ts +0 -1
- package/dist/suspense-actions.test.js +0 -72
- package/dist/suspense.test.d.ts +0 -1
- package/dist/suspense.test.js +0 -556
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
-
import { createSignal, createEffect, disposeAll, krateHookState } from './signal';
|
|
3
|
-
import './effects';
|
|
4
|
-
import { __krateOptimistic, runActionTransition } from './actions';
|
|
5
|
-
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
6
|
-
async function settle() {
|
|
7
|
-
for (let i = 0; i < 6; i++)
|
|
8
|
-
await tick();
|
|
9
|
-
}
|
|
10
|
-
describe('state written at the end of an async action', () => {
|
|
11
|
-
beforeEach(() => disposeAll());
|
|
12
|
-
// The update cascades through several effects (optimistic state, a list
|
|
13
|
-
// binding, row bindings). The action settles, and its optimistic revert
|
|
14
|
-
// reads the committed state, in a microtask that used to run between two
|
|
15
|
-
// passes of that cascade, before the state was committed.
|
|
16
|
-
for (const hops of [0, 1, 2, 3, 4])
|
|
17
|
-
it('survives an unrelated plain signal write in a later microtask (' + hops + ' hops)', async () => {
|
|
18
|
-
const [messages, setMessages] = createSignal([{ text: 'hi' }], krateHookState);
|
|
19
|
-
const [optimistic, setOptimistic] = createSignal(messages());
|
|
20
|
-
const add = __krateOptimistic(() => messages(), setOptimistic, (s, text) => [...s, { text, sending: true }]);
|
|
21
|
-
const [rows, setRows] = createSignal([]);
|
|
22
|
-
createEffect(() => setRows(optimistic().map((m) => m.text + (m.sending ? '*' : ''))));
|
|
23
|
-
const [label, setLabel] = createSignal('');
|
|
24
|
-
createEffect(() => setLabel(rows().join(',')));
|
|
25
|
-
const shown = [];
|
|
26
|
-
createEffect(() => void shown.push(label()));
|
|
27
|
-
await settle();
|
|
28
|
-
void runActionTransition(async () => {
|
|
29
|
-
add('hello');
|
|
30
|
-
await new Promise((r) => setTimeout(r, 5));
|
|
31
|
-
// A startTransition that writes its own pending signal afterwards.
|
|
32
|
-
const s = createSignal(true);
|
|
33
|
-
setMessages((m) => [...m, { text: 'hello' }]);
|
|
34
|
-
queueMicrotask(() => s[1](false));
|
|
35
|
-
// The action resolves a few microtasks later (the form action wrapper).
|
|
36
|
-
for (let i = 0; i < hops; i++)
|
|
37
|
-
await null;
|
|
38
|
-
});
|
|
39
|
-
await settle();
|
|
40
|
-
await new Promise((r) => setTimeout(r, 20));
|
|
41
|
-
expect(messages().map((m) => m.text)).toEqual(['hi', 'hello']);
|
|
42
|
-
expect(optimistic().map((m) => m.text)).toEqual(['hi', 'hello']);
|
|
43
|
-
expect(shown[shown.length - 1]).toBe('hi,hello');
|
|
44
|
-
});
|
|
45
|
-
});
|
package/dist/patterns.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/patterns.test.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { h, krateSpread } from './dom';
|
|
3
|
-
import { createContext, krateContextRows } from './context';
|
|
4
|
-
import { createSignal, krateDerived } from './signal';
|
|
5
|
-
import { krateParam } from './hook-args';
|
|
6
|
-
import { krateCondSwap } from './slots';
|
|
7
|
-
import { krateOn } from './events';
|
|
8
|
-
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
9
|
-
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
10
|
-
describe('SVG elements created by h()', () => {
|
|
11
|
-
it('uses the SVG namespace for SVG-only tags and their children', () => {
|
|
12
|
-
const svg = h('svg', { width: '10' }, h('g', null, h('circle', { r: '2' })));
|
|
13
|
-
expect(svg.namespaceURI).toBe(SVG_NS);
|
|
14
|
-
const circle = svg.querySelector('circle');
|
|
15
|
-
expect(circle.namespaceURI).toBe(SVG_NS);
|
|
16
|
-
expect(circle.parentElement.localName).toBe('g');
|
|
17
|
-
});
|
|
18
|
-
it('re-creates an ambiguous tag appended to an SVG parent in the SVG namespace', () => {
|
|
19
|
-
const svg = h('svg', null, h('a', { href: '#x' }, h('path', { d: 'M0 0' })));
|
|
20
|
-
const a = svg.querySelector('a');
|
|
21
|
-
expect(a.namespaceURI).toBe(SVG_NS);
|
|
22
|
-
expect(a.getAttribute('href')).toBe('#x');
|
|
23
|
-
expect(a.firstElementChild.namespaceURI).toBe(SVG_NS);
|
|
24
|
-
});
|
|
25
|
-
it('keeps HTML inside a foreignObject', () => {
|
|
26
|
-
const fo = h('svg', null, h('foreignObject', null, h('a', { href: '#y' }, 'x')));
|
|
27
|
-
expect(fo.querySelector('a').namespaceURI).toBe('http://www.w3.org/1999/xhtml');
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe('krateCondSwap with an SVG template', () => {
|
|
31
|
-
it('unwraps the <svg> wrapper so the branch keeps the SVG namespace', () => {
|
|
32
|
-
document.body.innerHTML =
|
|
33
|
-
'<svg id="s"><!--k:c--><!--/k:c--></svg><template data-kc="c" data-kb="c" data-kns="svg"><svg><g id="grp"><path d="M0 0"></path></g></svg></template>';
|
|
34
|
-
const start = document.querySelector('svg').firstChild;
|
|
35
|
-
const tpl = document.querySelector('template');
|
|
36
|
-
krateCondSwap(start, true, tpl);
|
|
37
|
-
const g = document.getElementById('grp');
|
|
38
|
-
expect(g.namespaceURI).toBe(SVG_NS);
|
|
39
|
-
expect(g.parentElement.id).toBe('s');
|
|
40
|
-
expect(g.firstElementChild.namespaceURI).toBe(SVG_NS);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
describe('krateSpread', () => {
|
|
44
|
-
it('applies attributes and handlers and removes keys that disappear', async () => {
|
|
45
|
-
const [on, setOn] = createSignal(true);
|
|
46
|
-
const el = document.createElement('button');
|
|
47
|
-
document.body.appendChild(el);
|
|
48
|
-
let clicks = 0;
|
|
49
|
-
krateSpread(el, () => (on() ? { title: 'x', className: 'a', onClick: () => clicks++ } : { className: 'b' }));
|
|
50
|
-
expect(el.getAttribute('title')).toBe('x');
|
|
51
|
-
expect(el.getAttribute('class')).toBe('a');
|
|
52
|
-
el.dispatchEvent(new Event('click', { bubbles: true }));
|
|
53
|
-
expect(clicks).toBe(1);
|
|
54
|
-
setOn(false);
|
|
55
|
-
await tick();
|
|
56
|
-
expect(el.hasAttribute('title')).toBe(false);
|
|
57
|
-
expect(el.getAttribute('class')).toBe('b');
|
|
58
|
-
el.dispatchEvent(new Event('click', { bubbles: true }));
|
|
59
|
-
expect(clicks).toBe(1);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
describe('krateSpread with function values', () => {
|
|
63
|
-
it('writes no attribute for a function and binds on* handlers as events', async () => {
|
|
64
|
-
const [n, setN] = createSignal(1);
|
|
65
|
-
const el = document.createElement('div');
|
|
66
|
-
document.body.appendChild(el);
|
|
67
|
-
let clicks = 0;
|
|
68
|
-
const props = { id: 'd', title: n, 'data-count': () => 1, onClick: () => clicks++, onclick2: () => 0 };
|
|
69
|
-
krateSpread(el, () => props);
|
|
70
|
-
expect(el.id).toBe('d');
|
|
71
|
-
expect(el.hasAttribute('title')).toBe(false);
|
|
72
|
-
expect(el.hasAttribute('data-count')).toBe(false);
|
|
73
|
-
expect(el.hasAttribute('onclick2')).toBe(false);
|
|
74
|
-
expect(el.outerHTML).not.toContain('=>');
|
|
75
|
-
el.dispatchEvent(new Event('click', { bubbles: true }));
|
|
76
|
-
expect(clicks).toBe(1);
|
|
77
|
-
// A string that later becomes a function drops the attribute.
|
|
78
|
-
const [v, setV] = createSignal('t');
|
|
79
|
-
const el2 = document.createElement('div');
|
|
80
|
-
document.body.appendChild(el2);
|
|
81
|
-
krateSpread(el2, () => ({ title: v() }));
|
|
82
|
-
expect(el2.getAttribute('title')).toBe('t');
|
|
83
|
-
setV(() => () => 5);
|
|
84
|
-
await tick();
|
|
85
|
-
expect(el2.hasAttribute('title')).toBe(false);
|
|
86
|
-
setN(2);
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
describe('krateContextRows', () => {
|
|
90
|
-
it('shows live provider values to rows created under it', async () => {
|
|
91
|
-
const Sel = createContext('');
|
|
92
|
-
const [sel, setSel] = createSignal('a');
|
|
93
|
-
const row = krateContextRows([[Sel, () => sel()]], () => Sel.useContextLive());
|
|
94
|
-
const read = row();
|
|
95
|
-
expect(read()).toBe('a');
|
|
96
|
-
setSel('b');
|
|
97
|
-
expect(read()).toBe('b');
|
|
98
|
-
// Outside the rows the context has its default again.
|
|
99
|
-
expect(Sel.useContext()).toBe('');
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
describe('krateParam', () => {
|
|
103
|
-
it('unwraps a derived getter argument and passes other values through', () => {
|
|
104
|
-
const [n, setN] = createSignal(1);
|
|
105
|
-
const arg = krateDerived(() => n() * 2);
|
|
106
|
-
expect(krateParam(arg)).toBe(2);
|
|
107
|
-
setN(2);
|
|
108
|
-
expect(krateParam(arg)).toBe(4);
|
|
109
|
-
expect(krateParam(5)).toBe(5);
|
|
110
|
-
const fn = () => 1;
|
|
111
|
-
expect(krateParam(fn)).toBe(fn);
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
describe('krateOn unaffected by spread removal', () => {
|
|
115
|
-
it('removes a handler with null', () => {
|
|
116
|
-
const el = document.createElement('div');
|
|
117
|
-
document.body.appendChild(el);
|
|
118
|
-
let n = 0;
|
|
119
|
-
krateOn(el, 'onClick', () => n++);
|
|
120
|
-
el.dispatchEvent(new Event('click', { bubbles: true }));
|
|
121
|
-
krateOn(el, 'onClick', null);
|
|
122
|
-
el.dispatchEvent(new Event('click', { bubbles: true }));
|
|
123
|
-
expect(n).toBe(1);
|
|
124
|
-
});
|
|
125
|
-
});
|
package/dist/portal.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/portal.test.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
-
import { krateMountPortals, kratePortalFind, kratePortalReset, kratePortalScan, kratePortalTarget } from './portal';
|
|
3
|
-
import { krateCondSwap } from './slots';
|
|
4
|
-
import { krateOn } from './events';
|
|
5
|
-
import { disposeNode } from './dom';
|
|
6
|
-
import { createPortal } from './react';
|
|
7
|
-
function startMarker(root, id) {
|
|
8
|
-
const w = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
|
|
9
|
-
while (w.nextNode()) {
|
|
10
|
-
if (w.currentNode.nodeValue === 'k:' + id)
|
|
11
|
-
return w.currentNode;
|
|
12
|
-
}
|
|
13
|
-
throw new Error('marker not found: ' + id);
|
|
14
|
-
}
|
|
15
|
-
const bodyChildren = () => Array.from(document.body.children).map((el) => el.id || el.tagName.toLowerCase());
|
|
16
|
-
describe('portals', () => {
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
kratePortalReset();
|
|
19
|
-
document.body.innerHTML = '';
|
|
20
|
-
});
|
|
21
|
-
it('mounts portal content into body and bubbles events through the React tree', () => {
|
|
22
|
-
document.body.innerHTML =
|
|
23
|
-
'<div id="root"><div id="host"><template data-krate-portal="p1"><div id="toast"><button id="btn">ok</button></div></template></div></div>';
|
|
24
|
-
const host = document.getElementById('host');
|
|
25
|
-
const btn = document.querySelector('template').content.querySelector('#btn');
|
|
26
|
-
const seen = [];
|
|
27
|
-
krateOn(btn, 'onClick', () => seen.push('button'));
|
|
28
|
-
krateOn(host, 'onClick', () => seen.push('host'));
|
|
29
|
-
krateMountPortals();
|
|
30
|
-
expect(bodyChildren()).toEqual(['root', 'toast']);
|
|
31
|
-
expect(document.getElementById('btn')).toBe(btn);
|
|
32
|
-
btn.click();
|
|
33
|
-
expect(seen).toEqual(['button', 'host']);
|
|
34
|
-
});
|
|
35
|
-
it('mounts into a selector or registered container', () => {
|
|
36
|
-
document.body.innerHTML =
|
|
37
|
-
'<div id="root"><template data-krate-portal="a" data-krate-portal-target="#modal-root"><p id="a">a</p></template>' +
|
|
38
|
-
'<template data-krate-portal="b"><p id="b">b</p></template></div><div id="modal-root"></div><div id="side"></div>';
|
|
39
|
-
kratePortalTarget('b', () => document.getElementById('side'));
|
|
40
|
-
krateMountPortals();
|
|
41
|
-
expect(document.getElementById('a').parentElement.id).toBe('modal-root');
|
|
42
|
-
expect(document.getElementById('b').parentElement.id).toBe('side');
|
|
43
|
-
});
|
|
44
|
-
it('follows its conditional branch: unmounts when hidden, remounts when shown', () => {
|
|
45
|
-
document.body.innerHTML =
|
|
46
|
-
'<div id="root"><!--k:c--><!--/k:c--></div>' +
|
|
47
|
-
'<template data-kc="c" data-kb="c"><template data-krate-portal="m"><div id="modal">hi</div></template></template>';
|
|
48
|
-
const start = startMarker(document.body, 'c');
|
|
49
|
-
const tpl = document.querySelector('template[data-kc]');
|
|
50
|
-
krateCondSwap(start, false, tpl);
|
|
51
|
-
krateMountPortals();
|
|
52
|
-
expect(document.getElementById('modal')).toBeNull();
|
|
53
|
-
krateCondSwap(start, true, tpl);
|
|
54
|
-
expect(bodyChildren()).toContain('modal');
|
|
55
|
-
const modal = document.getElementById('modal');
|
|
56
|
-
krateCondSwap(start, false, tpl);
|
|
57
|
-
expect(document.getElementById('modal')).toBeNull();
|
|
58
|
-
krateCondSwap(start, true, tpl);
|
|
59
|
-
expect(document.getElementById('modal')).toBe(modal);
|
|
60
|
-
});
|
|
61
|
-
it('drops the content when the anchor is disposed', () => {
|
|
62
|
-
document.body.innerHTML = '<div id="root"><span id="wrap"><template data-krate-portal="d"><i id="x">x</i></template></span></div>';
|
|
63
|
-
krateMountPortals();
|
|
64
|
-
expect(document.getElementById('x')).not.toBeNull();
|
|
65
|
-
disposeNode(document.getElementById('wrap'));
|
|
66
|
-
expect(document.getElementById('x')).toBeNull();
|
|
67
|
-
});
|
|
68
|
-
it('finds bound nodes in portal templates before and after mounting', () => {
|
|
69
|
-
document.body.innerHTML = '<div id="root"><template data-krate-portal="f"><b data-k="k:9">b</b><!--k:8--><!--/k:8--></template></div>';
|
|
70
|
-
const root = document.getElementById('root');
|
|
71
|
-
const before = kratePortalFind('[data-k="k:9"]', [root]);
|
|
72
|
-
expect(before).not.toBeNull();
|
|
73
|
-
const comments = [];
|
|
74
|
-
kratePortalScan([root], (content) => {
|
|
75
|
-
const w = document.createTreeWalker(content, NodeFilter.SHOW_COMMENT);
|
|
76
|
-
while (w.nextNode())
|
|
77
|
-
comments.push(String(w.currentNode.nodeValue));
|
|
78
|
-
});
|
|
79
|
-
expect(comments).toContain('k:8');
|
|
80
|
-
krateMountPortals();
|
|
81
|
-
expect(kratePortalFind('[data-k="k:9"]', [root])).toBe(before);
|
|
82
|
-
});
|
|
83
|
-
it('runtime createPortal returns an anchor and removes content on dispose', () => {
|
|
84
|
-
document.body.innerHTML = '<div id="root"><div id="host"></div></div>';
|
|
85
|
-
const host = document.getElementById('host');
|
|
86
|
-
const content = document.createElement('p');
|
|
87
|
-
content.id = 'rt';
|
|
88
|
-
const seen = [];
|
|
89
|
-
krateOn(host, 'onClick', () => seen.push('host'));
|
|
90
|
-
const anchor = createPortal(content, document.body);
|
|
91
|
-
host.appendChild(anchor);
|
|
92
|
-
expect(anchor.nodeType).toBe(8);
|
|
93
|
-
expect(content.parentNode).toBe(document.body);
|
|
94
|
-
content.click();
|
|
95
|
-
expect(seen).toEqual(['host']);
|
|
96
|
-
disposeNode(anchor);
|
|
97
|
-
expect(document.getElementById('rt')).toBeNull();
|
|
98
|
-
});
|
|
99
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
-
import { useEffect, useLayoutEffect, useInsertionEffect, useState, useReducer, useMemo, useCallback, useImperativeHandle, createElement, } from './react';
|
|
3
|
-
import { disposeAll } from './signal';
|
|
4
|
-
import { flushPassive } from './effects';
|
|
5
|
-
import { disposeNode } from './dom';
|
|
6
|
-
const tick = () => new Promise((r) => setTimeout(r, 0));
|
|
7
|
-
async function settle() {
|
|
8
|
-
for (let i = 0; i < 3; i++)
|
|
9
|
-
await tick();
|
|
10
|
-
}
|
|
11
|
-
describe('React compat hooks (unlowered code paths)', () => {
|
|
12
|
-
beforeEach(() => disposeAll());
|
|
13
|
-
it('runs insertion, layout and passive effects in React order', async () => {
|
|
14
|
-
const log = [];
|
|
15
|
-
const Child = () => {
|
|
16
|
-
useEffect(() => void log.push('child passive'), []);
|
|
17
|
-
useLayoutEffect(() => void log.push('child layout'), []);
|
|
18
|
-
useInsertionEffect(() => void log.push('child insertion'), []);
|
|
19
|
-
return createElement('i', null);
|
|
20
|
-
};
|
|
21
|
-
const Parent = () => {
|
|
22
|
-
useEffect(() => void log.push('parent passive'), []);
|
|
23
|
-
useLayoutEffect(() => void log.push('parent layout'), []);
|
|
24
|
-
return createElement('div', null, createElement(Child, null));
|
|
25
|
-
};
|
|
26
|
-
createElement(Parent, null);
|
|
27
|
-
await settle();
|
|
28
|
-
expect(log).toEqual(['child insertion', 'child layout', 'parent layout', 'child passive', 'parent passive']);
|
|
29
|
-
});
|
|
30
|
-
it('honours dependency arrays whose elements are state getters', async () => {
|
|
31
|
-
const log = [];
|
|
32
|
-
let setCount;
|
|
33
|
-
let other;
|
|
34
|
-
const Comp = () => {
|
|
35
|
-
const [count, set] = useState(0);
|
|
36
|
-
const [, setOther] = useState(0);
|
|
37
|
-
setCount = set;
|
|
38
|
-
other = setOther;
|
|
39
|
-
useEffect(() => void log.push(count()), [count]);
|
|
40
|
-
return createElement('span', null);
|
|
41
|
-
};
|
|
42
|
-
createElement(Comp, null);
|
|
43
|
-
await settle();
|
|
44
|
-
other(1);
|
|
45
|
-
await settle();
|
|
46
|
-
expect(log).toEqual([0]);
|
|
47
|
-
setCount(1);
|
|
48
|
-
await settle();
|
|
49
|
-
expect(log).toEqual([0, 1]);
|
|
50
|
-
});
|
|
51
|
-
it('runs cleanups when the component unmounts', async () => {
|
|
52
|
-
const log = [];
|
|
53
|
-
const Comp = () => {
|
|
54
|
-
useEffect(() => () => void log.push('passive cleanup'), []);
|
|
55
|
-
useLayoutEffect(() => () => void log.push('layout cleanup'), []);
|
|
56
|
-
return createElement('span', null);
|
|
57
|
-
};
|
|
58
|
-
const node = createElement(Comp, null);
|
|
59
|
-
await settle();
|
|
60
|
-
disposeNode(node);
|
|
61
|
-
await settle();
|
|
62
|
-
expect(log).toEqual(['layout cleanup', 'passive cleanup']);
|
|
63
|
-
});
|
|
64
|
-
it('keeps useMemo and useCallback values stable for the component lifetime', () => {
|
|
65
|
-
let calls = 0;
|
|
66
|
-
const fn = () => 1;
|
|
67
|
-
const memo = useMemo(() => ++calls, [1]);
|
|
68
|
-
expect(memo).toBe(1);
|
|
69
|
-
expect(useCallback(fn, [])).toBe(fn);
|
|
70
|
-
});
|
|
71
|
-
it('supports the useReducer lazy initializer and bail-out', async () => {
|
|
72
|
-
const [state, dispatch] = useReducer((s, a) => (a === 0 ? s : s + a), 2, (n) => n * 3);
|
|
73
|
-
expect(state()).toBe(6);
|
|
74
|
-
let runs = 0;
|
|
75
|
-
useEffect(() => void runs++, [state]);
|
|
76
|
-
await settle();
|
|
77
|
-
dispatch(0);
|
|
78
|
-
await settle();
|
|
79
|
-
expect(runs).toBe(1);
|
|
80
|
-
dispatch(1);
|
|
81
|
-
await settle();
|
|
82
|
-
expect(runs).toBe(2);
|
|
83
|
-
});
|
|
84
|
-
it('attaches imperative handles in the layout phase and detaches on unmount', async () => {
|
|
85
|
-
const ref = { current: null };
|
|
86
|
-
const Comp = () => {
|
|
87
|
-
useImperativeHandle(ref, () => ({ focus: () => 'focused' }), []);
|
|
88
|
-
return createElement('input', null);
|
|
89
|
-
};
|
|
90
|
-
const node = createElement(Comp, null);
|
|
91
|
-
expect(ref.current).toBe(null);
|
|
92
|
-
await Promise.resolve();
|
|
93
|
-
expect(ref.current?.focus()).toBe('focused');
|
|
94
|
-
disposeNode(node);
|
|
95
|
-
expect(ref.current).toBe(null);
|
|
96
|
-
});
|
|
97
|
-
it('a passive cleanup that throws does not drop the effects queued behind it', async () => {
|
|
98
|
-
const log = [];
|
|
99
|
-
let set;
|
|
100
|
-
const A = () => {
|
|
101
|
-
const [n, s] = useState(0);
|
|
102
|
-
set = s;
|
|
103
|
-
useEffect(() => {
|
|
104
|
-
log.push('A setup ' + n());
|
|
105
|
-
return () => {
|
|
106
|
-
log.push('A cleanup');
|
|
107
|
-
throw new Error('cleanup boom');
|
|
108
|
-
};
|
|
109
|
-
}, [n]);
|
|
110
|
-
return createElement('i', null);
|
|
111
|
-
};
|
|
112
|
-
const B = () => {
|
|
113
|
-
const [m] = useState(0);
|
|
114
|
-
useEffect(() => {
|
|
115
|
-
log.push('B setup');
|
|
116
|
-
return () => void log.push('B cleanup');
|
|
117
|
-
}, [m]);
|
|
118
|
-
return createElement('b', null);
|
|
119
|
-
};
|
|
120
|
-
createElement('div', null, createElement(A, null), createElement(B, null));
|
|
121
|
-
await settle();
|
|
122
|
-
set(1);
|
|
123
|
-
// The passive phase reports the error after running everything queued.
|
|
124
|
-
let failure;
|
|
125
|
-
const onError = (e) => (failure = e);
|
|
126
|
-
process.once('uncaughtException', onError);
|
|
127
|
-
await settle();
|
|
128
|
-
process.off('uncaughtException', onError);
|
|
129
|
-
expect(String(failure)).toContain('cleanup boom');
|
|
130
|
-
expect(log).toEqual(['A setup 0', 'B setup', 'A cleanup', 'A setup 1']);
|
|
131
|
-
flushPassive();
|
|
132
|
-
});
|
|
133
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { Profiler, StrictMode, Suspense, createElement, jsx, lazy, startTransition, use, useDeferredValue, useState, useSyncExternalStore, useTransition, createContext, } from './react.js';
|
|
3
|
-
import { setFallbackThrottle, setSuspenseErrorHandler } from './suspense.js';
|
|
4
|
-
async function flush(n = 20) {
|
|
5
|
-
for (let i = 0; i < n; i++)
|
|
6
|
-
await Promise.resolve();
|
|
7
|
-
}
|
|
8
|
-
function deferred() {
|
|
9
|
-
let resolve;
|
|
10
|
-
let reject;
|
|
11
|
-
const promise = new Promise((res, rej) => {
|
|
12
|
-
resolve = res;
|
|
13
|
-
reject = rej;
|
|
14
|
-
});
|
|
15
|
-
return { promise, resolve, reject };
|
|
16
|
-
}
|
|
17
|
-
function mountInto(node) {
|
|
18
|
-
const root = document.createElement('div');
|
|
19
|
-
root.appendChild(node);
|
|
20
|
-
document.body.innerHTML = '';
|
|
21
|
-
document.body.appendChild(root);
|
|
22
|
-
return root;
|
|
23
|
-
}
|
|
24
|
-
beforeEach(() => setFallbackThrottle(0));
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
setSuspenseErrorHandler(null);
|
|
27
|
-
vi.useRealTimers();
|
|
28
|
-
});
|
|
29
|
-
describe('compat Suspense and use()', () => {
|
|
30
|
-
it('shows the fallback until use(promise) resolves, then renders the value', async () => {
|
|
31
|
-
const d = deferred();
|
|
32
|
-
const Name = () => createElement('b', null, use(d.promise));
|
|
33
|
-
const root = mountInto(createElement(Suspense, { fallback: createElement('i', null, 'loading') }, createElement(Name, null)));
|
|
34
|
-
expect(root.textContent).toBe('loading');
|
|
35
|
-
d.resolve('Ada');
|
|
36
|
-
await flush();
|
|
37
|
-
expect(root.textContent).toBe('Ada');
|
|
38
|
-
expect(root.querySelector('b')?.textContent).toBe('Ada');
|
|
39
|
-
});
|
|
40
|
-
it('reveals siblings together', async () => {
|
|
41
|
-
const a = deferred();
|
|
42
|
-
const b = deferred();
|
|
43
|
-
const A = () => createElement('span', null, use(a.promise));
|
|
44
|
-
const B = () => createElement('span', null, use(b.promise));
|
|
45
|
-
const root = mountInto(createElement(Suspense, { fallback: 'wait' }, createElement(A, null), createElement(B, null)));
|
|
46
|
-
a.resolve('a');
|
|
47
|
-
await flush();
|
|
48
|
-
expect(root.textContent).toBe('wait');
|
|
49
|
-
b.resolve('b');
|
|
50
|
-
await flush();
|
|
51
|
-
expect(root.textContent).toBe('ab');
|
|
52
|
-
});
|
|
53
|
-
it('suspends the innermost boundary only', async () => {
|
|
54
|
-
const d = deferred();
|
|
55
|
-
const Slow = () => createElement('span', null, use(d.promise));
|
|
56
|
-
const root = mountInto(createElement(Suspense, { fallback: 'outer' }, createElement('p', null, 'shell'), createElement(Suspense, { fallback: 'inner' }, createElement(Slow, null))));
|
|
57
|
-
expect(root.textContent).toBe('shellinner');
|
|
58
|
-
d.resolve('done');
|
|
59
|
-
await flush();
|
|
60
|
-
expect(root.textContent).toBe('shelldone');
|
|
61
|
-
});
|
|
62
|
-
it('does not re-suspend on a resolved promise and allows conditional use()', async () => {
|
|
63
|
-
const p = Promise.resolve('v');
|
|
64
|
-
await flush();
|
|
65
|
-
let renders = 0;
|
|
66
|
-
const C = (props) => {
|
|
67
|
-
renders++;
|
|
68
|
-
return createElement('span', null, props.on ? use(p) : 'off');
|
|
69
|
-
};
|
|
70
|
-
// First read tracks the promise; it is already resolved by the next read.
|
|
71
|
-
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(C, { on: true })));
|
|
72
|
-
await flush();
|
|
73
|
-
const root = mountInto(createElement(Suspense, { fallback: 'x' }, createElement(C, { on: true })));
|
|
74
|
-
expect(root.textContent).toBe('v');
|
|
75
|
-
const off = mountInto(createElement(C, { on: false }));
|
|
76
|
-
expect(off.textContent).toBe('off');
|
|
77
|
-
expect(renders).toBeGreaterThan(0);
|
|
78
|
-
});
|
|
79
|
-
it('reads a context with use()', () => {
|
|
80
|
-
const Ctx = createContext('ctx');
|
|
81
|
-
const C = () => createElement('span', null, use(Ctx));
|
|
82
|
-
expect(mountInto(createElement(C, null)).textContent).toBe('ctx');
|
|
83
|
-
});
|
|
84
|
-
it('routes a rejected promise to the error handler', async () => {
|
|
85
|
-
const errors = [];
|
|
86
|
-
setSuspenseErrorHandler((e) => {
|
|
87
|
-
errors.push(e);
|
|
88
|
-
return true;
|
|
89
|
-
});
|
|
90
|
-
const d = deferred();
|
|
91
|
-
const C = () => createElement('span', null, use(d.promise));
|
|
92
|
-
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(C, null)));
|
|
93
|
-
d.reject(new Error('bad'));
|
|
94
|
-
await flush();
|
|
95
|
-
expect(errors[0].message).toBe('bad');
|
|
96
|
-
});
|
|
97
|
-
it('jsx() renders through the same suspending path', async () => {
|
|
98
|
-
const d = deferred();
|
|
99
|
-
const C = () => jsx('span', { children: use(d.promise) });
|
|
100
|
-
const root = mountInto(jsx(Suspense, { fallback: 'f', children: jsx(C, {}) }));
|
|
101
|
-
expect(root.textContent).toBe('f');
|
|
102
|
-
d.resolve('ok');
|
|
103
|
-
await flush();
|
|
104
|
-
expect(root.textContent).toBe('ok');
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
describe('compat lazy()', () => {
|
|
108
|
-
it('starts loading on first render and suspends until the module loads', async () => {
|
|
109
|
-
const d = deferred();
|
|
110
|
-
let calls = 0;
|
|
111
|
-
const Lazy = lazy(() => {
|
|
112
|
-
calls++;
|
|
113
|
-
return d.promise;
|
|
114
|
-
});
|
|
115
|
-
expect(calls).toBe(0);
|
|
116
|
-
const root = mountInto(createElement(Suspense, { fallback: 'loading' }, createElement(Lazy, { name: 'x' })));
|
|
117
|
-
expect(calls).toBe(1);
|
|
118
|
-
expect(root.textContent).toBe('loading');
|
|
119
|
-
d.resolve({ default: (p) => createElement('em', null, 'hi ' + p.name) });
|
|
120
|
-
await flush();
|
|
121
|
-
expect(root.textContent).toBe('hi x');
|
|
122
|
-
expect(calls).toBe(1);
|
|
123
|
-
});
|
|
124
|
-
it('reports a module without a component default export', async () => {
|
|
125
|
-
const errors = [];
|
|
126
|
-
setSuspenseErrorHandler((e) => {
|
|
127
|
-
errors.push(e);
|
|
128
|
-
return true;
|
|
129
|
-
});
|
|
130
|
-
const Lazy = lazy(() => Promise.resolve({ default: 42 }));
|
|
131
|
-
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(Lazy, null)));
|
|
132
|
-
await flush();
|
|
133
|
-
expect(String(errors[0].message)).toContain('default export');
|
|
134
|
-
});
|
|
135
|
-
it('reports a failed import', async () => {
|
|
136
|
-
const errors = [];
|
|
137
|
-
setSuspenseErrorHandler((e) => {
|
|
138
|
-
errors.push(e);
|
|
139
|
-
return true;
|
|
140
|
-
});
|
|
141
|
-
const Lazy = lazy(() => Promise.reject(new Error('chunk failed')));
|
|
142
|
-
mountInto(createElement(Suspense, { fallback: 'x' }, createElement(Lazy, null)));
|
|
143
|
-
await flush();
|
|
144
|
-
expect(errors[0].message).toBe('chunk failed');
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
describe('compat transitions and deferred values', () => {
|
|
148
|
-
it('useTransition stays pending until the async action settles', async () => {
|
|
149
|
-
vi.useFakeTimers();
|
|
150
|
-
const [isPending, start] = useTransition();
|
|
151
|
-
start(async () => {
|
|
152
|
-
await new Promise((r) => setTimeout(r, 100));
|
|
153
|
-
});
|
|
154
|
-
expect(isPending()).toBe(true);
|
|
155
|
-
await vi.advanceTimersByTimeAsync(100);
|
|
156
|
-
expect(isPending()).toBe(false);
|
|
157
|
-
});
|
|
158
|
-
it('startTransition runs the action', () => {
|
|
159
|
-
let ran = false;
|
|
160
|
-
startTransition(() => {
|
|
161
|
-
ran = true;
|
|
162
|
-
});
|
|
163
|
-
expect(ran).toBe(true);
|
|
164
|
-
});
|
|
165
|
-
it('useDeferredValue defers a getter and passes plain values through', async () => {
|
|
166
|
-
vi.useFakeTimers();
|
|
167
|
-
const [q, setQ] = useState('a');
|
|
168
|
-
const d = useDeferredValue(q);
|
|
169
|
-
setQ('b');
|
|
170
|
-
await flush();
|
|
171
|
-
expect(d()).toBe('a');
|
|
172
|
-
await vi.advanceTimersByTimeAsync(1);
|
|
173
|
-
expect(d()).toBe('b');
|
|
174
|
-
expect(useDeferredValue(5)).toBe(5);
|
|
175
|
-
});
|
|
176
|
-
it('useSyncExternalStore reads and follows the store', () => {
|
|
177
|
-
let v = 1;
|
|
178
|
-
let notify = () => { };
|
|
179
|
-
const get = useSyncExternalStore((fn) => {
|
|
180
|
-
notify = fn;
|
|
181
|
-
return () => { };
|
|
182
|
-
}, () => v);
|
|
183
|
-
expect(get()).toBe(1);
|
|
184
|
-
v = 2;
|
|
185
|
-
notify();
|
|
186
|
-
expect(get()).toBe(2);
|
|
187
|
-
});
|
|
188
|
-
it('StrictMode and Profiler render their children', () => {
|
|
189
|
-
expect(mountInto(createElement(StrictMode, null, 'a')).textContent).toBe('a');
|
|
190
|
-
const onRender = vi.fn();
|
|
191
|
-
expect(mountInto(createElement(Profiler, { id: 'p', onRender }, 'b')).textContent).toBe('b');
|
|
192
|
-
expect(onRender).not.toHaveBeenCalled();
|
|
193
|
-
});
|
|
194
|
-
});
|
package/dist/react.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|