RN: Migrate LogBoxInspector-test.js from Shallow Renderer (#44974)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44974

Migrates this Jest unit test away from using `react-shallow-renderer` because it is no longer recommended.

Changelog:
[Internal]

Reviewed By: robhogan

Differential Revision: D58641098

fbshipit-source-id: be7592b3fb5c4a66879ad734439faceb4ad5cdde
This commit is contained in:
Tim Yung
2024-06-16 10:30:39 -07:00
committed by Facebook GitHub Bot
parent e05cbd9912
commit 7dd4811bea
5 changed files with 125 additions and 78 deletions
@@ -9,40 +9,37 @@
*/
import Keyboard from '../../Components/Keyboard/Keyboard';
import ScrollView from '../../Components/ScrollView/ScrollView';
import View from '../../Components/View/View';
import StyleSheet from '../../StyleSheet/StyleSheet';
import * as LogBoxData from '../Data/LogBoxData';
import LogBoxLog, {type LogLevel} from '../Data/LogBoxLog';
import LogBoxInspectorCodeFrame from './LogBoxInspectorCodeFrame';
import LogBoxInspectorBody from './LogBoxInspectorBody';
import LogBoxInspectorFooter from './LogBoxInspectorFooter';
import LogBoxInspectorHeader from './LogBoxInspectorHeader';
import LogBoxInspectorMessageHeader from './LogBoxInspectorMessageHeader';
import LogBoxInspectorReactFrames from './LogBoxInspectorReactFrames';
import LogBoxInspectorStackFrames from './LogBoxInspectorStackFrames';
import * as LogBoxStyle from './LogBoxStyle';
import * as React from 'react';
import {useEffect} from 'react';
type Props = $ReadOnly<{|
type Props = $ReadOnly<{
onDismiss: () => void,
onChangeSelectedIndex: (index: number) => void,
onMinimize: () => void,
logs: $ReadOnlyArray<LogBoxLog>,
selectedIndex: number,
fatalType?: ?LogLevel,
|}>;
}>;
function LogBoxInspector(props: Props): React.Node {
export default function LogBoxInspector(props: Props): React.Node {
const {logs, selectedIndex} = props;
let log = logs[selectedIndex];
React.useEffect(() => {
useEffect(() => {
if (log) {
LogBoxData.symbolicateLogNow(log);
}
}, [log]);
React.useEffect(() => {
useEffect(() => {
// Optimistically symbolicate the last and next logs.
if (logs.length > 1) {
const selected = selectedIndex;
@@ -54,7 +51,7 @@ function LogBoxInspector(props: Props): React.Node {
}
}, [logs, selectedIndex]);
React.useEffect(() => {
useEffect(() => {
Keyboard.dismiss();
}, []);
@@ -84,68 +81,9 @@ function LogBoxInspector(props: Props): React.Node {
);
}
const headerTitleMap = {
warn: 'Console Warning',
error: 'Console Error',
fatal: 'Uncaught Error',
syntax: 'Syntax Error',
component: 'Render Error',
};
function LogBoxInspectorBody(props: {log: LogBoxLog, onRetry: () => void}) {
const [collapsed, setCollapsed] = React.useState(true);
React.useEffect(() => {
setCollapsed(true);
}, [props.log]);
const headerTitle =
props.log.type ??
headerTitleMap[props.log.isComponentError ? 'component' : props.log.level];
if (collapsed) {
return (
<>
<LogBoxInspectorMessageHeader
collapsed={collapsed}
onPress={() => setCollapsed(!collapsed)}
message={props.log.message}
level={props.log.level}
title={headerTitle}
/>
<ScrollView style={styles.scrollBody}>
<LogBoxInspectorCodeFrame codeFrame={props.log.codeFrame} />
<LogBoxInspectorReactFrames log={props.log} />
<LogBoxInspectorStackFrames log={props.log} onRetry={props.onRetry} />
</ScrollView>
</>
);
}
return (
<ScrollView style={styles.scrollBody}>
<LogBoxInspectorMessageHeader
collapsed={collapsed}
onPress={() => setCollapsed(!collapsed)}
message={props.log.message}
level={props.log.level}
title={headerTitle}
/>
<LogBoxInspectorCodeFrame codeFrame={props.log.codeFrame} />
<LogBoxInspectorReactFrames log={props.log} />
<LogBoxInspectorStackFrames log={props.log} onRetry={props.onRetry} />
</ScrollView>
);
}
const styles = StyleSheet.create({
root: {
flex: 1,
backgroundColor: LogBoxStyle.getTextColor(),
},
scrollBody: {
backgroundColor: LogBoxStyle.getBackgroundColor(0.9),
flex: 1,
},
});
export default LogBoxInspector;
@@ -0,0 +1,87 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import ScrollView from '../../Components/ScrollView/ScrollView';
import StyleSheet from '../../StyleSheet/StyleSheet';
import LogBoxLog from '../Data/LogBoxLog';
import LogBoxInspectorCodeFrame from './LogBoxInspectorCodeFrame';
import LogBoxInspectorMessageHeader from './LogBoxInspectorMessageHeader';
import LogBoxInspectorReactFrames from './LogBoxInspectorReactFrames';
import LogBoxInspectorStackFrames from './LogBoxInspectorStackFrames';
import * as LogBoxStyle from './LogBoxStyle';
import * as React from 'react';
import {useEffect, useState} from 'react';
const headerTitleMap = {
warn: 'Console Warning',
error: 'Console Error',
fatal: 'Uncaught Error',
syntax: 'Syntax Error',
component: 'Render Error',
};
export default function LogBoxInspectorBody(props: {
log: LogBoxLog,
onRetry: () => void,
}): React.Node {
const [collapsed, setCollapsed] = useState(true);
useEffect(() => {
setCollapsed(true);
}, [props.log]);
const headerTitle =
props.log.type ??
headerTitleMap[props.log.isComponentError ? 'component' : props.log.level];
if (collapsed) {
return (
<>
<LogBoxInspectorMessageHeader
collapsed={collapsed}
onPress={() => setCollapsed(!collapsed)}
message={props.log.message}
level={props.log.level}
title={headerTitle}
/>
<ScrollView style={styles.scrollBody}>
<LogBoxInspectorCodeFrame codeFrame={props.log.codeFrame} />
<LogBoxInspectorReactFrames log={props.log} />
<LogBoxInspectorStackFrames log={props.log} onRetry={props.onRetry} />
</ScrollView>
</>
);
}
return (
<ScrollView style={styles.scrollBody}>
<LogBoxInspectorMessageHeader
collapsed={collapsed}
onPress={() => setCollapsed(!collapsed)}
message={props.log.message}
level={props.log.level}
title={headerTitle}
/>
<LogBoxInspectorCodeFrame codeFrame={props.log.codeFrame} />
<LogBoxInspectorReactFrames log={props.log} />
<LogBoxInspectorStackFrames log={props.log} onRetry={props.onRetry} />
</ScrollView>
);
}
const styles = StyleSheet.create({
root: {
flex: 1,
backgroundColor: LogBoxStyle.getTextColor(),
},
scrollBody: {
backgroundColor: LogBoxStyle.getBackgroundColor(0.9),
flex: 1,
},
});
@@ -16,6 +16,21 @@ const LogBoxLog = require('../../Data/LogBoxLog').default;
const LogBoxInspector = require('../LogBoxInspector').default;
const React = require('react');
// Mock child components because we are interested in snapshotting the behavior
// of `LogBoxInspector`, not its children.
jest.mock('../LogBoxInspectorBody', () => ({
__esModule: true,
default: 'LogBoxInspectorBody',
}));
jest.mock('../LogBoxInspectorFooter', () => ({
__esModule: true,
default: 'LogBoxInspectorFooter',
}));
jest.mock('../LogBoxInspectorHeader', () => ({
__esModule: true,
default: 'LogBoxInspectorHeader',
}));
const logs = [
new LogBoxLog({
level: 'warn',
@@ -54,7 +69,7 @@ const logs = [
describe('LogBoxContainer', () => {
it('should render null with no logs', () => {
const output = render.shallowRender(
const output = render.create(
<LogBoxInspector
onDismiss={() => {}}
onMinimize={() => {}}
@@ -68,7 +83,7 @@ describe('LogBoxContainer', () => {
});
it('should render warning with selectedIndex 0', () => {
const output = render.shallowRender(
const output = render.create(
<LogBoxInspector
onDismiss={() => {}}
onMinimize={() => {}}
@@ -82,7 +97,7 @@ describe('LogBoxContainer', () => {
});
it('should render fatal with selectedIndex 2', () => {
const output = render.shallowRender(
const output = render.create(
<LogBoxInspector
onDismiss={() => {}}
onMinimize={() => {}}
@@ -35,7 +35,7 @@ exports[`LogBoxContainer should render fatal with selectedIndex 2 1`] = `
"symbolicated": Object {
"error": null,
"stack": null,
"status": "NONE",
"status": "PENDING",
},
"symbolicatedComponentStack": Object {
"componentStack": null,
@@ -5525,16 +5525,23 @@ declare export default typeof LogBoxButton;
`;
exports[`public API should not change unintentionally Libraries/LogBox/UI/LogBoxInspector.js 1`] = `
"type Props = $ReadOnly<{|
"type Props = $ReadOnly<{
onDismiss: () => void,
onChangeSelectedIndex: (index: number) => void,
onMinimize: () => void,
logs: $ReadOnlyArray<LogBoxLog>,
selectedIndex: number,
fatalType?: ?LogLevel,
|}>;
declare function LogBoxInspector(props: Props): React.Node;
declare export default typeof LogBoxInspector;
}>;
declare export default function LogBoxInspector(props: Props): React.Node;
"
`;
exports[`public API should not change unintentionally Libraries/LogBox/UI/LogBoxInspectorBody.js 1`] = `
"declare export default function LogBoxInspectorBody(props: {
log: LogBoxLog,
onRetry: () => void,
}): React.Node;
"
`;