'use strict';
let React;
let ReactFabric;
let ReactNativePrivateInterface;
let fabricUIManager;
let createReactNativeComponentClass;
let StrictMode;
let act;
let assertConsoleErrorDev;
const DISPATCH_COMMAND_REQUIRES_HOST_COMPONENT =
"dispatchCommand was called with a ref that isn't a " +
'native component. Use React.forwardRef to get access to the underlying native component';
const SEND_ACCESSIBILITY_EVENT_REQUIRES_HOST_COMPONENT =
"sendAccessibilityEvent was called with a ref that isn't a " +
'native component. Use React.forwardRef to get access to the underlying native component';
describe('ReactFabric', () => {
beforeEach(() => {
jest.resetModules();
require('react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager');
React = require('react');
StrictMode = React.StrictMode;
ReactFabric = require('react-native-renderer/fabric');
ReactNativePrivateInterface = require('react-native/react-private-interface');
fabricUIManager = ReactNativePrivateInterface.fabricUIManager;
createReactNativeComponentClass =
require('react-native/react-private-interface')
.ReactNativeViewConfigRegistry.register;
({act, assertConsoleErrorDev} = require('internal-test-utils'));
});
it('should be able to create and render a native component', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
await act(() => {
ReactFabric.render(<View foo="test" />, 1, null, true);
});
expect(fabricUIManager.createNode).toHaveBeenCalled();
expect(fabricUIManager.appendChild).not.toHaveBeenCalled();
expect(fabricUIManager.completeRoot).toHaveBeenCalled();
});
it('should be able to create and update a native component', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
const firstNode = {};
fabricUIManager.createNode.mockReturnValue(firstNode);
await act(() => {
ReactFabric.render(<View foo="foo" />, 11, null, true);
});
expect(fabricUIManager.createNode).toHaveBeenCalledTimes(1);
await act(() => {
ReactFabric.render(<View foo="bar" />, 11, null, true);
});
expect(fabricUIManager.createNode).toHaveBeenCalledTimes(1);
expect(fabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledTimes(1);
expect(fabricUIManager.cloneNodeWithNewProps.mock.calls[0][0]).toBe(
firstNode,
);
expect(fabricUIManager.cloneNodeWithNewProps.mock.calls[0][1]).toEqual({
foo: 'bar',
});
});
it('should not call FabricUIManager.cloneNode after render for properties that have not changed', async () => {
const Text = createReactNativeComponentClass('RCTText', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTText',
}));
await act(() => {
ReactFabric.render(<Text foo="a">1</Text>, 11, null, true);
});
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).not.toHaveBeenCalled();
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
await act(() => {
ReactFabric.render(<Text foo="a">1</Text>, 11, null, true);
});
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).not.toHaveBeenCalled();
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
await act(() => {
ReactFabric.render(<Text foo="b">1</Text>, 11, null, true);
});
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledTimes(1);
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
await act(() => {
ReactFabric.render(<Text foo="b">2</Text>, 11, null, true);
});
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).toHaveBeenCalledTimes(1);
expect(fabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledTimes(1);
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
await act(() => {
ReactFabric.render(<Text foo="c">3</Text>, 11, null, true);
});
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).toHaveBeenCalledTimes(1);
expect(fabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledTimes(1);
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).toHaveBeenCalledTimes(1);
});
it('should only pass props diffs to FabricUIManager.cloneNode', async () => {
const Text = createReactNativeComponentClass('RCTText', () => ({
validAttributes: {foo: true, bar: true},
uiViewClassName: 'RCTText',
}));
await act(() => {
ReactFabric.render(
<Text foo="a" bar="a">
1
</Text>,
11,
null,
true,
);
});
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).not.toHaveBeenCalled();
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
jest
.spyOn(ReactNativePrivateInterface, 'diffAttributePayloads')
.mockReturnValue({bar: 'b'});
await act(() => {
ReactFabric.render(
<Text foo="a" bar="b">
1
</Text>,
11,
null,
true,
);
});
expect(fabricUIManager.cloneNodeWithNewProps.mock.calls[0][1]).toEqual({
bar: 'b',
});
expect(fabricUIManager.__dumpHierarchyForJestTestsOnly()).toBe(`11
RCTText {"foo":"a","bar":"b"}
RCTRawText {"text":"1"}`);
jest
.spyOn(ReactNativePrivateInterface, 'diffAttributePayloads')
.mockReturnValue({foo: 'b'});
await act(() => {
ReactFabric.render(
<Text foo="b" bar="b">
2
</Text>,
11,
null,
true,
);
});
const argIndex = gate(flags => flags.passChildrenWhenCloningPersistedNodes)
? 2
: 1;
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps.mock.calls[0][argIndex],
).toEqual({
foo: 'b',
});
expect(fabricUIManager.__dumpHierarchyForJestTestsOnly()).toBe(`11
RCTText {"foo":"b","bar":"b"}
RCTRawText {"text":"2"}`);
});
it('should not clone nodes without children when updating props', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
const Component = ({foo}) => (
<View>
<View foo={foo} />
</View>
);
await act(() =>
ReactFabric.render(<Component foo={true} />, 11, null, true),
);
expect(fabricUIManager.completeRoot).toHaveBeenCalled();
jest.clearAllMocks();
await act(() =>
ReactFabric.render(<Component foo={false} />, 11, null, true),
);
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledTimes(1);
expect(fabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledWith(
expect.anything(),
{foo: false},
);
expect(fabricUIManager.cloneNodeWithNewChildren).toHaveBeenCalledTimes(1);
if (gate(flags => flags.passChildrenWhenCloningPersistedNodes)) {
expect(fabricUIManager.cloneNodeWithNewChildren).toHaveBeenCalledWith(
expect.anything(),
[expect.objectContaining({props: {foo: false}})],
);
expect(fabricUIManager.appendChild).not.toHaveBeenCalled();
} else {
expect(fabricUIManager.cloneNodeWithNewChildren).toHaveBeenCalledWith(
expect.anything(),
);
expect(fabricUIManager.appendChild).toHaveBeenCalledTimes(1);
}
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
expect(fabricUIManager.completeRoot).toHaveBeenCalled();
});
it('should not clone nodes when layout effects are used', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
const ComponentWithEffect = () => {
React.useLayoutEffect(() => {});
return null;
};
await act(() =>
ReactFabric.render(
<View>
<ComponentWithEffect />
</View>,
11,
null,
true,
),
);
expect(fabricUIManager.completeRoot).toHaveBeenCalled();
jest.clearAllMocks();
await act(() =>
ReactFabric.render(
<View>
<ComponentWithEffect />
</View>,
11,
null,
true,
),
);
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).not.toHaveBeenCalled();
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
expect(fabricUIManager.completeRoot).not.toHaveBeenCalled();
});
it('should not clone nodes when insertion effects are used', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
const ComponentWithRef = () => {
React.useInsertionEffect(() => {});
return null;
};
await act(() =>
ReactFabric.render(
<View>
<ComponentWithRef />
</View>,
11,
null,
true,
),
);
expect(fabricUIManager.completeRoot).toHaveBeenCalled();
jest.clearAllMocks();
await act(() =>
ReactFabric.render(
<View>
<ComponentWithRef />
</View>,
11,
null,
true,
),
);
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).not.toHaveBeenCalled();
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
expect(fabricUIManager.completeRoot).not.toHaveBeenCalled();
});
it('should not clone nodes when useImperativeHandle is used', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
const ComponentWithImperativeHandle = props => {
React.useImperativeHandle(props.ref, () => ({greet: () => 'hello'}));
return null;
};
const ref = React.createRef();
await act(() =>
ReactFabric.render(
<View>
<ComponentWithImperativeHandle ref={ref} />
</View>,
11,
null,
true,
),
);
expect(fabricUIManager.completeRoot).toHaveBeenCalled();
expect(ref.current.greet()).toBe('hello');
jest.clearAllMocks();
await act(() =>
ReactFabric.render(
<View>
<ComponentWithImperativeHandle ref={ref} />
</View>,
11,
null,
true,
),
);
expect(fabricUIManager.cloneNode).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewChildren).not.toHaveBeenCalled();
expect(fabricUIManager.cloneNodeWithNewProps).not.toHaveBeenCalled();
expect(
fabricUIManager.cloneNodeWithNewChildrenAndProps,
).not.toHaveBeenCalled();
expect(fabricUIManager.completeRoot).not.toHaveBeenCalled();
expect(ref.current.greet()).toBe('hello');
});
it('should call dispatchCommand for native refs', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
fabricUIManager.dispatchCommand.mockClear();
let viewRef;
await act(() => {
ReactFabric.render(
<View
ref={ref => {
viewRef = ref;
}}
/>,
11,
null,
true,
);
});
expect(fabricUIManager.dispatchCommand).not.toHaveBeenCalled();
ReactFabric.dispatchCommand(viewRef, 'updateCommand', [10, 20]);
expect(fabricUIManager.dispatchCommand).toHaveBeenCalledTimes(1);
expect(fabricUIManager.dispatchCommand).toHaveBeenCalledWith(
expect.any(Object),
'updateCommand',
[10, 20],
);
});
it('should warn and no-op if calling dispatchCommand on non native refs', async () => {
class BasicClass extends React.Component {
render() {
return <React.Fragment />;
}
}
fabricUIManager.dispatchCommand.mockReset();
let viewRef;
await act(() => {
ReactFabric.render(
<BasicClass
ref={ref => {
viewRef = ref;
}}
/>,
11,
null,
true,
);
});
expect(fabricUIManager.dispatchCommand).not.toHaveBeenCalled();
ReactFabric.dispatchCommand(viewRef, 'updateCommand', [10, 20]);
assertConsoleErrorDev([DISPATCH_COMMAND_REQUIRES_HOST_COMPONENT]);
expect(fabricUIManager.dispatchCommand).not.toHaveBeenCalled();
});
it('should call sendAccessibilityEvent for native refs', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
fabricUIManager.sendAccessibilityEvent.mockClear();
let viewRef;
await act(() => {
ReactFabric.render(
<View
ref={ref => {
viewRef = ref;
}}
/>,
11,
null,
true,
);
});
expect(fabricUIManager.sendAccessibilityEvent).not.toHaveBeenCalled();
ReactFabric.sendAccessibilityEvent(viewRef, 'focus');
expect(fabricUIManager.sendAccessibilityEvent).toHaveBeenCalledTimes(1);
expect(fabricUIManager.sendAccessibilityEvent).toHaveBeenCalledWith(
expect.any(Object),
'focus',
);
});
it('should warn and no-op if calling sendAccessibilityEvent on non native refs', async () => {
class BasicClass extends React.Component {
render() {
return <React.Fragment />;
}
}
fabricUIManager.sendAccessibilityEvent.mockReset();
let viewRef;
await act(() => {
ReactFabric.render(
<BasicClass
ref={ref => {
viewRef = ref;
}}
/>,
11,
null,
true,
);
});
expect(fabricUIManager.sendAccessibilityEvent).not.toHaveBeenCalled();
ReactFabric.sendAccessibilityEvent(viewRef, 'eventTypeName');
assertConsoleErrorDev([SEND_ACCESSIBILITY_EVENT_REQUIRES_HOST_COMPONENT]);
expect(fabricUIManager.sendAccessibilityEvent).not.toHaveBeenCalled();
});
it('calls the callback with the correct instance and returns null', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
let a;
let b;
let c;
await act(() => {
c = ReactFabric.render(
<View foo="foo" ref={v => (a = v)} />,
11,
function () {
b = this;
},
true,
);
});
expect(a).toBeTruthy();
expect(a).toBe(b);
expect(c).toBe(null);
});
it('returns the instance in legacy mode and calls the callback with it', () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
let a;
let b;
const c = ReactFabric.render(
<View
foo="foo"
ref={v => {
a = v;
}}
/>,
11,
function () {
b = this;
},
);
expect(a).toBeTruthy();
expect(a).toBe(b);
expect(a).toBe(c);
});
it('renders and reorders children', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {title: true},
uiViewClassName: 'RCTView',
}));
class Component extends React.Component {
render() {
const chars = this.props.chars.split('');
return (
<View>
{chars.map(text => (
<View key={text} title={text} />
))}
</View>
);
}
}
const before = 'abcdefghijklmnopqrst';
const after = 'mxhpgwfralkeoivcstzy';
await act(() => {
ReactFabric.render(<Component chars={before} />, 11, null, true);
});
expect(fabricUIManager.__dumpHierarchyForJestTestsOnly()).toBe(`11
RCTView {}
RCTView {"title":"a"}
RCTView {"title":"b"}
RCTView {"title":"c"}
RCTView {"title":"d"}
RCTView {"title":"e"}
RCTView {"title":"f"}
RCTView {"title":"g"}
RCTView {"title":"h"}
RCTView {"title":"i"}
RCTView {"title":"j"}
RCTView {"title":"k"}
RCTView {"title":"l"}
RCTView {"title":"m"}
RCTView {"title":"n"}
RCTView {"title":"o"}
RCTView {"title":"p"}
RCTView {"title":"q"}
RCTView {"title":"r"}
RCTView {"title":"s"}
RCTView {"title":"t"}`);
await act(() => {
ReactFabric.render(<Component chars={after} />, 11, null, true);
});
expect(fabricUIManager.__dumpHierarchyForJestTestsOnly()).toBe(`11
RCTView {}
RCTView {"title":"m"}
RCTView {"title":"x"}
RCTView {"title":"h"}
RCTView {"title":"p"}
RCTView {"title":"g"}
RCTView {"title":"w"}
RCTView {"title":"f"}
RCTView {"title":"r"}
RCTView {"title":"a"}
RCTView {"title":"l"}
RCTView {"title":"k"}
RCTView {"title":"e"}
RCTView {"title":"o"}
RCTView {"title":"i"}
RCTView {"title":"v"}
RCTView {"title":"c"}
RCTView {"title":"s"}
RCTView {"title":"t"}
RCTView {"title":"z"}
RCTView {"title":"y"}`);
});
it('recreates host parents even if only children changed', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {title: true},
uiViewClassName: 'RCTView',
}));
const before = 'abcdefghijklmnopqrst';
const after = 'mxhpgwfralkeoivcstzy';
class Component extends React.Component {
state = {
chars: before,
};
render() {
const chars = this.state.chars.split('');
return (
<View>
{chars.map(text => (
<View key={text} title={text} />
))}
</View>
);
}
}
const ref = React.createRef();
await act(() => {
ReactFabric.render(
<View>
<Component ref={ref} />
</View>,
11,
null,
true,
);
});
expect(fabricUIManager.__dumpHierarchyForJestTestsOnly()).toBe(
`11
RCTView {}
RCTView {}
RCTView {"title":"a"}
RCTView {"title":"b"}
RCTView {"title":"c"}
RCTView {"title":"d"}
RCTView {"title":"e"}
RCTView {"title":"f"}
RCTView {"title":"g"}
RCTView {"title":"h"}
RCTView {"title":"i"}
RCTView {"title":"j"}
RCTView {"title":"k"}
RCTView {"title":"l"}
RCTView {"title":"m"}
RCTView {"title":"n"}
RCTView {"title":"o"}
RCTView {"title":"p"}
RCTView {"title":"q"}
RCTView {"title":"r"}
RCTView {"title":"s"}
RCTView {"title":"t"}`,
);
await act(() => {
ref.current.setState({
chars: after,
});
});
expect(fabricUIManager.__dumpHierarchyForJestTestsOnly()).toBe(`11
RCTView {}
RCTView {}
RCTView {"title":"m"}
RCTView {"title":"x"}
RCTView {"title":"h"}
RCTView {"title":"p"}
RCTView {"title":"g"}
RCTView {"title":"w"}
RCTView {"title":"f"}
RCTView {"title":"r"}
RCTView {"title":"a"}
RCTView {"title":"l"}
RCTView {"title":"k"}
RCTView {"title":"e"}
RCTView {"title":"o"}
RCTView {"title":"i"}
RCTView {"title":"v"}
RCTView {"title":"c"}
RCTView {"title":"s"}
RCTView {"title":"t"}
RCTView {"title":"z"}
RCTView {"title":"y"}`);
});
it('calls setState with no arguments', async () => {
let mockArgs;
class Component extends React.Component {
componentDidMount() {
this.setState({}, (...args) => (mockArgs = args));
}
render() {
return false;
}
}
await act(() => {
ReactFabric.render(<Component />, 11, null, true);
});
expect(mockArgs.length).toEqual(0);
});
it('should call complete after inserting children', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
const snapshots = [];
fabricUIManager.completeRoot.mockImplementation(
function (rootTag, newChildSet) {
snapshots.push(
fabricUIManager.__dumpChildSetForJestTestsOnly(newChildSet),
);
},
);
await act(() => {
ReactFabric.render(
<View foo="a">
<View foo="b" />
</View>,
22,
null,
true,
);
});
expect(snapshots).toEqual([
`RCTView {"foo":"a"}
RCTView {"foo":"b"}`,
]);
});
it('should not throw when <View> is used inside of a <Text> ancestor', async () => {
const Image = createReactNativeComponentClass('RCTImage', () => ({
validAttributes: {},
uiViewClassName: 'RCTImage',
}));
const Text = createReactNativeComponentClass('RCTText', () => ({
validAttributes: {},
uiViewClassName: 'RCTText',
}));
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {},
uiViewClassName: 'RCTView',
}));
await act(() => {
ReactFabric.render(
<Text>
<View />
</Text>,
11,
null,
true,
);
});
await act(() => {
ReactFabric.render(
<Text>
<Image />
</Text>,
11,
null,
true,
);
});
});
it('should console error for text not inside of a <Text> ancestor', async () => {
const ScrollView = createReactNativeComponentClass('RCTScrollView', () => ({
validAttributes: {},
uiViewClassName: 'RCTScrollView',
}));
const Text = createReactNativeComponentClass('RCTText', () => ({
validAttributes: {},
uiViewClassName: 'RCTText',
}));
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {},
uiViewClassName: 'RCTView',
}));
await act(() => {
ReactFabric.render(<View>this should warn</View>, 11, null, true);
});
assertConsoleErrorDev([
'Text strings must be rendered within a <Text> component.\n' +
' in RCTView (at **)',
]);
await act(() => {
ReactFabric.render(
<Text>
<ScrollView>hi hello hi</ScrollView>
</Text>,
11,
null,
true,
);
});
assertConsoleErrorDev([
'Text strings must be rendered within a <Text> component.\n' +
' in RCTScrollView (at **)',
]);
});
it('should not throw for text inside of an indirect <Text> ancestor', async () => {
const Text = createReactNativeComponentClass('RCTText', () => ({
validAttributes: {},
uiViewClassName: 'RCTText',
}));
const Indirection = () => 'Hi';
await act(() => {
ReactFabric.render(
<Text>
<Indirection />
</Text>,
11,
null,
true,
);
});
});
it('dispatches native events with a null target after the target unmounts', async () => {
jest.spyOn(ReactNativePrivateInterface, 'dispatchNativeEvent');
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {},
uiViewClassName: 'RCTView',
}));
await act(() => {
ReactFabric.render(<View />, 11, null, true);
});
const [, , , , instanceHandle] = fabricUIManager.createNode.mock.calls[0];
const [dispatchEvent] = fabricUIManager.registerEventHandler.mock.calls[0];
await act(() => {
ReactFabric.render(null, 11, null, true);
});
const touchEvent = {
touches: [],
changedTouches: [],
};
dispatchEvent(instanceHandle, 'topTouchEnd', touchEvent);
expect(
ReactNativePrivateInterface.dispatchNativeEvent,
).toHaveBeenCalledWith(null, 'topTouchEnd', touchEvent);
});
it('findHostInstance_DEPRECATED should warn if used to find a host component inside StrictMode', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
let parent = undefined;
let child = undefined;
class ContainsStrictModeChild extends React.Component {
render() {
return (
<StrictMode>
<View ref={n => (child = n)} />
</StrictMode>
);
}
}
await act(() => {
ReactFabric.render(
<ContainsStrictModeChild ref={n => (parent = n)} />,
11,
null,
true,
);
});
const match = ReactFabric.findHostInstance_DEPRECATED(parent);
assertConsoleErrorDev([
'findHostInstance_DEPRECATED is deprecated in StrictMode. ' +
'findHostInstance_DEPRECATED was passed an instance of ContainsStrictModeChild which renders StrictMode children. ' +
'Instead, add a ref directly to the element you want to reference. ' +
'Learn more about using refs safely here: ' +
'https://react.dev/link/strict-mode-find-node' +
'\n in RCTView (at **)' +
'\n in ContainsStrictModeChild (at **)',
]);
expect(match).toBe(child);
});
it('findHostInstance_DEPRECATED should warn if passed a component that is inside StrictMode', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
let parent = undefined;
let child = undefined;
class IsInStrictMode extends React.Component {
render() {
return <View ref={n => (child = n)} />;
}
}
await act(() => {
ReactFabric.render(
<StrictMode>
<IsInStrictMode ref={n => (parent = n)} />
</StrictMode>,
11,
null,
true,
);
});
const match = ReactFabric.findHostInstance_DEPRECATED(parent);
assertConsoleErrorDev([
'findHostInstance_DEPRECATED is deprecated in StrictMode. ' +
'findHostInstance_DEPRECATED was passed an instance of IsInStrictMode which is inside StrictMode. ' +
'Instead, add a ref directly to the element you want to reference. ' +
'Learn more about using refs safely here: ' +
'https://react.dev/link/strict-mode-find-node' +
'\n in RCTView (at **)' +
'\n in IsInStrictMode (at **)',
]);
expect(match).toBe(child);
});
it('findNodeHandle should warn if used to find a host component inside StrictMode', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
let parent = undefined;
let child = undefined;
class ContainsStrictModeChild extends React.Component {
render() {
return (
<StrictMode>
<View ref={n => (child = n)} />
</StrictMode>
);
}
}
await act(() => {
ReactFabric.render(
<ContainsStrictModeChild ref={n => (parent = n)} />,
11,
null,
true,
);
});
const match = ReactFabric.findNodeHandle(parent);
assertConsoleErrorDev([
'findNodeHandle is deprecated in StrictMode. ' +
'findNodeHandle was passed an instance of ContainsStrictModeChild which renders StrictMode children. ' +
'Instead, add a ref directly to the element you want to reference. ' +
'Learn more about using refs safely here: ' +
'https://react.dev/link/strict-mode-find-node' +
'\n in RCTView (at **)' +
'\n in ContainsStrictModeChild (at **)',
]);
expect(match).toBe(
ReactNativePrivateInterface.getNativeTagFromPublicInstance(child),
);
});
it('findNodeHandle should warn if passed a component that is inside StrictMode', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
let parent = undefined;
let child = undefined;
class IsInStrictMode extends React.Component {
render() {
return <View ref={n => (child = n)} />;
}
}
await act(() => {
ReactFabric.render(
<StrictMode>
<IsInStrictMode ref={n => (parent = n)} />
</StrictMode>,
11,
null,
true,
);
});
const match = ReactFabric.findNodeHandle(parent);
assertConsoleErrorDev([
'findNodeHandle is deprecated in StrictMode. ' +
'findNodeHandle was passed an instance of IsInStrictMode which is inside StrictMode. ' +
'Instead, add a ref directly to the element you want to reference. ' +
'Learn more about using refs safely here: ' +
'https://react.dev/link/strict-mode-find-node' +
'\n in RCTView (at **)' +
'\n in IsInStrictMode (at **)',
]);
expect(match).toBe(
ReactNativePrivateInterface.getNativeTagFromPublicInstance(child),
);
});
it('findNodeHandle errors when called from render', async () => {
class TestComponent extends React.Component {
render() {
ReactFabric.findNodeHandle(this);
return null;
}
}
await act(() => {
ReactFabric.render(<TestComponent />, 11, null, true);
});
assertConsoleErrorDev([
'TestComponent is accessing findNodeHandle inside its render(). ' +
'render() should be a pure function of props and state. It should ' +
'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.\n' +
' in TestComponent (at **)',
]);
});
it("findNodeHandle doesn't error when called outside render", async () => {
class TestComponent extends React.Component {
render() {
return null;
}
componentDidMount() {
ReactFabric.findNodeHandle(this);
}
}
await act(() => {
ReactFabric.render(<TestComponent />, 11, null, true);
});
});
it('should no-op if calling sendAccessibilityEvent on unmounted refs', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
fabricUIManager.sendAccessibilityEvent.mockReset();
let viewRef;
await act(() => {
ReactFabric.render(
<View
ref={ref => {
viewRef = ref;
}}
/>,
11,
null,
true,
);
});
const dangerouslyRetainedViewRef = viewRef;
await act(() => {
ReactFabric.stopSurface(11);
});
ReactFabric.sendAccessibilityEvent(
dangerouslyRetainedViewRef,
'eventTypeName',
);
assertConsoleErrorDev([
"sendAccessibilityEvent was called with a ref that isn't a " +
'native component. Use React.forwardRef to get access to the underlying native component',
]);
expect(fabricUIManager.sendAccessibilityEvent).not.toHaveBeenCalled();
});
it('getNodeFromInternalInstanceHandle should return the correct shadow node', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
await act(() => {
ReactFabric.render(<View foo="test" />, 1, null, true);
});
const internalInstanceHandle = fabricUIManager.createNode.mock.calls[0][4];
expect(internalInstanceHandle).toEqual(expect.any(Object));
const expectedShadowNode = fabricUIManager.createNode.mock.results[0].value;
expect(expectedShadowNode).toEqual(expect.any(Object));
const node = ReactFabric.getNodeFromInternalInstanceHandle(
internalInstanceHandle,
);
expect(node).toBe(expectedShadowNode);
});
it('getPublicInstanceFromInternalInstanceHandle should provide public instances for HostComponent', async () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));
let viewRef;
await act(() => {
ReactFabric.render(
<View
foo="test"
ref={ref => {
viewRef = ref;
}}
/>,
1,
null,
true,
);
});
const internalInstanceHandle = fabricUIManager.createNode.mock.calls[0][4];
expect(internalInstanceHandle).toEqual(expect.any(Object));
const publicInstance =
ReactFabric.getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle,
);
expect(publicInstance).toBe(viewRef);
await act(() => {
ReactFabric.render(null, 1, null, true);
});
const publicInstanceAfterUnmount =
ReactFabric.getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle,
);
expect(publicInstanceAfterUnmount).toBe(null);
});
it('getPublicInstanceFromInternalInstanceHandle should provide public instances for HostText', async () => {
jest.spyOn(ReactNativePrivateInterface, 'createPublicTextInstance');
const RCTText = createReactNativeComponentClass('RCTText', () => ({
validAttributes: {},
uiViewClassName: 'RCTText',
}));
await act(() => {
ReactFabric.render(<RCTText>Text content</RCTText>, 1, null, true);
});
const internalInstanceHandle = fabricUIManager.createNode.mock.calls[0][4];
expect(internalInstanceHandle).toEqual(expect.any(Object));
expect(
ReactNativePrivateInterface.createPublicTextInstance,
).not.toHaveBeenCalled();
const publicInstance =
ReactFabric.getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle,
);
expect(
ReactNativePrivateInterface.createPublicTextInstance,
).toHaveBeenCalledTimes(1);
expect(
ReactNativePrivateInterface.createPublicTextInstance,
).toHaveBeenCalledWith(internalInstanceHandle);
const expectedPublicInstance =
ReactNativePrivateInterface.createPublicTextInstance.mock.results[0]
.value;
expect(publicInstance).toBe(expectedPublicInstance);
await act(() => {
ReactFabric.render(null, 1, null, true);
});
const publicInstanceAfterUnmount =
ReactFabric.getPublicInstanceFromInternalInstanceHandle(
internalInstanceHandle,
);
expect(publicInstanceAfterUnmount).toBe(null);
});
});