-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose component stack from reactTag to React Native renderer (#12549)
This is not safe in general and therefore shouldn't be exposed to anything other than React Native internals. It will also need a different version in Fabric that will not have the reactTag exposed.
- Loading branch information
1 parent
27535e7
commit 7a3416f
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/react-native-renderer/src/__tests__/ReactNativeError-test.internal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
* @jest-environment node | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactNative; | ||
let createReactNativeComponentClass; | ||
let computeComponentStackForErrorReporting; | ||
|
||
function normalizeCodeLocInfo(str) { | ||
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)'); | ||
} | ||
|
||
describe('ReactNativeError', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactNative = require('react-native-renderer'); | ||
createReactNativeComponentClass = require('../createReactNativeComponentClass') | ||
.default; | ||
computeComponentStackForErrorReporting = | ||
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED | ||
.computeComponentStackForErrorReporting; | ||
}); | ||
|
||
it('should be able to extract a component stack from a native view', () => { | ||
const View = createReactNativeComponentClass('View', () => ({ | ||
validAttributes: {foo: true}, | ||
uiViewClassName: 'View', | ||
})); | ||
|
||
const ref = React.createRef(); | ||
|
||
function FunctionalComponent(props) { | ||
return props.children; | ||
} | ||
|
||
class ClassComponent extends React.Component { | ||
render() { | ||
return ( | ||
<FunctionalComponent> | ||
<View foo="test" ref={ref} /> | ||
</FunctionalComponent> | ||
); | ||
} | ||
} | ||
|
||
ReactNative.render(<ClassComponent />, 1); | ||
|
||
let reactTag = ReactNative.findNodeHandle(ref.current); | ||
|
||
let componentStack = normalizeCodeLocInfo( | ||
computeComponentStackForErrorReporting(reactTag), | ||
); | ||
|
||
if (__DEV__) { | ||
expect(componentStack).toBe( | ||
'\n' + | ||
' in View (at **)\n' + | ||
' in FunctionalComponent (at **)\n' + | ||
' in ClassComponent (at **)', | ||
); | ||
} else { | ||
expect(componentStack).toBe( | ||
'\n' + | ||
' in View\n' + | ||
' in FunctionalComponent\n' + | ||
' in ClassComponent', | ||
); | ||
} | ||
}); | ||
}); |