universal-test-renderer 0.2.0 → 0.3.1-react19.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.js CHANGED
@@ -1,6 +1,22 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
1
4
  var __getOwnPropSymbols = Object.getOwnPropertySymbols;
2
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
3
6
  var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
4
20
  var __objRest = (source, exclude) => {
5
21
  var target = {};
6
22
  for (var prop in source)
@@ -16,7 +32,10 @@ var __objRest = (source, exclude) => {
16
32
 
17
33
  // src/reconciler.ts
18
34
  import ReactReconciler from "react-reconciler";
19
- import { DefaultEventPriority } from "react-reconciler/constants";
35
+ import {
36
+ DefaultEventPriority,
37
+ NoEventPriority
38
+ } from "react-reconciler/constants";
20
39
 
21
40
  // src/utils.ts
22
41
  function formatComponentList(names) {
@@ -35,8 +54,8 @@ function formatComponentList(names) {
35
54
  }
36
55
 
37
56
  // src/reconciler.ts
38
- var UPDATE_SIGNAL = {};
39
57
  var nodeToInstanceMap = /* @__PURE__ */ new WeakMap();
58
+ var currentUpdatePriority = NoEventPriority;
40
59
  var hostConfig = {
41
60
  /**
42
61
  * The reconciler has two modes: mutation mode and persistent mode. You must specify one of them.
@@ -77,6 +96,8 @@ var hostConfig = {
77
96
  */
78
97
  supportsPersistence: false,
79
98
  /**
99
+ * #### `createInstance(type, props, rootContainer, hostContext, internalHandle)`
100
+ *
80
101
  * This method should return a newly created node. For example, the DOM renderer would call
81
102
  * `document.createElement(type)` here and then set the properties from `props`.
82
103
  *
@@ -109,14 +130,16 @@ var hostConfig = {
109
130
  };
110
131
  },
111
132
  /**
133
+ * #### `createTextInstance(text, rootContainer, hostContext, internalHandle)`
134
+ *
112
135
  * Same as `createInstance`, but for text nodes. If your renderer doesn't support text nodes, you can
113
136
  * throw here.
114
137
  */
115
138
  createTextInstance(text, rootContainer, hostContext, _internalHandle) {
116
- if (rootContainer.textComponents && !hostContext.isInsideText) {
139
+ if (rootContainer.config.textComponents && !hostContext.isInsideText) {
117
140
  throw new Error(
118
141
  `Invariant Violation: Text strings must be rendered within a ${formatComponentList(
119
- rootContainer.textComponents
142
+ rootContainer.config.textComponents
120
143
  )} component. Detected attempt to render "${text}" string within a <${hostContext.type}> component.`
121
144
  );
122
145
  }
@@ -127,8 +150,20 @@ var hostConfig = {
127
150
  isHidden: false
128
151
  };
129
152
  },
153
+ /**
154
+ * #### `appendInitialChild(parentInstance, child)`
155
+ *
156
+ * This method should mutate the `parentInstance` and add the child to its list of children.
157
+ * For example, in the DOM this would translate to a `parentInstance.appendChild(child)` call.
158
+ *
159
+ * This method happens **in the render phase**. It can mutate `parentInstance` and `child`, but it
160
+ * must not modify any other nodes. It's called while the tree is still being built up and not connected
161
+ * to the actual tree on the screen.
162
+ */
130
163
  appendInitialChild: appendChild,
131
164
  /**
165
+ * #### `finalizeInitialChildren(instance, type, props, rootContainer, hostContext)`
166
+ *
132
167
  * In this method, you can perform some final mutations on the `instance`. Unlike with `createInstance`,
133
168
  * by the time `finalizeInitialChildren` is called, all the initial children have already been added to
134
169
  * the `instance`, but the instance itself has not yet been connected to the tree on the screen.
@@ -146,22 +181,8 @@ var hostConfig = {
146
181
  return false;
147
182
  },
148
183
  /**
149
- * React calls this method so that you can compare the previous and the next props, and decide whether you
150
- * need to update the underlying instance or not. If you don't need to update it, return `null`. If you
151
- * need to update it, you can return an arbitrary object representing the changes that need to happen.
152
- * Then in `commitUpdate` you would need to apply those changes to the instance.
184
+ * #### `shouldSetTextContent(type, props)`
153
185
  *
154
- * This method happens **in the render phase**. It should only *calculate* the update — but not apply it!
155
- * For example, the DOM renderer returns an array that looks like `[prop1, value1, prop2, value2, ...]`
156
- * for all props that have actually changed. And only in `commitUpdate` it applies those changes. You should
157
- * calculate as much as you can in `prepareUpdate` so that `commitUpdate` can be very fast and straightforward.
158
- *
159
- * See the meaning of `rootContainer` and `hostContext` in the `createInstance` documentation.
160
- */
161
- prepareUpdate(_instance, _type, _oldProps, _newProps, _rootContainer, _hostContext) {
162
- return UPDATE_SIGNAL;
163
- },
164
- /**
165
186
  * Some target platforms support setting an instance's text content without manually creating a text node.
166
187
  * For example, in the DOM, you can set `node.textContent` instead of creating a text node and appending it.
167
188
  *
@@ -177,17 +198,37 @@ var hostConfig = {
177
198
  shouldSetTextContent(_type, _props) {
178
199
  return false;
179
200
  },
201
+ setCurrentUpdatePriority(newPriority) {
202
+ currentUpdatePriority = newPriority;
203
+ },
204
+ getCurrentUpdatePriority() {
205
+ return currentUpdatePriority;
206
+ },
207
+ resolveUpdatePriority() {
208
+ return currentUpdatePriority || DefaultEventPriority;
209
+ },
210
+ shouldAttemptEagerTransition() {
211
+ return false;
212
+ },
180
213
  /**
214
+ * #### `getRootHostContext(rootContainer)`
215
+ *
181
216
  * This method lets you return the initial host context from the root of the tree. See `getChildHostContext`
182
217
  * for the explanation of host context.
183
218
  *
184
219
  * If you don't intend to use host context, you can return `null`.
185
220
  * This method happens **in the render phase**. Do not mutate the tree from it.
186
221
  */
187
- getRootHostContext(_rootContainer) {
188
- return { type: "ROOT", isInsideText: false };
222
+ getRootHostContext(rootContainer) {
223
+ return {
224
+ type: "ROOT",
225
+ config: rootContainer.config,
226
+ isInsideText: false
227
+ };
189
228
  },
190
229
  /**
230
+ * #### `getChildHostContext(parentHostContext, type, rootContainer)`
231
+ *
191
232
  * Host context lets you track some information about where you are in the tree so that it's available
192
233
  * inside `createInstance` as the `hostContext` parameter. For example, the DOM renderer uses it to track
193
234
  * whether it's inside an HTML or an SVG tree, because `createInstance` implementation needs to be
@@ -201,12 +242,16 @@ var hostConfig = {
201
242
  *
202
243
  * This method happens **in the render phase**. Do not mutate the tree from it.
203
244
  */
204
- getChildHostContext(parentHostContext, type, rootContainer) {
245
+ getChildHostContext(parentHostContext, type) {
205
246
  var _a;
206
- const isInsideText = Boolean((_a = rootContainer.textComponents) == null ? void 0 : _a.includes(type));
207
- return { type, isInsideText };
247
+ const isInsideText = Boolean(
248
+ (_a = parentHostContext.config.textComponents) == null ? void 0 : _a.includes(type)
249
+ );
250
+ return __spreadProps(__spreadValues({}, parentHostContext), { type, isInsideText });
208
251
  },
209
252
  /**
253
+ * #### `getPublicInstance(instance)`
254
+ *
210
255
  * Determines what object gets exposed as a ref. You'll likely want to return the `instance` itself. But
211
256
  * in some cases it might make sense to only expose some part of it.
212
257
  *
@@ -215,7 +260,7 @@ var hostConfig = {
215
260
  getPublicInstance(instance) {
216
261
  switch (instance.tag) {
217
262
  case "INSTANCE": {
218
- const createNodeMock = instance.rootContainer.createNodeMock;
263
+ const createNodeMock = instance.rootContainer.config.createNodeMock;
219
264
  const mockNode = createNodeMock({
220
265
  type: instance.type,
221
266
  props: instance.props,
@@ -229,6 +274,8 @@ var hostConfig = {
229
274
  }
230
275
  },
231
276
  /**
277
+ * #### `prepareForCommit(containerInfo)`
278
+ *
232
279
  * This method lets you store some information before React starts making changes to the tree on
233
280
  * the screen. For example, the DOM renderer stores the current text selection so that it can later
234
281
  * restore it. This method is mirrored by `resetAfterCommit`.
@@ -239,6 +286,8 @@ var hostConfig = {
239
286
  return null;
240
287
  },
241
288
  /**
289
+ * #### `resetAfterCommit(containerInfo)`
290
+ *
242
291
  * This method is called right after React has performed the tree mutations. You can use it to restore
243
292
  * something you've stored in `prepareForCommit` — for example, text selection.
244
293
  *
@@ -247,21 +296,34 @@ var hostConfig = {
247
296
  resetAfterCommit(_containerInfo) {
248
297
  },
249
298
  /**
299
+ * #### `preparePortalMount(containerInfo)`
300
+ *
250
301
  * This method is called for a container that's used as a portal target. Usually you can leave it empty.
251
302
  */
252
303
  preparePortalMount(_containerInfo) {
253
304
  },
254
305
  /**
306
+ * #### `scheduleTimeout(fn, delay)`
307
+ *
255
308
  * You can proxy this to `setTimeout` or its equivalent in your environment.
256
309
  */
257
310
  scheduleTimeout: setTimeout,
311
+ /**
312
+ * #### `cancelTimeout(id)`
313
+ *
314
+ * You can proxy this to `clearTimeout` or its equivalent in your environment.
315
+ */
258
316
  cancelTimeout: clearTimeout,
259
317
  /**
318
+ * #### `noTimeout`
319
+ *
260
320
  * This is a property (not a function) that should be set to something that can never be a valid timeout ID.
261
321
  * For example, you can set it to `-1`.
262
322
  */
263
323
  noTimeout: -1,
264
324
  /**
325
+ * #### `supportsMicrotasks`
326
+ *
265
327
  * Set this to `true` to indicate that your renderer supports `scheduleMicrotask`. We use microtasks as part
266
328
  * of our discrete event implementation in React DOM. If you're not sure if your renderer should support this,
267
329
  * you probably should. The option to not implement `scheduleMicrotask` exists so that platforms with more control
@@ -269,10 +331,14 @@ var hostConfig = {
269
331
  */
270
332
  supportsMicrotasks: true,
271
333
  /**
334
+ * #### `scheduleMicrotask(fn)`
335
+ *
272
336
  * Optional. You can proxy this to `queueMicrotask` or its equivalent in your environment.
273
337
  */
274
338
  scheduleMicrotask: queueMicrotask,
275
339
  /**
340
+ * #### `isPrimaryRenderer`
341
+ *
276
342
  * This is a property (not a function) that should be set to `true` if your renderer is the main one on the
277
343
  * page. For example, if you're writing a renderer for the Terminal, it makes sense to set it to `true`, but
278
344
  * if your renderer is used *on top of* React DOM or some other existing renderer, set it to `false`.
@@ -282,46 +348,6 @@ var hostConfig = {
282
348
  * Whether the renderer shouldn't trigger missing `act()` warnings
283
349
  */
284
350
  warnsIfNotActing: true,
285
- /**
286
- * To implement this method, you'll need some constants available on the special `react-reconciler/constants` entry point:
287
- *
288
- * ```
289
- * import {
290
- * DiscreteEventPriority,
291
- * ContinuousEventPriority,
292
- * DefaultEventPriority,
293
- * } from 'react-reconciler/constants';
294
- *
295
- * const HostConfig = {
296
- * // ...
297
- * getCurrentEventPriority() {
298
- * return DefaultEventPriority;
299
- * },
300
- * // ...
301
- * }
302
- *
303
- * const MyRenderer = Reconciler(HostConfig);
304
- * ```
305
- *
306
- * The constant you return depends on which event, if any, is being handled right now. (In the browser, you can
307
- * check this using `window.event && window.event.type`).
308
- *
309
- * - **Discrete events**: If the active event is directly caused by the user (such as mouse and keyboard events)
310
- * and each event in a sequence is intentional (e.g. click), return DiscreteEventPriority. This tells React that
311
- * they should interrupt any background work and cannot be batched across time.
312
- *
313
- * - **Continuous events**: If the active event is directly caused by the user but the user can't distinguish between
314
- * individual events in a sequence (e.g. mouseover), return ContinuousEventPriority. This tells React they
315
- * should interrupt any background work but can be batched across time.
316
- *
317
- * - **Other events / No active event**: In all other cases, return DefaultEventPriority. This tells React that
318
- * this event is considered background work, and interactive events will be prioritized over it.
319
- *
320
- * You can consult the `getCurrentEventPriority()` implementation in `ReactDOMHostConfig.js` for a reference implementation.
321
- */
322
- getCurrentEventPriority() {
323
- return DefaultEventPriority;
324
- },
325
351
  getInstanceFromNode(node) {
326
352
  const instance = nodeToInstanceMap.get(node);
327
353
  if (instance !== void 0) {
@@ -343,6 +369,8 @@ var hostConfig = {
343
369
  detachDeletedInstance(_node) {
344
370
  },
345
371
  /**
372
+ * #### `appendChild(parentInstance, child)`
373
+ *
346
374
  * This method should mutate the `parentInstance` and add the child to its list of children. For example,
347
375
  * in the DOM this would translate to a `parentInstance.appendChild(child)` call.
348
376
  *
@@ -351,12 +379,16 @@ var hostConfig = {
351
379
  */
352
380
  appendChild,
353
381
  /**
382
+ * #### `appendChildToContainer(container, child)`
383
+ *
354
384
  * Same as `appendChild`, but for when a node is attached to the root container. This is useful if attaching
355
385
  * to the root has a slightly different implementation, or if the root container nodes are of a different
356
386
  * type than the rest of the tree.
357
387
  */
358
388
  appendChildToContainer: appendChild,
359
389
  /**
390
+ * #### `insertBefore(parentInstance, child, beforeChild)`
391
+ *
360
392
  * This method should mutate the `parentInstance` and place the `child` before `beforeChild` in the list of
361
393
  * its children. For example, in the DOM this would translate to a `parentInstance.insertBefore(child, beforeChild)` call.
362
394
  *
@@ -365,12 +397,16 @@ var hostConfig = {
365
397
  */
366
398
  insertBefore,
367
399
  /**
400
+ * #### `insertInContainerBefore(container, child, beforeChild)
401
+ *
368
402
  * Same as `insertBefore`, but for when a node is attached to the root container. This is useful if attaching
369
403
  * to the root has a slightly different implementation, or if the root container nodes are of a different type
370
404
  * than the rest of the tree.
371
405
  */
372
406
  insertInContainerBefore: insertBefore,
373
407
  /**
408
+ * #### `removeChild(parentInstance, child)`
409
+ *
374
410
  * This method should mutate the `parentInstance` to remove the `child` from the list of its children.
375
411
  *
376
412
  * React will only call it for the top-level node that is being removed. It is expected that garbage collection
@@ -378,12 +414,16 @@ var hostConfig = {
378
414
  */
379
415
  removeChild,
380
416
  /**
417
+ * #### `removeChildFromContainer(container, child)`
418
+ *
381
419
  * Same as `removeChild`, but for when a node is detached from the root container. This is useful if attaching
382
420
  * to the root has a slightly different implementation, or if the root container nodes are of a different type
383
421
  * than the rest of the tree.
384
422
  */
385
423
  removeChildFromContainer: removeChild,
386
424
  /**
425
+ * #### `resetTextContent(instance)`
426
+ *
387
427
  * If you returned `true` from `shouldSetTextContent` for the previous props, but returned `false` from
388
428
  * `shouldSetTextContent` for the next props, React will call this method so that you can clear the text
389
429
  * content you were managing manually. For example, in the DOM you could set `node.textContent = ''`.
@@ -393,6 +433,8 @@ var hostConfig = {
393
433
  resetTextContent(_instance) {
394
434
  },
395
435
  /**
436
+ * #### `commitTextUpdate(textInstance, prevText, nextText)`
437
+ *
396
438
  * This method should mutate the `textInstance` and update its text content to `nextText`.
397
439
  *
398
440
  * Here, `textInstance` is a node created by `createTextInstance`.
@@ -401,6 +443,8 @@ var hostConfig = {
401
443
  textInstance.text = newText;
402
444
  },
403
445
  /**
446
+ * #### `commitMount(instance, type, props, internalHandle)`
447
+ *
404
448
  * This method is only called if you returned `true` from `finalizeInitialChildren` for this instance.
405
449
  *
406
450
  * It lets you do some additional work after the node is actually attached to the tree on the screen for
@@ -420,6 +464,8 @@ var hostConfig = {
420
464
  commitMount(_instance, _type, _props, _internalHandle) {
421
465
  },
422
466
  /**
467
+ * #### `commitUpdate(instance, type, prevProps, nextProps, internalHandle)`
468
+ *
423
469
  * This method should mutate the `instance` according to the set of changes in `updatePayload`. Here, `updatePayload`
424
470
  * is the object that you've returned from `prepareUpdate` and has an arbitrary structure that makes sense for your
425
471
  * renderer. For example, the DOM renderer returns an update payload like `[prop1, value1, prop2, value2, ...]` from
@@ -430,12 +476,15 @@ var hostConfig = {
430
476
  * be aware that it may change significantly between versions. You're taking on additional maintenance risk by
431
477
  * reading from it, and giving up all guarantees if you write something to it.
432
478
  */
433
- commitUpdate(instance, _updatePayload, type, _prevProps, nextProps, internalHandle) {
479
+ // @ts-expect-error types are not updated
480
+ commitUpdate(instance, type, _prevProps, nextProps, internalHandle) {
434
481
  instance.type = type;
435
482
  instance.props = nextProps;
436
483
  instance.internalHandle = internalHandle;
437
484
  },
438
485
  /**
486
+ * #### `hideInstance(instance)`
487
+ *
439
488
  * This method should make the `instance` invisible without removing it from the tree. For example, it can apply
440
489
  * visual styling to hide it. It is used by Suspense to hide the tree while the fallback is visible.
441
490
  */
@@ -443,24 +492,32 @@ var hostConfig = {
443
492
  instance.isHidden = true;
444
493
  },
445
494
  /**
495
+ * #### `hideTextInstance(textInstance)`
496
+ *
446
497
  * Same as `hideInstance`, but for nodes created by `createTextInstance`.
447
498
  */
448
499
  hideTextInstance(textInstance) {
449
500
  textInstance.isHidden = true;
450
501
  },
451
502
  /**
503
+ * #### `unhideInstance(instance, props)`
504
+ *
452
505
  * This method should make the `instance` visible, undoing what `hideInstance` did.
453
506
  */
454
507
  unhideInstance(instance, _props) {
455
508
  instance.isHidden = false;
456
509
  },
457
510
  /**
511
+ * #### `unhideTextInstance(textInstance, text)`
512
+ *
458
513
  * Same as `unhideInstance`, but for nodes created by `createTextInstance`.
459
514
  */
460
515
  unhideTextInstance(textInstance, _text) {
461
516
  textInstance.isHidden = false;
462
517
  },
463
518
  /**
519
+ * #### `clearContainer(container)`
520
+ *
464
521
  * This method should mutate the `container` root node and remove all children from it.
465
522
  */
466
523
  clearContainer(container) {
@@ -469,6 +526,53 @@ var hostConfig = {
469
526
  });
470
527
  container.children.splice(0);
471
528
  },
529
+ /**
530
+ * #### `maySuspendCommit(type, props)`
531
+ *
532
+ * This method is called during render to determine if the Host Component type and props require
533
+ * some kind of loading process to complete before committing an update.
534
+ */
535
+ maySuspendCommit(_type, _props) {
536
+ return false;
537
+ },
538
+ /**
539
+ * #### `preloadInstance(type, props)`
540
+ *
541
+ * This method may be called during render if the Host Component type and props might suspend a commit.
542
+ * It can be used to initiate any work that might shorten the duration of a suspended commit.
543
+ */
544
+ preloadInstance(_type, _props) {
545
+ return true;
546
+ },
547
+ /**
548
+ * #### `startSuspendingCommit()`
549
+ *
550
+ * This method is called just before the commit phase. Use it to set up any necessary state while any Host
551
+ * Components that might suspend this commit are evaluated to determine if the commit must be suspended.
552
+ */
553
+ startSuspendingCommit() {
554
+ },
555
+ /**
556
+ * #### `suspendInstance(type, props)`
557
+ *
558
+ * This method is called after `startSuspendingCommit` for each Host Component that indicated it might
559
+ * suspend a commit.
560
+ */
561
+ suspendInstance() {
562
+ },
563
+ /**
564
+ * #### `waitForCommitToBeReady()`
565
+ *
566
+ * This method is called after all `suspendInstance` calls are complete.
567
+ *
568
+ * Return `null` if the commit can happen immediately.
569
+ * Return `(initiateCommit: Function) => Function` if the commit must be suspended. The argument to this
570
+ * callback will initiate the commit when called. The return value is a cancellation function that the
571
+ * Reconciler can use to abort the commit.
572
+ */
573
+ waitForCommitToBeReady() {
574
+ return null;
575
+ },
472
576
  // -------------------
473
577
  // Hydration Methods
474
578
  // (optional)
@@ -479,7 +583,12 @@ var hostConfig = {
479
583
  // the "Hydration" section [listed in this file](https://github.com/facebook/react/blob/master/packages/react-reconciler/src/forks/ReactFiberHostConfig.custom.js).
480
584
  // File an issue if you need help.
481
585
  // -------------------
482
- supportsHydration: false
586
+ supportsHydration: false,
587
+ NotPendingTransition: null,
588
+ resetFormInstance(_form) {
589
+ },
590
+ requestPostPaintCallback(_callback) {
591
+ }
483
592
  };
484
593
  var TestReconciler = ReactReconciler(hostConfig);
485
594
  function appendChild(parentInstance, child) {
@@ -644,14 +753,16 @@ function createRenderer(options) {
644
753
  tag: "CONTAINER",
645
754
  children: [],
646
755
  parent: null,
647
- textComponents: options == null ? void 0 : options.textComponents,
648
- createNodeMock: (_a = options == null ? void 0 : options.createNodeMock) != null ? _a : () => ({})
756
+ config: {
757
+ textComponents: options == null ? void 0 : options.textComponents,
758
+ createNodeMock: (_a = options == null ? void 0 : options.createNodeMock) != null ? _a : () => ({})
759
+ }
649
760
  };
650
761
  let containerFiber = TestReconciler.createContainer(
651
762
  container,
652
763
  (options == null ? void 0 : options.isConcurrent) ? ConcurrentRoot : LegacyRoot,
653
764
  null,
654
- // no hydration callback
765
+ // hydration callbacks
655
766
  false,
656
767
  // isStrictMode
657
768
  null,
@@ -660,6 +771,13 @@ function createRenderer(options) {
660
771
  // identifierPrefix
661
772
  () => {
662
773
  },
774
+ // onUncaughtError
775
+ () => {
776
+ },
777
+ // onCaughtError
778
+ // @ts-expect-error missing types
779
+ () => {
780
+ },
663
781
  // onRecoverableError
664
782
  null
665
783
  // transitionCallbacks