Skip to content

Commit 00122d9

Browse files
refactor: remove createNodeMock option (#45)
1 parent f5b3730 commit 00122d9

5 files changed

Lines changed: 17 additions & 303 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ Configuration options for the test renderer. Many of these options correspond to
9797
| ------------------------------ | ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
9898
| `textComponentTypes` | `string[]` | Types of host components that are allowed to contain text nodes. Trying to render text outside of these components will throw an error. Useful for simulating React Native's text rendering rules. |
9999
| `publicTextComponentTypes` | `string[]` | Host component types to display to users in error messages when they try to render text outside of `textComponentTypes`. Defaults to `textComponentTypes` if not provided. |
100-
| `createNodeMock` | `(element: ReactElement) => object` | Function to create mock objects for refs. Called once per element that has a ref. Defaults to returning an empty object. |
101100
| `transformHiddenInstanceProps` | `({ props, type }: { props: Record<string, unknown>; type: string }) => Record<string, unknown>` | Transforms host instance props when React marks an instance as hidden (for example, while Suspense fallback is shown). Return a new props object instead of mutating the provided one. When provided, hidden instances stay visible in `children` and `toJSON()` output using transformed props. |
102101
| `identifierPrefix` | `string` | A string prefix React uses for IDs generated by `useId()`. Useful to avoid conflicts when using multiple roots. |
103102
| `isStrictMode` | `boolean` | Enable React Strict Mode. When enabled, components render twice and effects run twice in development. |

src/__tests__/create-node-mock.test.tsx

Lines changed: 0 additions & 280 deletions
This file was deleted.

src/__tests__/renderer.test.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { beforeEach, describe, expect, test } from "@jest/globals";
2-
import { createElement } from "react";
2+
import { createElement, createRef, type Ref } from "react";
33

44
import { createRoot } from "../renderer";
5-
import { renderWithAct } from "../test-utils/render";
5+
import type { TestInstance } from "../test-instance";
6+
import { getRootInstance, renderWithAct } from "../test-utils/render";
67

78
beforeEach(() => {
89
global.IS_REACT_ACT_ENVIRONMENT = true;
@@ -20,6 +21,14 @@ test("basic renderer usage", async () => {
2021
`);
2122
});
2223

24+
test("refs receive TestInstance by default", async () => {
25+
const renderer = createRoot();
26+
const ref = createRef<TestInstance | null>();
27+
await renderWithAct(renderer, <div ref={ref as Ref<HTMLDivElement>}>Hello</div>);
28+
const rootInstance = getRootInstance(renderer);
29+
expect(ref.current).toBe(rootInstance);
30+
});
31+
2332
describe("textComponentTypes", () => {
2433
test("single allowed text component", async () => {
2534
const renderer = createRoot({

src/reconciler.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { ReactElement } from "react";
21
import type { Fiber } from "react-reconciler";
32
import ReactReconciler from "react-reconciler";
43
import { DefaultEventPriority, NoEventPriority } from "react-reconciler/constants";
54

65
import { Tag } from "./constants";
76
import { mark, measureEnd, measureStart } from "./performance";
7+
import { TestInstance } from "./test-instance";
88
import { formatComponentList } from "./utils";
99

1010
export type Type = string;
@@ -14,7 +14,6 @@ export type TransformHiddenInstanceProps = (input: { props: Props; type: Type })
1414
type ReconcilerConfig = {
1515
textComponentTypes?: string[];
1616
publicTextComponentTypes?: string[];
17-
createNodeMock: (element: ReactElement) => object;
1817
transformHiddenInstanceProps?: TransformHiddenInstanceProps;
1918
};
2019

@@ -48,7 +47,7 @@ export type TextInstance = {
4847

4948
export type SuspenseInstance = object;
5049
export type HydratableInstance = object;
51-
export type PublicInstance = object | TextInstance;
50+
export type PublicInstance = object | null;
5251
export type UpdatePayload = unknown;
5352
export type ChildSet = unknown;
5453
export type TimeoutHandle = unknown;
@@ -351,20 +350,13 @@ const hostConfig: ReactReconciler.HostConfig<
351350

352351
switch (instance.tag) {
353352
case Tag.Instance: {
354-
const createNodeMock = instance.rootContainer.config.createNodeMock;
355-
const mockNode = createNodeMock({
356-
type: instance.type,
357-
props: instance.props,
358-
key: null,
359-
});
360-
361-
nodeToInstanceMap.set(mockNode, instance);
362-
363-
return mockNode;
353+
const testInstance = TestInstance.fromInstance(instance);
354+
nodeToInstanceMap.set(testInstance, instance);
355+
return testInstance;
364356
}
365357

366358
default:
367-
return instance;
359+
return null;
368360
}
369361
},
370362

src/renderer.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import { TestInstance } from "./test-instance";
1212
// https://github.com/facebook/react/blob/main/packages/react-noop-renderer/src/createReactNoop.js
1313
// https://github.com/facebook/react/blob/main/packages/react-native-renderer/src/ReactFiberConfigFabric.js
1414

15-
const defaultCreateMockNode = () => ({});
16-
1715
const defaultOnUncaughtError = (error: unknown, errorInfo: ErrorInfo) => {
1816
console.error("Uncaught error:", error, errorInfo);
1917
};
@@ -37,9 +35,6 @@ export type RootOptions = {
3735
*/
3836
publicTextComponentTypes?: string[];
3937

40-
/** Function to create mock nodes for refs. */
41-
createNodeMock?: (element: ReactElement) => object;
42-
4338
/**
4439
* Transform props when React marks a host instance as hidden (e.g. during Suspense fallback).
4540
* Receives `{ props, type }` and should return a new props object.
@@ -100,7 +95,6 @@ export function createRoot(options?: RootOptions): Root {
10095
config: {
10196
textComponentTypes: options?.textComponentTypes,
10297
publicTextComponentTypes: options?.publicTextComponentTypes,
103-
createNodeMock: options?.createNodeMock ?? defaultCreateMockNode,
10498
transformHiddenInstanceProps: options?.transformHiddenInstanceProps,
10599
},
106100
};

0 commit comments

Comments
 (0)