what-core 0.14.0 → 0.15.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/LICENSE +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
package/src/testing.js
CHANGED
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
// Helpers for testing components, similar to @testing-library/react
|
|
3
3
|
// Works with Node.js test runner or any test framework
|
|
4
4
|
|
|
5
|
-
import { signal, effect, flushSync, createRoot, __DEV__, __devtools, __setDevToolsHooks } from './reactive.js';
|
|
5
|
+
import { signal, effect, flushSync, createRoot, __DEV__, __devtools, __setDevToolsHooks, __depsOf, __subsOf, __ownerPath } from './reactive.js';
|
|
6
6
|
import { mount } from './dom.js';
|
|
7
7
|
import { h } from './h.js';
|
|
8
|
+
import { capture } from './diagnostics.js';
|
|
9
|
+
import { settled } from './async.js';
|
|
10
|
+
|
|
11
|
+
// Resolves once nothing is queued, fetching or in a transition (async.js): act,
|
|
12
|
+
// then `await settled()`, then read the DOM.
|
|
13
|
+
export { settled };
|
|
8
14
|
|
|
9
15
|
// Minimal DOM implementation for Node.js
|
|
10
16
|
/** @type {HTMLDivElement | null} */
|
|
@@ -25,12 +31,21 @@ const mountedTrees = new Set();
|
|
|
25
31
|
|
|
26
32
|
// Idempotent and re-entrant: renderTest's own unmount() calls cleanup() at the
|
|
27
33
|
// end, so a disposer that is still in the set when cleanup() runs must not fire
|
|
28
|
-
// twice.
|
|
29
|
-
|
|
34
|
+
// twice. Effect creations are watched from before the mount until after the
|
|
35
|
+
// disposal, so a capture can tell which component each effect belongs to.
|
|
36
|
+
function trackMount(mountTree) {
|
|
37
|
+
const stopWatching = watchCreations();
|
|
38
|
+
let dispose;
|
|
39
|
+
try {
|
|
40
|
+
dispose = mountTree();
|
|
41
|
+
} catch (err) {
|
|
42
|
+
stopWatching();
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
30
45
|
const unmount = () => {
|
|
31
46
|
if (!mountedTrees.has(unmount)) return;
|
|
32
47
|
mountedTrees.delete(unmount);
|
|
33
|
-
dispose();
|
|
48
|
+
try { dispose(); } finally { stopWatching(); }
|
|
34
49
|
};
|
|
35
50
|
mountedTrees.add(unmount);
|
|
36
51
|
return unmount;
|
|
@@ -71,7 +86,7 @@ export function render(vnode, options = {}) {
|
|
|
71
86
|
throw new Error('No DOM container available. Are you running in Node.js without jsdom?');
|
|
72
87
|
}
|
|
73
88
|
|
|
74
|
-
const unmount = trackMount(mount(vnode, target));
|
|
89
|
+
const unmount = trackMount(() => mount(vnode, target));
|
|
75
90
|
|
|
76
91
|
return {
|
|
77
92
|
container: target,
|
|
@@ -111,7 +126,7 @@ export function renderTest(Component, props) {
|
|
|
111
126
|
createRoot((dispose) => {
|
|
112
127
|
rootDispose = dispose;
|
|
113
128
|
const vnode = h(Component, props || {});
|
|
114
|
-
unmountFn = trackMount(mount(vnode, target));
|
|
129
|
+
unmountFn = trackMount(() => mount(vnode, target));
|
|
115
130
|
});
|
|
116
131
|
|
|
117
132
|
return {
|
|
@@ -152,9 +167,120 @@ export function flushEffects() {
|
|
|
152
167
|
flushSync();
|
|
153
168
|
}
|
|
154
169
|
|
|
155
|
-
// ---
|
|
156
|
-
//
|
|
157
|
-
//
|
|
170
|
+
// --- Development hook listeners ---
|
|
171
|
+
//
|
|
172
|
+
// trackSignals(), captureDiagnostics() and the effect owner record below listen
|
|
173
|
+
// to the engine's development hooks, and several can be open at once
|
|
174
|
+
// (overlapping async captures, a capture while a tree is mounted). So
|
|
175
|
+
// each adds its own listener to one shared wrapper instead of swapping the
|
|
176
|
+
// global hooks: a capture that ends removes only its own listener, and the
|
|
177
|
+
// wrapper forwards to whatever hooks were installed underneath it, so installed
|
|
178
|
+
// devtools keep working. The wrapper comes out when the last listener leaves.
|
|
179
|
+
const hookListeners = {
|
|
180
|
+
onSignalUpdate: new Set(),
|
|
181
|
+
onEffectRun: new Set(),
|
|
182
|
+
onEffectCreate: new Set(),
|
|
183
|
+
onEffectDispose: new Set(),
|
|
184
|
+
};
|
|
185
|
+
/** @type {WhatDevToolsHooks | null} */
|
|
186
|
+
let hookBase = null;
|
|
187
|
+
/** @type {WhatDevToolsHooks | null} */
|
|
188
|
+
let hookWrapper = null;
|
|
189
|
+
|
|
190
|
+
// Installs the wrapper for the first listener, and again when other hooks were
|
|
191
|
+
// installed over it (devtools, a test): the listeners have heard nothing since,
|
|
192
|
+
// so it wraps those instead (the creations made meanwhile went unseen). Only
|
|
193
|
+
// development reaches it; the guard lets the per-module production dist drop
|
|
194
|
+
// its body.
|
|
195
|
+
function ensureWrapper() {
|
|
196
|
+
if (!__DEV__) return;
|
|
197
|
+
if (hookWrapper !== null && __devtools === hookWrapper) return;
|
|
198
|
+
const base = __devtools;
|
|
199
|
+
/** @type {WhatDevToolsHooks} */
|
|
200
|
+
const wrapper = { ...(base || {}) };
|
|
201
|
+
for (const hook of Object.keys(hookListeners)) {
|
|
202
|
+
wrapper[hook] = (...args) => {
|
|
203
|
+
for (const listener of hookListeners[hook]) listener(...args);
|
|
204
|
+
base?.[hook]?.(...args);
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
hookBase = base;
|
|
208
|
+
hookWrapper = wrapper;
|
|
209
|
+
__setDevToolsHooks(wrapper);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function listenToHook(name, fn) {
|
|
213
|
+
// Only its development-only callers reach this; production builds drop it.
|
|
214
|
+
if (!__DEV__) return () => {};
|
|
215
|
+
hookListeners[name].add(fn);
|
|
216
|
+
ensureWrapper();
|
|
217
|
+
return () => {
|
|
218
|
+
hookListeners[name].delete(fn);
|
|
219
|
+
if (Object.values(hookListeners).some((set) => set.size > 0)) return;
|
|
220
|
+
if (__devtools === hookWrapper) __setDevToolsHooks(hookBase);
|
|
221
|
+
hookWrapper = null;
|
|
222
|
+
hookBase = null;
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// --- Effect owners ---
|
|
227
|
+
//
|
|
228
|
+
// Every effect and computed sits in the owner tree: under the component whose
|
|
229
|
+
// body, output or region created it, under the effect whose run created it,
|
|
230
|
+
// or under a root. Its owner is the components on its __ownerPath(), root
|
|
231
|
+
// first, read from the tree when the capture ends (an owner stays readable
|
|
232
|
+
// after it is disposed), so it is right however the node was created: in a
|
|
233
|
+
// region or a row long after the body ran, in a useEffect or onMount callback,
|
|
234
|
+
// or by an effect re-run. It is null when no component holds the node: module
|
|
235
|
+
// scope, or a createRoot outside any component.
|
|
236
|
+
//
|
|
237
|
+
// Which effects exist is another matter: the engine keeps no list. The helpers
|
|
238
|
+
// note each creation while they watch (while a tree render() or renderTest()
|
|
239
|
+
// mounted is alive, and while a capture is open), so a capture can list the
|
|
240
|
+
// effects that did not run. An effect created while nothing watched appears
|
|
241
|
+
// only if it runs.
|
|
242
|
+
|
|
243
|
+
/** The effects and computeds created while something watched. @type {Set<any>} */
|
|
244
|
+
const created = new Set();
|
|
245
|
+
let watchers = 0;
|
|
246
|
+
let unwatch = () => {};
|
|
247
|
+
|
|
248
|
+
function recordCreation(node) {
|
|
249
|
+
created.add(node);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function forgetCreation(node) {
|
|
253
|
+
created.delete(node);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// One watch per mounted tree and per open capture. The record is dropped with
|
|
257
|
+
// the last watch: disposals after that go unseen, so it would turn stale.
|
|
258
|
+
function watchCreations() {
|
|
259
|
+
if (!__DEV__) return () => {};
|
|
260
|
+
if (watchers++ === 0) {
|
|
261
|
+
const stopCreate = listenToHook('onEffectCreate', recordCreation);
|
|
262
|
+
const stopDispose = listenToHook('onEffectDispose', forgetCreation);
|
|
263
|
+
unwatch = () => {
|
|
264
|
+
stopCreate();
|
|
265
|
+
stopDispose();
|
|
266
|
+
created.clear();
|
|
267
|
+
};
|
|
268
|
+
} else {
|
|
269
|
+
ensureWrapper();
|
|
270
|
+
}
|
|
271
|
+
let watching = true;
|
|
272
|
+
return () => {
|
|
273
|
+
if (!watching) return;
|
|
274
|
+
watching = false;
|
|
275
|
+
if (--watchers === 0) unwatch();
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// The owner path diagnostics print: component names, root first, or null.
|
|
280
|
+
function componentPath(node) {
|
|
281
|
+
const path = __ownerPath(node).filter((scope) => scope[0] === '<');
|
|
282
|
+
return path.length ? path : null;
|
|
283
|
+
}
|
|
158
284
|
|
|
159
285
|
// --- trackSignals ---
|
|
160
286
|
//
|
|
@@ -175,7 +301,7 @@ export function trackSignals(fn) {
|
|
|
175
301
|
if (!__DEV__) {
|
|
176
302
|
throw new Error(
|
|
177
303
|
'[what] trackSignals() requires a development build. Signal debug names ' +
|
|
178
|
-
'and
|
|
304
|
+
'and the dependency graph API are stripped in production, so there is ' +
|
|
179
305
|
'nothing to report. Run your tests with NODE_ENV !== "production".'
|
|
180
306
|
);
|
|
181
307
|
}
|
|
@@ -184,32 +310,15 @@ export function trackSignals(fn) {
|
|
|
184
310
|
const written = [];
|
|
185
311
|
const addOnce = (list, name) => { if (!list.includes(name)) list.push(name); };
|
|
186
312
|
|
|
187
|
-
//
|
|
188
|
-
//
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
//
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
...(previousHooks || {}),
|
|
197
|
-
onSignalUpdate(sig) {
|
|
198
|
-
addOnce(written, sig?._debugName || UNNAMED);
|
|
199
|
-
previousHooks?.onSignalUpdate?.(sig);
|
|
200
|
-
},
|
|
201
|
-
};
|
|
202
|
-
// A chained hook must not claim to be the pre-install buffer, or the real
|
|
203
|
-
// devtools would later try to drain it a second time.
|
|
204
|
-
delete trackingHooks.__isPreinstallBuffer;
|
|
205
|
-
|
|
206
|
-
// --- Reads ---
|
|
207
|
-
//
|
|
208
|
-
// Reading a signal inside an effect adds that effect to the signal's
|
|
209
|
-
// subscriber Set and pushes the Set onto effect.deps. effect() returns a
|
|
210
|
-
// dispose function rather than the effect, so the effect is reached through
|
|
211
|
-
// a probe signal: after the run, the probe's subscriber Set holds exactly
|
|
212
|
-
// the effect that read it.
|
|
313
|
+
// Writes: every signal write calls the onSignalUpdate hook in development,
|
|
314
|
+
// so this sees writes to signals that existed long before this call.
|
|
315
|
+
const stopWrites = listenToHook('onSignalUpdate', (sig) => addOnce(written, sig?._debugName || UNNAMED));
|
|
316
|
+
|
|
317
|
+
// Reads: fn runs inside an effect, reached through a probe signal the effect
|
|
318
|
+
// also reads (effect() returns a disposer, not the node): after the run the
|
|
319
|
+
// probe's only subscriber is that effect, and __depsOf lists what it read.
|
|
320
|
+
// A dependency is a signal (its function) or a computed (its node), whose
|
|
321
|
+
// own dependencies are the sources to follow.
|
|
213
322
|
const probe = signal(0, '__trackSignals_probe__');
|
|
214
323
|
// Held in a box rather than a `let`: the assignment happens inside
|
|
215
324
|
// createRoot's callback, and the finally below has to run it whether the
|
|
@@ -218,23 +327,18 @@ export function trackSignals(fn) {
|
|
|
218
327
|
const root = { current: null };
|
|
219
328
|
let thrown = null;
|
|
220
329
|
|
|
221
|
-
const collectReads = (
|
|
222
|
-
for (const
|
|
223
|
-
if (seen.has(
|
|
224
|
-
seen.add(
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
330
|
+
const collectReads = (node, seen) => {
|
|
331
|
+
for (const dep of __depsOf(node)) {
|
|
332
|
+
if (seen.has(dep)) continue;
|
|
333
|
+
seen.add(dep);
|
|
334
|
+
if (typeof dep === 'function') {
|
|
335
|
+
addOnce(accessed, dep._debugName || UNNAMED);
|
|
336
|
+
} else {
|
|
337
|
+
collectReads(dep, seen);
|
|
229
338
|
}
|
|
230
|
-
// Not a signal's Set, so it belongs to a computed. `_owner` is that
|
|
231
|
-
// computed's inner effect; its own deps are the sources to follow.
|
|
232
|
-
const owner = depSet._owner;
|
|
233
|
-
if (owner?.deps) collectReads(owner.deps, seen);
|
|
234
339
|
}
|
|
235
340
|
};
|
|
236
341
|
|
|
237
|
-
__setDevToolsHooks(trackingHooks);
|
|
238
342
|
try {
|
|
239
343
|
createRoot((disposeRoot) => {
|
|
240
344
|
root.current = disposeRoot;
|
|
@@ -244,20 +348,240 @@ export function trackSignals(fn) {
|
|
|
244
348
|
});
|
|
245
349
|
});
|
|
246
350
|
|
|
247
|
-
|
|
248
|
-
|
|
351
|
+
// The probe itself is not one of fn's reads.
|
|
352
|
+
const seen = new Set([probe]);
|
|
353
|
+
for (const reader of __subsOf(probe)) collectReads(reader, seen);
|
|
249
354
|
} catch (err) {
|
|
250
355
|
thrown = err;
|
|
251
356
|
} finally {
|
|
252
|
-
//
|
|
357
|
+
// Dependencies are read before this: disposal clears them.
|
|
253
358
|
root.current?.();
|
|
254
|
-
|
|
359
|
+
stopWrites();
|
|
255
360
|
}
|
|
256
361
|
|
|
257
362
|
if (thrown) throw thrown;
|
|
258
363
|
return { accessed, written };
|
|
259
364
|
}
|
|
260
365
|
|
|
366
|
+
// --- captureDiagnostics ---
|
|
367
|
+
//
|
|
368
|
+
// Runs a callback and returns the diagnostics it reported plus how many times
|
|
369
|
+
// each effect ran, so a test can fail on a new diagnostic code or on reactive
|
|
370
|
+
// work it did not expect without scraping the console. Effects and computeds
|
|
371
|
+
// are named by their function's name, '(anonymous)' otherwise; `effects` lists
|
|
372
|
+
// each one with its owner, including the known ones that did not run, so an
|
|
373
|
+
// assertion can tell an idle effect from a misspelled name.
|
|
374
|
+
//
|
|
375
|
+
// Reports are kept off the console while the capture is open; the events carry
|
|
376
|
+
// the same message and fix the console would have printed.
|
|
377
|
+
// Effect runs are counted through the shared hook listener above, so captures
|
|
378
|
+
// that overlap (two async tests at once) each see every run made while they are
|
|
379
|
+
// open, installed devtools keep working, and the hooks are restored even when
|
|
380
|
+
// fn throws.
|
|
381
|
+
|
|
382
|
+
function effectName(node) {
|
|
383
|
+
return (node.fn?.inner ?? node.fn)?.name || '(anonymous)';
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function captureDiagnostics(fn) {
|
|
387
|
+
if (!__DEV__) {
|
|
388
|
+
throw new Error(
|
|
389
|
+
'[what] captureDiagnostics() requires a development build: production builds ' +
|
|
390
|
+
'report no diagnostics, so an empty result would look like a pass. Run your ' +
|
|
391
|
+
'tests with NODE_ENV !== "production".'
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const cap = capture({ quiet: true });
|
|
396
|
+
const stopWatching = watchCreations();
|
|
397
|
+
// node -> runs: what existed when the capture opened, then each node as it
|
|
398
|
+
// is created or runs, kept when a disposal drops it from the watch.
|
|
399
|
+
const seen = new Map();
|
|
400
|
+
for (const node of created) seen.set(node, 0);
|
|
401
|
+
const stopRuns = listenToHook('onEffectRun', (node) => {
|
|
402
|
+
seen.set(node, (seen.get(node) ?? 0) + 1);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const finish = () => {
|
|
406
|
+
stopRuns();
|
|
407
|
+
for (const node of created) if (!seen.has(node)) seen.set(node, 0);
|
|
408
|
+
stopWatching();
|
|
409
|
+
const runs = {};
|
|
410
|
+
const effects = [];
|
|
411
|
+
for (const [node, ran] of seen) {
|
|
412
|
+
const name = effectName(node);
|
|
413
|
+
if (ran > 0) runs[name] = (runs[name] || 0) + ran;
|
|
414
|
+
effects.push({
|
|
415
|
+
name,
|
|
416
|
+
kind: 'subHead' in node ? 'computed' : 'effect',
|
|
417
|
+
owner: componentPath(node),
|
|
418
|
+
runs: ran,
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
return { events: cap.stop(), runs, effects };
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
let result;
|
|
425
|
+
try {
|
|
426
|
+
result = fn();
|
|
427
|
+
} catch (err) {
|
|
428
|
+
finish();
|
|
429
|
+
throw err;
|
|
430
|
+
}
|
|
431
|
+
if (result && typeof result.then === 'function') {
|
|
432
|
+
return result.then(
|
|
433
|
+
(value) => ({ ...finish(), result: value }),
|
|
434
|
+
(err) => { finish(); throw err; },
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
return { ...finish(), result };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function describeEvents(events) {
|
|
441
|
+
return events.map((e) => {
|
|
442
|
+
const lines = [` ${e.code}${e.count > 1 ? ` ×${e.count}` : ''} ${e.message}`];
|
|
443
|
+
if (e.owner?.length) lines.push(` in ${e.owner.join(' › ')}`);
|
|
444
|
+
if (e.loc) lines.push(` at ${e.loc}`);
|
|
445
|
+
lines.push(` fix: ${e.fix}`);
|
|
446
|
+
return lines.join('\n');
|
|
447
|
+
}).join('\n');
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Fail unless the capture holds no diagnostics, other than codes in `allow`.
|
|
452
|
+
* @param {{ events: any[] }} captured result of captureDiagnostics()
|
|
453
|
+
* @param {{ allow?: string[] }} [options]
|
|
454
|
+
*/
|
|
455
|
+
export function expectNoDiagnostics(captured, { allow = [] } = {}) {
|
|
456
|
+
const unexpected = captured.events.filter((e) => !allow.includes(e.code));
|
|
457
|
+
if (unexpected.length) {
|
|
458
|
+
throw new Error(`Expected no diagnostics, got ${unexpected.length}:\n${describeEvents(unexpected)}`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// expectDiagnostic() and expectRuns() take the same count options: `exact`, or
|
|
463
|
+
// `min` and/or `max`. With none of them the count is "at least once".
|
|
464
|
+
const COUNT_OPTIONS = ['exact', 'min', 'max'];
|
|
465
|
+
|
|
466
|
+
function readCount(helper, options, names) {
|
|
467
|
+
if (options === null || typeof options !== 'object') {
|
|
468
|
+
throw new TypeError(`[what] ${helper}() takes an options object: { ${names.join(', ')} }.`);
|
|
469
|
+
}
|
|
470
|
+
for (const key of Object.keys(options)) {
|
|
471
|
+
if (!names.includes(key)) {
|
|
472
|
+
throw new TypeError(`[what] ${helper}(): unknown option "${key}". The options are ${names.join(', ')}.`);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
for (const key of COUNT_OPTIONS) {
|
|
476
|
+
const n = options[key];
|
|
477
|
+
if (n !== undefined && !(Number.isInteger(n) && n >= 0)) {
|
|
478
|
+
throw new TypeError(`[what] ${helper}(): ${key} must be a whole number of 0 or more, got ${String(n)}.`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const { exact, min, max } = options;
|
|
482
|
+
if (exact !== undefined && (min !== undefined || max !== undefined)) {
|
|
483
|
+
throw new TypeError(`[what] ${helper}(): exact cannot be combined with min or max.`);
|
|
484
|
+
}
|
|
485
|
+
if (min !== undefined && max !== undefined && min > max) {
|
|
486
|
+
throw new TypeError(`[what] ${helper}(): min (${min}) is greater than max (${max}).`);
|
|
487
|
+
}
|
|
488
|
+
const lo = exact ?? min ?? (max === undefined ? 1 : 0);
|
|
489
|
+
const hi = exact ?? max ?? Infinity;
|
|
490
|
+
let text = `between ${lo} and ${hi} times`;
|
|
491
|
+
if (lo === hi) text = `exactly ${lo} time(s)`;
|
|
492
|
+
else if (hi === Infinity) text = `at least ${lo} time(s)`;
|
|
493
|
+
else if (lo === 0) text = `at most ${hi} time(s)`;
|
|
494
|
+
return { lo, hi, text };
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function readName(helper, key, value) {
|
|
498
|
+
if (value !== undefined && (typeof value !== 'string' || value === '')) {
|
|
499
|
+
throw new TypeError(`[what] ${helper}(): ${key} must be a non-empty string, got ${String(value)}.`);
|
|
500
|
+
}
|
|
501
|
+
return value;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Fail unless the capture holds `code` and no other code, reported the given
|
|
506
|
+
* number of times (at least once by default). A repeat on the same subject
|
|
507
|
+
* folds into one event's `count`, so the occurrences are the counts added up.
|
|
508
|
+
* @param {{ events: any[] }} captured result of captureDiagnostics()
|
|
509
|
+
* @param {{ code: string, exact?: number, min?: number, max?: number }} options
|
|
510
|
+
*/
|
|
511
|
+
export function expectDiagnostic(captured, options) {
|
|
512
|
+
const count = readCount('expectDiagnostic', options, ['code', ...COUNT_OPTIONS]);
|
|
513
|
+
const code = readName('expectDiagnostic', 'code', options.code);
|
|
514
|
+
if (code === undefined) throw new TypeError('[what] expectDiagnostic(): code, the ERR_* code to expect, is required.');
|
|
515
|
+
if (captured.events.some((e) => e.code !== code)) {
|
|
516
|
+
throw new Error(`Expected exactly ${code}, got:\n${describeEvents(captured.events)}`);
|
|
517
|
+
}
|
|
518
|
+
const reported = captured.events.reduce((sum, e) => sum + e.count, 0);
|
|
519
|
+
if (reported < count.lo || reported > count.hi) {
|
|
520
|
+
const got = reported ? `${reported}:\n${describeEvents(captured.events)}` : 'none.';
|
|
521
|
+
throw new Error(`Expected ${code} ${count.text}, got ${got}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function describeRuns(effects) {
|
|
526
|
+
const lines = new Map();
|
|
527
|
+
for (const e of effects) {
|
|
528
|
+
if (e.runs === 0) continue;
|
|
529
|
+
const where = e.owner === null ? ' · outside any component' : ` in ${e.owner.join(' › ')}`;
|
|
530
|
+
const label = `${e.name}${e.kind === 'computed' ? ' (computed)' : ''}`;
|
|
531
|
+
const key = `${label}\n${where}`;
|
|
532
|
+
const line = lines.get(key);
|
|
533
|
+
if (line) line.runs += e.runs;
|
|
534
|
+
else lines.set(key, { label, where, runs: e.runs });
|
|
535
|
+
}
|
|
536
|
+
if (lines.size === 0) return 'No effect ran during the capture.';
|
|
537
|
+
const sorted = [...lines.values()].sort((a, b) => b.runs - a.runs);
|
|
538
|
+
return `All runs:\n${sorted.map((l) => ` ${l.label}: ${l.runs}${l.where}`).join('\n')}`;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Fail unless the selected effects ran the given number of times during the
|
|
543
|
+
* capture (at least once by default). `effect` selects by function name,
|
|
544
|
+
* `component` by the component that created them, components inside it
|
|
545
|
+
* included; with neither, every effect and computed run of the capture counts.
|
|
546
|
+
* A name or component that matches no effect that ran or was known to exist
|
|
547
|
+
* throws, listing what did run. An effect outside any component (`owner`
|
|
548
|
+
* null) is in no component's count.
|
|
549
|
+
* @param {{ effects: Array<{ name: string, kind: string, owner: string[] | null, runs: number }> }} captured result of captureDiagnostics()
|
|
550
|
+
* @param {{ effect?: string, component?: string, exact?: number, min?: number, max?: number }} [options]
|
|
551
|
+
*/
|
|
552
|
+
export function expectRuns(captured, options = {}) {
|
|
553
|
+
const count = readCount('expectRuns', options, ['effect', 'component', ...COUNT_OPTIONS]);
|
|
554
|
+
const name = readName('expectRuns', 'effect', options.effect);
|
|
555
|
+
const component = readName('expectRuns', 'component', options.component);
|
|
556
|
+
const tag = component === undefined ? null : `<${component}>`;
|
|
557
|
+
|
|
558
|
+
let matched = name === undefined && tag === null;
|
|
559
|
+
let ran = 0;
|
|
560
|
+
for (const e of captured.effects) {
|
|
561
|
+
if (name !== undefined && e.name !== name) continue;
|
|
562
|
+
if (tag !== null && !e.owner?.includes(tag)) continue;
|
|
563
|
+
matched = true;
|
|
564
|
+
ran += e.runs;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const subject = name === undefined
|
|
568
|
+
? (tag === null ? 'all effects together' : `effects in ${tag}`)
|
|
569
|
+
: `effect "${name}"${tag === null ? '' : ` in ${tag}`}`;
|
|
570
|
+
const they = name === undefined ? 'they' : 'it';
|
|
571
|
+
const runs = describeRuns(captured.effects);
|
|
572
|
+
if (!matched) {
|
|
573
|
+
const which = `${name === undefined ? '' : ` named "${name}"`}${tag === null ? '' : ` created in ${tag}`}`;
|
|
574
|
+
throw new Error(
|
|
575
|
+
`Expected ${subject} to run ${count.text}, but no effect${which} ran or was known to exist during ` +
|
|
576
|
+
'the capture. An effect that did not run is known only when it was created inside a ' +
|
|
577
|
+
`render()/renderTest() tree or the capture.\n${runs}`
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (ran >= count.lo && ran <= count.hi) return;
|
|
582
|
+
throw new Error(`Expected ${subject} to run ${count.text}, ${they} ran ${ran}.\n${runs}`);
|
|
583
|
+
}
|
|
584
|
+
|
|
261
585
|
// --- mockSignal ---
|
|
262
586
|
// Signal with full history tracking for testing.
|
|
263
587
|
|
|
@@ -299,7 +623,6 @@ export function mockSignal(name, initialValue) {
|
|
|
299
623
|
mock.set = s.set;
|
|
300
624
|
mock.subscribe = s.subscribe;
|
|
301
625
|
if (s._debugName) mock._debugName = s._debugName;
|
|
302
|
-
if (s._subs) mock._subs = s._subs;
|
|
303
626
|
|
|
304
627
|
// Testing-specific properties
|
|
305
628
|
Object.defineProperty(mock, 'history', {
|
|
@@ -476,16 +799,15 @@ export async function waitForElementToBeRemoved(callback, options = {}) {
|
|
|
476
799
|
}
|
|
477
800
|
|
|
478
801
|
// --- Act ---
|
|
479
|
-
//
|
|
802
|
+
// Run `callback`, then wait until what it started has landed and committed:
|
|
803
|
+
// queued effects, async values it made fetch, transitions it opened.
|
|
480
804
|
|
|
481
805
|
export async function act(callback) {
|
|
482
806
|
const result = await callback();
|
|
483
|
-
// Synchronously flush all pending effects
|
|
484
807
|
flushSync();
|
|
485
|
-
// Wait for microtasks to flush
|
|
486
808
|
await /** @type {Promise<void>} */ (new Promise(r => queueMicrotask(() => r())));
|
|
487
|
-
// Wait for any scheduled effects
|
|
488
809
|
await /** @type {Promise<void>} */ (new Promise(r => setTimeout(() => r(), 0)));
|
|
810
|
+
await settled();
|
|
489
811
|
return result;
|
|
490
812
|
}
|
|
491
813
|
|
|
@@ -536,7 +858,7 @@ export function mockComponent(name = 'MockComponent') {
|
|
|
536
858
|
|
|
537
859
|
export const expect = {
|
|
538
860
|
toBeInTheDocument(element) {
|
|
539
|
-
if (!element || !element.
|
|
861
|
+
if (!element || !element.isConnected) {
|
|
540
862
|
throw new Error('Expected element to be in the document');
|
|
541
863
|
}
|
|
542
864
|
},
|
|
@@ -578,9 +900,12 @@ export const expect = {
|
|
|
578
900
|
if (!element) {
|
|
579
901
|
throw new Error('Element not found');
|
|
580
902
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
903
|
+
// Hidden by itself or by an ancestor: display none or opacity 0 anywhere
|
|
904
|
+
// up the tree, visibility as the element inherits it.
|
|
905
|
+
if (window.getComputedStyle(element).visibility === 'hidden') throw new Error('Expected element to be visible');
|
|
906
|
+
for (let el = element; el && el.nodeType === 1; el = el.parentElement) {
|
|
907
|
+
const style = window.getComputedStyle(el);
|
|
908
|
+
if (style.display === 'none' || style.opacity === '0') throw new Error('Expected element to be visible');
|
|
584
909
|
}
|
|
585
910
|
},
|
|
586
911
|
|
|
@@ -588,7 +913,8 @@ export const expect = {
|
|
|
588
913
|
if (!element) {
|
|
589
914
|
throw new Error('Element not found');
|
|
590
915
|
}
|
|
591
|
-
|
|
916
|
+
// :disabled also matches a control inside a disabled <fieldset>.
|
|
917
|
+
if (!element.disabled && !element.matches(':disabled')) {
|
|
592
918
|
throw new Error('Expected element to be disabled');
|
|
593
919
|
}
|
|
594
920
|
},
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// What Framework - hydrating the vnodes compiled JSX never makes, and the
|
|
2
|
+
// fields only what builds one can hand the walk
|
|
3
|
+
//
|
|
4
|
+
// hydrate() adopts a component vnode itself (render.js adoptVNode): the JSX
|
|
5
|
+
// handed to it lowers to one. Every other vnode (an h() or JSX-runtime
|
|
6
|
+
// element, <Suspense>, <ErrorBoundary>, a <Portal>) is made by a vnode
|
|
7
|
+
// producer (h.js), and is adopted by render.js _adoptVNodeKinds (a region's
|
|
8
|
+
// keyed vnode rows by _adoptVNodeRows), which render.js reaches through this
|
|
9
|
+
// module, as h.js installs the vnode renderer through it (and so does
|
|
10
|
+
// render.js's _installVNodeRuntime, what-react's mixed mode's install).
|
|
11
|
+
//
|
|
12
|
+
// What the user changed in the server's form controls before hydration goes
|
|
13
|
+
// to the controls built before the walk reached them (render.js placeBuilt,
|
|
14
|
+
// fieldCarry here) once what can build a form field is in the program: a
|
|
15
|
+
// compiled module whose JSX can (`_$carryFields()` at module scope,
|
|
16
|
+
// installFieldCarry here) or a vnode producer (the vnode renderer installed;
|
|
17
|
+
// what-react's islands, whose renderer builds fields too, are in a mixed app,
|
|
18
|
+
// whose what-react/mixed.js installs one). A field built by hand
|
|
19
|
+
// (document.createElement) in a program with neither keeps what it was built
|
|
20
|
+
// with.
|
|
21
|
+
//
|
|
22
|
+
// Here each is what it names, so a program that hydrates carries the
|
|
23
|
+
// adoption of every kind and the carrying. In a client build it compiles for
|
|
24
|
+
// hydration, what-compiler's Vite plugin serves a stand-in for this module
|
|
25
|
+
// (vite-plugin.js, vnodeAdoptionStandIn) in which installing the vnode
|
|
26
|
+
// renderer also installs the adoption and the carrying, and installFieldCarry
|
|
27
|
+
// installs the carrying: a program none of whose code makes such a vnode or
|
|
28
|
+
// builds a field carries neither, and one that does adopts and carries as it
|
|
29
|
+
// does here. That holds for the adoption because adopting one writes its
|
|
30
|
+
// props and renders through the vnode renderer, which only a producer
|
|
31
|
+
// installs (what-react's elements reach a hydration only in a mixed app,
|
|
32
|
+
// whose what-react/mixed.js imports h()).
|
|
33
|
+
import { _carryFields } from './render.js';
|
|
34
|
+
import { vnodeRenderer } from './vnode-renderer.js';
|
|
35
|
+
export { _adoptVNodeKinds as adoptVNodeKinds, _adoptVNodeRows as adoptVNodeRows } from './render.js';
|
|
36
|
+
export { _installVNodeRuntime as installVNodeRuntime } from './vnode-renderer.js';
|
|
37
|
+
|
|
38
|
+
let carries = false;
|
|
39
|
+
|
|
40
|
+
/** A compiled module whose JSX can build a form field was loaded. */
|
|
41
|
+
export function installFieldCarry() {
|
|
42
|
+
carries = true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What the user changed in `server`'s controls goes to `built`'s, once what can build a field is in the program. */
|
|
46
|
+
export function fieldCarry(server, built) {
|
|
47
|
+
if (carries || vnodeRenderer !== null) _carryFields(server, built);
|
|
48
|
+
}
|