Skip to content

Commit

Permalink
Unit Test for findNodeHandle Error Behavior (facebook#30669)
Browse files Browse the repository at this point in the history
## Summary

As promised on facebook#29627, this
creates a unit test for the `findNodeHandle` error that prevents
developers from calling it within render methods.

## How did you test this change?

```
$ yarn test ReactFabric-test.internal.js
```
  • Loading branch information
yungsters authored Aug 12, 2024
1 parent 0d7c12c commit 23830ea
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,40 @@ describe('ReactFabric', () => {
);
});

it('findNodeHandle errors when called from render', async () => {
class TestComponent extends React.Component {
render() {
ReactFabric.findNodeHandle(this);
return null;
}
}
await expect(async () => {
await act(() => {
ReactFabric.render(<TestComponent />, 11, null, true);
});
}).toErrorDev([
'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.',
]);
});

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},
Expand Down

0 comments on commit 23830ea

Please sign in to comment.