test-renderer 0.11.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/dist/index.cjs ADDED
@@ -0,0 +1,868 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __export = (target, all) => {
38
+ for (var name in all)
39
+ __defProp(target, name, { get: all[name], enumerable: true });
40
+ };
41
+ var __copyProps = (to, from, except, desc) => {
42
+ if (from && typeof from === "object" || typeof from === "function") {
43
+ for (let key of __getOwnPropNames(from))
44
+ if (!__hasOwnProp.call(to, key) && key !== except)
45
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
46
+ }
47
+ return to;
48
+ };
49
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
50
+ // If the importer is in node compatibility mode or this is not an ESM
51
+ // file that has been converted to a CommonJS file using a Babel-
52
+ // compatible transform (i.e. "__esModule" has not been set), then set
53
+ // "default" to the CommonJS "module.exports" for node compatibility.
54
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
55
+ mod
56
+ ));
57
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
58
+
59
+ // src/index.ts
60
+ var index_exports = {};
61
+ __export(index_exports, {
62
+ createRoot: () => createRoot
63
+ });
64
+ module.exports = __toCommonJS(index_exports);
65
+
66
+ // src/renderer.ts
67
+ var import_constants5 = require("react-reconciler/constants");
68
+
69
+ // src/constants.ts
70
+ var Tag = {
71
+ Container: "CONTAINER",
72
+ Instance: "INSTANCE",
73
+ Text: "TEXT"
74
+ };
75
+ var CONTAINER_TYPE = "";
76
+
77
+ // src/query-all.ts
78
+ function queryAll(element, predicate, options) {
79
+ var _a, _b;
80
+ const includeSelf = (_a = options == null ? void 0 : options.includeSelf) != null ? _a : false;
81
+ const matchDeepestOnly = (_b = options == null ? void 0 : options.matchDeepestOnly) != null ? _b : false;
82
+ const results = [];
83
+ const matchingDescendants = [];
84
+ element.children.forEach((child) => {
85
+ if (typeof child === "string") {
86
+ return;
87
+ }
88
+ matchingDescendants.push(...queryAll(child, predicate, __spreadProps(__spreadValues({}, options), { includeSelf: true })));
89
+ });
90
+ if (includeSelf && // When matchDeepestOnly = true: add current element only if no descendants match
91
+ (matchingDescendants.length === 0 || !matchDeepestOnly) && predicate(element)) {
92
+ results.push(element);
93
+ }
94
+ results.push(...matchingDescendants);
95
+ return results;
96
+ }
97
+
98
+ // src/render-to-json.ts
99
+ function renderContainerToJson(instance) {
100
+ return {
101
+ type: CONTAINER_TYPE,
102
+ props: {},
103
+ children: renderChildrenToJson(instance.children),
104
+ $$typeof: /* @__PURE__ */ Symbol.for("react.test.json")
105
+ };
106
+ }
107
+ function renderInstanceToJson(instance) {
108
+ if (instance.isHidden) {
109
+ return null;
110
+ }
111
+ const _a = instance.props, { children: _children } = _a, restProps = __objRest(_a, ["children"]);
112
+ return {
113
+ type: instance.type,
114
+ props: restProps,
115
+ children: renderChildrenToJson(instance.children),
116
+ $$typeof: /* @__PURE__ */ Symbol.for("react.test.json")
117
+ };
118
+ }
119
+ function renderTextInstanceToJson(instance) {
120
+ if (instance.isHidden) {
121
+ return null;
122
+ }
123
+ return instance.text;
124
+ }
125
+ function renderChildrenToJson(children) {
126
+ const result = [];
127
+ for (const child of children) {
128
+ if (child.tag === Tag.Instance) {
129
+ const renderedChild = renderInstanceToJson(child);
130
+ if (renderedChild !== null) {
131
+ result.push(renderedChild);
132
+ }
133
+ } else {
134
+ const renderedChild = renderTextInstanceToJson(child);
135
+ if (renderedChild !== null) {
136
+ result.push(renderedChild);
137
+ }
138
+ }
139
+ }
140
+ return result;
141
+ }
142
+
143
+ // src/host-element.ts
144
+ var instanceMap = /* @__PURE__ */ new WeakMap();
145
+ var HostElement = class _HostElement {
146
+ constructor(instance) {
147
+ this.instance = instance;
148
+ }
149
+ /** The element type (e.g., "div", "span"). Empty string for container. */
150
+ get type() {
151
+ return this.instance.tag === Tag.Instance ? this.instance.type : CONTAINER_TYPE;
152
+ }
153
+ /** The element's props object. */
154
+ get props() {
155
+ return this.instance.tag === Tag.Instance ? this.instance.props : {};
156
+ }
157
+ /** The parent element, or null if this is the root container. */
158
+ get parent() {
159
+ const parentInstance = this.instance.parent;
160
+ if (parentInstance == null) {
161
+ return null;
162
+ }
163
+ return _HostElement.fromInstance(parentInstance);
164
+ }
165
+ /** Array of child nodes (elements and text strings). Hidden children are excluded. */
166
+ get children() {
167
+ const result = this.instance.children.filter((child) => !child.isHidden).map((child) => getHostNodeForInstance(child));
168
+ return result;
169
+ }
170
+ /**
171
+ * Access to the underlying React Fiber node. This is an unstable API that exposes
172
+ * internal react-reconciler structures which may change without warning in future
173
+ * React versions. Use with caution and only when absolutely necessary.
174
+ *
175
+ * @returns The Fiber node for this instance, or null if this is a container.
176
+ */
177
+ get unstable_fiber() {
178
+ return this.instance.tag === Tag.Instance ? this.instance.unstable_fiber : null;
179
+ }
180
+ /**
181
+ * Convert this element to a JSON representation suitable for snapshots.
182
+ *
183
+ * @returns JSON element or null if the element is hidden.
184
+ */
185
+ toJSON() {
186
+ return this.instance.tag === Tag.Container ? renderContainerToJson(this.instance) : renderInstanceToJson(this.instance);
187
+ }
188
+ /**
189
+ * Find all descendant elements matching the predicate.
190
+ *
191
+ * @param predicate - Function that returns true for matching elements.
192
+ * @param options - Optional query configuration.
193
+ * @returns Array of matching elements.
194
+ */
195
+ queryAll(predicate, options) {
196
+ return queryAll(this, predicate, options);
197
+ }
198
+ /** @internal */
199
+ static fromInstance(instance) {
200
+ const hostElement = instanceMap.get(instance);
201
+ if (hostElement) {
202
+ return hostElement;
203
+ }
204
+ const result = new _HostElement(instance);
205
+ instanceMap.set(instance, result);
206
+ return result;
207
+ }
208
+ };
209
+ function getHostNodeForInstance(instance) {
210
+ switch (instance.tag) {
211
+ case Tag.Text:
212
+ return instance.text;
213
+ case Tag.Instance:
214
+ return HostElement.fromInstance(instance);
215
+ }
216
+ }
217
+
218
+ // src/reconciler.ts
219
+ var import_react_reconciler = __toESM(require("react-reconciler"), 1);
220
+ var import_constants3 = require("react-reconciler/constants");
221
+
222
+ // src/utils.ts
223
+ function formatComponentList(names) {
224
+ if (names.length === 0) {
225
+ return "";
226
+ }
227
+ if (names.length === 1) {
228
+ return `<${names[0]}>`;
229
+ }
230
+ if (names.length === 2) {
231
+ return `<${names[0]}> or <${names[1]}>`;
232
+ }
233
+ const allButLast = names.slice(0, -1);
234
+ const last = names[names.length - 1];
235
+ return `${allButLast.map((name) => `<${name}>`).join(", ")}, or <${last}>`;
236
+ }
237
+
238
+ // src/reconciler.ts
239
+ var nodeToInstanceMap = /* @__PURE__ */ new WeakMap();
240
+ var currentUpdatePriority = import_constants3.NoEventPriority;
241
+ var hostConfig = {
242
+ /**
243
+ * The reconciler has two modes: mutation mode and persistent mode. You must specify one of them.
244
+ *
245
+ * If your target platform is similar to the DOM and has methods similar to `appendChild`, `removeChild`,
246
+ * and so on, you'll want to use the **mutation mode**. This is the same mode used by React DOM, React ART,
247
+ * and the classic React Native renderer.
248
+ *
249
+ * ```js
250
+ * const HostConfig = {
251
+ * // ...
252
+ * supportsMutation: true,
253
+ * // ...
254
+ * }
255
+ * ```
256
+ *
257
+ * Depending on the mode, the reconciler will call different methods on your host config.
258
+ * If you're not sure which one you want, you likely need the mutation mode.
259
+ */
260
+ supportsMutation: true,
261
+ /**
262
+ * The reconciler has two modes: mutation mode and persistent mode. You must specify one of them.
263
+ *
264
+ * If your target platform has immutable trees, you'll want the **persistent mode** instead. In that mode,
265
+ * existing nodes are never mutated, and instead every change clones the parent tree and then replaces
266
+ * the whole parent tree at the root. This is the node used by the new React Native renderer, codenamed "Fabric".
267
+ *
268
+ * ```js
269
+ * const HostConfig = {
270
+ * // ...
271
+ * supportsPersistence: true,
272
+ * // ...
273
+ * }
274
+ * ```
275
+ *
276
+ * Depending on the mode, the reconciler will call different methods on your host config.
277
+ * If you're not sure which one you want, you likely need the mutation mode.
278
+ */
279
+ supportsPersistence: false,
280
+ /**
281
+ * #### `createInstance(type, props, rootContainer, hostContext, internalHandle)`
282
+ *
283
+ * This method should return a newly created node. For example, the DOM renderer would call
284
+ * `document.createElement(type)` here and then set the properties from `props`.
285
+ *
286
+ * You can use `rootContainer` to access the root container associated with that tree. For example,
287
+ * in the DOM renderer, this is useful to get the correct `document` reference that the root belongs to.
288
+ *
289
+ * The `hostContext` parameter lets you keep track of some information about your current place in
290
+ * the tree. To learn more about it, see `getChildHostContext` below.
291
+ *
292
+ * The `internalHandle` data structure is meant to be opaque. If you bend the rules and rely on its
293
+ * internal fields, be aware that it may change significantly between versions. You're taking on additional
294
+ * maintenance risk by reading from it, and giving up all guarantees if you write something to it.
295
+ *
296
+ * This method happens **in the render phase**. It can (and usually should) mutate the node it has
297
+ * just created before returning it, but it must not modify any other nodes. It must not register
298
+ * any event handlers on the parent tree. This is because an instance being created doesn't guarantee
299
+ * it would be placed in the tree — it could be left unused and later collected by GC. If you need to do
300
+ * something when an instance is definitely in the tree, look at `commitMount` instead.
301
+ */
302
+ createInstance(type, props, rootContainer, _hostContext, internalHandle) {
303
+ return {
304
+ tag: Tag.Instance,
305
+ type,
306
+ props,
307
+ isHidden: false,
308
+ children: [],
309
+ parent: null,
310
+ rootContainer,
311
+ unstable_fiber: internalHandle
312
+ };
313
+ },
314
+ /**
315
+ * #### `createTextInstance(text, rootContainer, hostContext, internalHandle)`
316
+ *
317
+ * Same as `createInstance`, but for text nodes. If your renderer doesn't support text nodes, you can
318
+ * throw here.
319
+ */
320
+ createTextInstance(text, rootContainer, hostContext, _internalHandle) {
321
+ if (rootContainer.config.textComponents && !hostContext.isInsideText) {
322
+ throw new Error(
323
+ `Invariant Violation: Text strings must be rendered within a ${formatComponentList(
324
+ rootContainer.config.textComponents
325
+ )} component. Detected attempt to render "${text}" string within a <${hostContext.type}> component.`
326
+ );
327
+ }
328
+ return {
329
+ tag: Tag.Text,
330
+ text,
331
+ parent: null,
332
+ isHidden: false
333
+ };
334
+ },
335
+ /**
336
+ * #### `appendInitialChild(parentInstance, child)`
337
+ *
338
+ * This method should mutate the `parentInstance` and add the child to its list of children.
339
+ * For example, in the DOM this would translate to a `parentInstance.appendChild(child)` call.
340
+ *
341
+ * This method happens **in the render phase**. It can mutate `parentInstance` and `child`, but it
342
+ * must not modify any other nodes. It's called while the tree is still being built up and not connected
343
+ * to the actual tree on the screen.
344
+ */
345
+ appendInitialChild: appendChild,
346
+ /**
347
+ * #### `finalizeInitialChildren(instance, type, props, rootContainer, hostContext)`
348
+ *
349
+ * In this method, you can perform some final mutations on the `instance`. Unlike with `createInstance`,
350
+ * by the time `finalizeInitialChildren` is called, all the initial children have already been added to
351
+ * the `instance`, but the instance itself has not yet been connected to the tree on the screen.
352
+ *
353
+ * This method happens **in the render phase**. It can mutate `instance`, but it must not modify any other
354
+ * nodes. It's called while the tree is still being built up and not connected to the actual tree on the screen.
355
+ *
356
+ * There is a second purpose to this method. It lets you specify whether there is some work that needs to
357
+ * happen when the node is connected to the tree on the screen. If you return `true`, the instance will
358
+ * receive a `commitMount` call later. See its documentation below.
359
+ *
360
+ * If you don't want to do anything here, you should return `false`.
361
+ */
362
+ finalizeInitialChildren(_instance, _type, _props, _rootContainer, _hostContext) {
363
+ return false;
364
+ },
365
+ /**
366
+ * #### `shouldSetTextContent(type, props)`
367
+ *
368
+ * Some target platforms support setting an instance's text content without manually creating a text node.
369
+ * For example, in the DOM, you can set `node.textContent` instead of creating a text node and appending it.
370
+ *
371
+ * If you return `true` from this method, React will assume that this node's children are text, and will
372
+ * not create nodes for them. It will instead rely on you to have filled that text during `createInstance`.
373
+ * This is a performance optimization. For example, the DOM renderer returns `true` only if `type` is a
374
+ * known text-only parent (like `'textarea'`) or if `props.children` has a `'string'` type. If you return `true`,
375
+ * you will need to implement `resetTextContent` too.
376
+ *
377
+ * If you don't want to do anything here, you should return `false`.
378
+ * This method happens **in the render phase**. Do not mutate the tree from it.
379
+ */
380
+ shouldSetTextContent(_type, _props) {
381
+ return false;
382
+ },
383
+ setCurrentUpdatePriority(newPriority) {
384
+ currentUpdatePriority = newPriority;
385
+ },
386
+ getCurrentUpdatePriority() {
387
+ return currentUpdatePriority;
388
+ },
389
+ resolveUpdatePriority() {
390
+ return currentUpdatePriority || import_constants3.DefaultEventPriority;
391
+ },
392
+ shouldAttemptEagerTransition() {
393
+ return false;
394
+ },
395
+ /**
396
+ * #### `getRootHostContext(rootContainer)`
397
+ *
398
+ * This method lets you return the initial host context from the root of the tree. See `getChildHostContext`
399
+ * for the explanation of host context.
400
+ *
401
+ * If you don't intend to use host context, you can return `null`.
402
+ * This method happens **in the render phase**. Do not mutate the tree from it.
403
+ */
404
+ getRootHostContext(rootContainer) {
405
+ return {
406
+ type: "ROOT",
407
+ config: rootContainer.config,
408
+ isInsideText: false
409
+ };
410
+ },
411
+ /**
412
+ * #### `getChildHostContext(parentHostContext, type, rootContainer)`
413
+ *
414
+ * Host context lets you track some information about where you are in the tree so that it's available
415
+ * inside `createInstance` as the `hostContext` parameter. For example, the DOM renderer uses it to track
416
+ * whether it's inside an HTML or an SVG tree, because `createInstance` implementation needs to be
417
+ * different for them.
418
+ *
419
+ * If the node of this `type` does not influence the context you want to pass down, you can return
420
+ * `parentHostContext`. Alternatively, you can return any custom object representing the information
421
+ * you want to pass down.
422
+ *
423
+ * If you don't want to do anything here, return `parentHostContext`.
424
+ *
425
+ * This method happens **in the render phase**. Do not mutate the tree from it.
426
+ */
427
+ getChildHostContext(parentHostContext, type) {
428
+ var _a;
429
+ const isInsideText = Boolean((_a = parentHostContext.config.textComponents) == null ? void 0 : _a.includes(type));
430
+ return __spreadProps(__spreadValues({}, parentHostContext), { type, isInsideText });
431
+ },
432
+ /**
433
+ * #### `getPublicInstance(instance)`
434
+ *
435
+ * Determines what object gets exposed as a ref. You'll likely want to return the `instance` itself. But
436
+ * in some cases it might make sense to only expose some part of it.
437
+ *
438
+ * If you don't want to do anything here, return `instance`.
439
+ */
440
+ getPublicInstance(instance) {
441
+ switch (instance.tag) {
442
+ case Tag.Instance: {
443
+ const createNodeMock = instance.rootContainer.config.createNodeMock;
444
+ const mockNode = createNodeMock({
445
+ type: instance.type,
446
+ props: instance.props,
447
+ key: null
448
+ });
449
+ nodeToInstanceMap.set(mockNode, instance);
450
+ return mockNode;
451
+ }
452
+ default:
453
+ return instance;
454
+ }
455
+ },
456
+ /**
457
+ * #### `prepareForCommit(containerInfo)`
458
+ *
459
+ * This method lets you store some information before React starts making changes to the tree on
460
+ * the screen. For example, the DOM renderer stores the current text selection so that it can later
461
+ * restore it. This method is mirrored by `resetAfterCommit`.
462
+ *
463
+ * Even if you don't want to do anything here, you need to return `null` from it.
464
+ */
465
+ prepareForCommit(_containerInfo) {
466
+ return null;
467
+ },
468
+ /**
469
+ * #### `resetAfterCommit(containerInfo)`
470
+ *
471
+ * This method is called right after React has performed the tree mutations. You can use it to restore
472
+ * something you've stored in `prepareForCommit` — for example, text selection.
473
+ *
474
+ * You can leave it empty.
475
+ */
476
+ resetAfterCommit(_containerInfo) {
477
+ },
478
+ /**
479
+ * #### `preparePortalMount(containerInfo)`
480
+ *
481
+ * This method is called for a container that's used as a portal target. Usually you can leave it empty.
482
+ */
483
+ preparePortalMount(_containerInfo) {
484
+ },
485
+ /**
486
+ * #### `scheduleTimeout(fn, delay)`
487
+ *
488
+ * You can proxy this to `setTimeout` or its equivalent in your environment.
489
+ */
490
+ scheduleTimeout: setTimeout,
491
+ /**
492
+ * #### `cancelTimeout(id)`
493
+ *
494
+ * You can proxy this to `clearTimeout` or its equivalent in your environment.
495
+ */
496
+ cancelTimeout: clearTimeout,
497
+ /**
498
+ * #### `noTimeout`
499
+ *
500
+ * This is a property (not a function) that should be set to something that can never be a valid timeout ID.
501
+ * For example, you can set it to `-1`.
502
+ */
503
+ noTimeout: -1,
504
+ /**
505
+ * #### `supportsMicrotasks`
506
+ *
507
+ * Set this to `true` to indicate that your renderer supports `scheduleMicrotask`. We use microtasks as part
508
+ * of our discrete event implementation in React DOM. If you're not sure if your renderer should support this,
509
+ * you probably should. The option to not implement `scheduleMicrotask` exists so that platforms with more control
510
+ * over user events, like React Native, can choose to use a different mechanism.
511
+ */
512
+ supportsMicrotasks: true,
513
+ /**
514
+ * #### `scheduleMicrotask(fn)`
515
+ *
516
+ * Optional. You can proxy this to `queueMicrotask` or its equivalent in your environment.
517
+ */
518
+ scheduleMicrotask: queueMicrotask,
519
+ /**
520
+ * #### `isPrimaryRenderer`
521
+ *
522
+ * This is a property (not a function) that should be set to `true` if your renderer is the main one on the
523
+ * page. For example, if you're writing a renderer for the Terminal, it makes sense to set it to `true`, but
524
+ * if your renderer is used *on top of* React DOM or some other existing renderer, set it to `false`.
525
+ */
526
+ isPrimaryRenderer: true,
527
+ /**
528
+ * Whether the renderer shouldn't trigger missing `act()` warnings
529
+ */
530
+ warnsIfNotActing: true,
531
+ getInstanceFromNode(node) {
532
+ const instance = nodeToInstanceMap.get(node);
533
+ if (instance !== void 0) {
534
+ return instance.unstable_fiber;
535
+ }
536
+ return null;
537
+ },
538
+ beforeActiveInstanceBlur() {
539
+ },
540
+ afterActiveInstanceBlur() {
541
+ },
542
+ prepareScopeUpdate(scopeInstance, instance) {
543
+ nodeToInstanceMap.set(scopeInstance, instance);
544
+ },
545
+ getInstanceFromScope(scopeInstance) {
546
+ var _a;
547
+ return (_a = nodeToInstanceMap.get(scopeInstance)) != null ? _a : null;
548
+ },
549
+ detachDeletedInstance(_node) {
550
+ },
551
+ /**
552
+ * #### `appendChild(parentInstance, child)`
553
+ *
554
+ * This method should mutate the `parentInstance` and add the child to its list of children. For example,
555
+ * in the DOM this would translate to a `parentInstance.appendChild(child)` call.
556
+ *
557
+ * Although this method currently runs in the commit phase, you still should not mutate any other nodes
558
+ * in it. If you need to do some additional work when a node is definitely connected to the visible tree, look at `commitMount`.
559
+ */
560
+ appendChild,
561
+ /**
562
+ * #### `appendChildToContainer(container, child)`
563
+ *
564
+ * Same as `appendChild`, but for when a node is attached to the root container. This is useful if attaching
565
+ * to the root has a slightly different implementation, or if the root container nodes are of a different
566
+ * type than the rest of the tree.
567
+ */
568
+ appendChildToContainer: appendChild,
569
+ /**
570
+ * #### `insertBefore(parentInstance, child, beforeChild)`
571
+ *
572
+ * This method should mutate the `parentInstance` and place the `child` before `beforeChild` in the list of
573
+ * its children. For example, in the DOM this would translate to a `parentInstance.insertBefore(child, beforeChild)` call.
574
+ *
575
+ * Note that React uses this method both for insertions and for reordering nodes. Similar to DOM, it is expected
576
+ * that you can call `insertBefore` to reposition an existing child. Do not mutate any other parts of the tree from it.
577
+ */
578
+ insertBefore,
579
+ /**
580
+ * #### `insertInContainerBefore(container, child, beforeChild)
581
+ *
582
+ * Same as `insertBefore`, but for when a node is attached to the root container. This is useful if attaching
583
+ * to the root has a slightly different implementation, or if the root container nodes are of a different type
584
+ * than the rest of the tree.
585
+ */
586
+ insertInContainerBefore: insertBefore,
587
+ /**
588
+ * #### `removeChild(parentInstance, child)`
589
+ *
590
+ * This method should mutate the `parentInstance` to remove the `child` from the list of its children.
591
+ *
592
+ * React will only call it for the top-level node that is being removed. It is expected that garbage collection
593
+ * would take care of the whole subtree. You are not expected to traverse the child tree in it.
594
+ */
595
+ removeChild,
596
+ /**
597
+ * #### `removeChildFromContainer(container, child)`
598
+ *
599
+ * Same as `removeChild`, but for when a node is detached from the root container. This is useful if attaching
600
+ * to the root has a slightly different implementation, or if the root container nodes are of a different type
601
+ * than the rest of the tree.
602
+ */
603
+ removeChildFromContainer: removeChild,
604
+ /**
605
+ * #### `resetTextContent(instance)`
606
+ *
607
+ * If you returned `true` from `shouldSetTextContent` for the previous props, but returned `false` from
608
+ * `shouldSetTextContent` for the next props, React will call this method so that you can clear the text
609
+ * content you were managing manually. For example, in the DOM you could set `node.textContent = ''`.
610
+ *
611
+ * If you never return `true` from `shouldSetTextContent`, you can leave it empty.
612
+ */
613
+ resetTextContent(_instance) {
614
+ },
615
+ /**
616
+ * #### `commitTextUpdate(textInstance, prevText, nextText)`
617
+ *
618
+ * This method should mutate the `textInstance` and update its text content to `nextText`.
619
+ *
620
+ * Here, `textInstance` is a node created by `createTextInstance`.
621
+ */
622
+ commitTextUpdate(textInstance, _oldText, newText) {
623
+ textInstance.text = newText;
624
+ },
625
+ /**
626
+ * #### `commitMount(instance, type, props, internalHandle)`
627
+ *
628
+ * This method is only called if you returned `true` from `finalizeInitialChildren` for this instance.
629
+ *
630
+ * It lets you do some additional work after the node is actually attached to the tree on the screen for
631
+ * the first time. For example, the DOM renderer uses it to trigger focus on nodes with the `autoFocus` attribute.
632
+ *
633
+ * Note that `commitMount` does not mirror `removeChild` one to one because `removeChild` is only called for
634
+ * the top-level removed node. This is why ideally `commitMount` should not mutate any nodes other than the
635
+ * `instance` itself. For example, if it registers some events on some node above, it will be your responsibility
636
+ * to traverse the tree in `removeChild` and clean them up, which is not ideal.
637
+ *
638
+ * The `internalHandle` data structure is meant to be opaque. If you bend the rules and rely on its internal
639
+ * fields, be aware that it may change significantly between versions. You're taking on additional maintenance
640
+ * risk by reading from it, and giving up all guarantees if you write something to it.
641
+ *
642
+ * If you never return `true` from `finalizeInitialChildren`, you can leave it empty.
643
+ */
644
+ commitMount(_instance, _type, _props, _internalHandle) {
645
+ },
646
+ /**
647
+ * #### `commitUpdate(instance, type, prevProps, nextProps, internalHandle)`
648
+ *
649
+ * This method should mutate the `instance` according to the set of changes in `updatePayload`. Here, `updatePayload`
650
+ * is the object that you've returned from `prepareUpdate` and has an arbitrary structure that makes sense for your
651
+ * renderer. For example, the DOM renderer returns an update payload like `[prop1, value1, prop2, value2, ...]` from
652
+ * `prepareUpdate`, and that structure gets passed into `commitUpdate`. Ideally, all the diffing and calculation
653
+ * should happen inside `prepareUpdate` so that `commitUpdate` can be fast and straightforward.
654
+ *
655
+ * The `internalHandle` data structure is meant to be opaque. If you bend the rules and rely on its internal fields,
656
+ * be aware that it may change significantly between versions. You're taking on additional maintenance risk by
657
+ * reading from it, and giving up all guarantees if you write something to it.
658
+ */
659
+ // @ts-expect-error @types/react-reconciler types don't fully match react-reconciler's actual Flow types.
660
+ // Correctness is verified through tests.
661
+ commitUpdate(instance, type, _prevProps, nextProps, internalHandle) {
662
+ instance.type = type;
663
+ instance.props = nextProps;
664
+ instance.unstable_fiber = internalHandle;
665
+ },
666
+ /**
667
+ * #### `hideInstance(instance)`
668
+ *
669
+ * This method should make the `instance` invisible without removing it from the tree. For example, it can apply
670
+ * visual styling to hide it. It is used by Suspense to hide the tree while the fallback is visible.
671
+ */
672
+ hideInstance(instance) {
673
+ instance.isHidden = true;
674
+ },
675
+ /**
676
+ * #### `hideTextInstance(textInstance)`
677
+ *
678
+ * Same as `hideInstance`, but for nodes created by `createTextInstance`.
679
+ */
680
+ hideTextInstance(textInstance) {
681
+ textInstance.isHidden = true;
682
+ },
683
+ /**
684
+ * #### `unhideInstance(instance, props)`
685
+ *
686
+ * This method should make the `instance` visible, undoing what `hideInstance` did.
687
+ */
688
+ unhideInstance(instance, _props) {
689
+ instance.isHidden = false;
690
+ },
691
+ /**
692
+ * #### `unhideTextInstance(textInstance, text)`
693
+ *
694
+ * Same as `unhideInstance`, but for nodes created by `createTextInstance`.
695
+ */
696
+ unhideTextInstance(textInstance, _text) {
697
+ textInstance.isHidden = false;
698
+ },
699
+ /**
700
+ * #### `clearContainer(container)`
701
+ *
702
+ * This method should mutate the `container` root node and remove all children from it.
703
+ */
704
+ clearContainer(container) {
705
+ container.children.forEach((child) => {
706
+ child.parent = null;
707
+ });
708
+ container.children.splice(0);
709
+ },
710
+ /**
711
+ * #### `maySuspendCommit(type, props)`
712
+ *
713
+ * This method is called during render to determine if the Host Component type and props require
714
+ * some kind of loading process to complete before committing an update.
715
+ */
716
+ maySuspendCommit(_type, _props) {
717
+ return false;
718
+ },
719
+ /**
720
+ * #### `preloadInstance(type, props)`
721
+ *
722
+ * This method may be called during render if the Host Component type and props might suspend a commit.
723
+ * It can be used to initiate any work that might shorten the duration of a suspended commit.
724
+ */
725
+ preloadInstance(_type, _props) {
726
+ return true;
727
+ },
728
+ /**
729
+ * #### `startSuspendingCommit()`
730
+ *
731
+ * This method is called just before the commit phase. Use it to set up any necessary state while any Host
732
+ * Components that might suspend this commit are evaluated to determine if the commit must be suspended.
733
+ */
734
+ startSuspendingCommit() {
735
+ },
736
+ /**
737
+ * #### `suspendInstance(type, props)`
738
+ *
739
+ * This method is called after `startSuspendingCommit` for each Host Component that indicated it might
740
+ * suspend a commit.
741
+ */
742
+ suspendInstance() {
743
+ },
744
+ /**
745
+ * #### `waitForCommitToBeReady()`
746
+ *
747
+ * This method is called after all `suspendInstance` calls are complete.
748
+ *
749
+ * Return `null` if the commit can happen immediately.
750
+ * Return `(initiateCommit: Function) => Function` if the commit must be suspended. The argument to this
751
+ * callback will initiate the commit when called. The return value is a cancellation function that the
752
+ * Reconciler can use to abort the commit.
753
+ */
754
+ waitForCommitToBeReady() {
755
+ return null;
756
+ },
757
+ // -------------------
758
+ // Hydration Methods
759
+ // (optional)
760
+ // You can optionally implement hydration to "attach" to the existing tree during the initial render instead
761
+ // of creating it from scratch. For example, the DOM renderer uses this to attach to an HTML markup.
762
+ //
763
+ // To support hydration, you need to declare `supportsHydration: true` and then implement the methods in
764
+ // the "Hydration" section [listed in this file](https://github.com/facebook/react/blob/master/packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js).
765
+ // File an issue if you need help.
766
+ // -------------------
767
+ supportsHydration: false,
768
+ NotPendingTransition: null,
769
+ resetFormInstance(_form) {
770
+ },
771
+ requestPostPaintCallback(_callback) {
772
+ }
773
+ };
774
+ var TestReconciler = (0, import_react_reconciler.default)(hostConfig);
775
+ function appendChild(parentInstance, child) {
776
+ const index = parentInstance.children.indexOf(child);
777
+ if (index !== -1) {
778
+ parentInstance.children.splice(index, 1);
779
+ }
780
+ child.parent = parentInstance;
781
+ parentInstance.children.push(child);
782
+ }
783
+ function insertBefore(parentInstance, child, beforeChild) {
784
+ const index = parentInstance.children.indexOf(child);
785
+ if (index !== -1) {
786
+ parentInstance.children.splice(index, 1);
787
+ }
788
+ child.parent = parentInstance;
789
+ const beforeIndex = parentInstance.children.indexOf(beforeChild);
790
+ parentInstance.children.splice(beforeIndex, 0, child);
791
+ }
792
+ function removeChild(parentInstance, child) {
793
+ const index = parentInstance.children.indexOf(child);
794
+ parentInstance.children.splice(index, 1);
795
+ child.parent = null;
796
+ }
797
+
798
+ // src/renderer.ts
799
+ var defaultCreateMockNode = () => ({});
800
+ var defaultOnUncaughtError = (error, errorInfo) => {
801
+ console.error("Uncaught error:", error, errorInfo);
802
+ };
803
+ var defaultOnCaughtError = (error, errorInfo) => {
804
+ console.error("Caught error:", error, errorInfo);
805
+ };
806
+ var defaultOnRecoverableError = (error, errorInfo) => {
807
+ console.error("Recoverable error:", error, errorInfo);
808
+ };
809
+ function createRoot(options) {
810
+ var _a, _b, _c, _d, _e, _f;
811
+ let container = {
812
+ tag: Tag.Container,
813
+ parent: null,
814
+ children: [],
815
+ isHidden: false,
816
+ config: {
817
+ textComponents: options == null ? void 0 : options.textComponents,
818
+ createNodeMock: (_a = options == null ? void 0 : options.createNodeMock) != null ? _a : defaultCreateMockNode
819
+ }
820
+ };
821
+ let containerFiber = TestReconciler.createContainer(
822
+ container,
823
+ import_constants5.ConcurrentRoot,
824
+ null,
825
+ // hydrationCallbacks
826
+ (_b = options == null ? void 0 : options.isStrictMode) != null ? _b : false,
827
+ false,
828
+ // concurrentUpdatesByDefaultOverride
829
+ (_c = options == null ? void 0 : options.identifierPrefix) != null ? _c : "",
830
+ (_d = options == null ? void 0 : options.onUncaughtError) != null ? _d : defaultOnUncaughtError,
831
+ (_e = options == null ? void 0 : options.onCaughtError) != null ? _e : defaultOnCaughtError,
832
+ // @ts-expect-error @types/react-reconciler types don't include onRecoverableError parameter
833
+ // in the createContainer signature, but react-reconciler's actual Flow types do.
834
+ // Correctness is verified through tests.
835
+ (_f = options == null ? void 0 : options.onRecoverableError) != null ? _f : defaultOnRecoverableError,
836
+ null
837
+ // transitionCallbacks
838
+ );
839
+ const render = (element) => {
840
+ if (containerFiber == null) {
841
+ throw new Error("Cannot render after unmount");
842
+ }
843
+ TestReconciler.updateContainer(element, containerFiber, null, null);
844
+ };
845
+ const unmount = () => {
846
+ if (container == null) {
847
+ return;
848
+ }
849
+ TestReconciler.updateContainer(null, containerFiber, null, null);
850
+ containerFiber = null;
851
+ container = null;
852
+ };
853
+ return {
854
+ render,
855
+ unmount,
856
+ get container() {
857
+ if (container == null) {
858
+ throw new Error("Cannot access .container on unmounted test renderer");
859
+ }
860
+ return HostElement.fromInstance(container);
861
+ }
862
+ };
863
+ }
864
+ // Annotate the CommonJS export names for ESM import in node:
865
+ 0 && (module.exports = {
866
+ createRoot
867
+ });
868
+ //# sourceMappingURL=index.cjs.map