Skip to content

Commit

Permalink
fix(react-devtools-shared): useDebugValue with complex types (#18070)
Browse files Browse the repository at this point in the history
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com>
  • Loading branch information
eps1lon and bvaughn committed Mar 17, 2020
1 parent 90f8fe6 commit 756e1ea
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`InspectedElementContext display complex values of useDebugValue: DisplayedComplexValue 1`] = `
{
"id": 2,
"owners": null,
"context": null,
"hooks": [
{
"id": null,
"isStateEditable": false,
"name": "DebuggableHook",
"value": {
"foo": 2
},
"subHooks": [
{
"id": 0,
"isStateEditable": true,
"name": "State",
"value": 1,
"subHooks": []
}
]
}
],
"props": {},
"state": null
}
`;

exports[`InspectedElementContext should dehydrate complex nested values when requested: 1: Initially inspect element 1`] = `
{
"id": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1571,4 +1571,54 @@ describe('InspectedElementContext', () => {

done();
});

it('display complex values of useDebugValue', async done => {
let getInspectedElementPath: GetInspectedElementPath = ((null: any): GetInspectedElementPath);
let inspectedElement = null;
function Suspender({target}) {
const context = React.useContext(InspectedElementContext);
getInspectedElementPath = context.getInspectedElementPath;
inspectedElement = context.getInspectedElement(target);
return null;
}

const container = document.createElement('div');

function useDebuggableHook() {
React.useDebugValue({foo: 2});
React.useState(1);
return 1;
}
function DisplayedComplexValue() {
useDebuggableHook();
return null;
}

await utils.actAsync(() =>
ReactDOM.render(<DisplayedComplexValue />, container),
);

const ignoredComplexValueIndex = 0;
const ignoredComplexValueId = ((store.getElementIDAtIndex(
ignoredComplexValueIndex,
): any): number);
await utils.actAsync(
() =>
TestRenderer.create(
<Contexts
defaultSelectedElementID={ignoredComplexValueId}
defaultSelectedElementIndex={ignoredComplexValueIndex}>
<React.Suspense fallback={null}>
<Suspender target={ignoredComplexValueId} />
</React.Suspense>
</Contexts>,
),
false,
);
expect(getInspectedElementPath).not.toBeNull();
expect(inspectedElement).not.toBeNull();
expect(inspectedElement).toMatchSnapshot('DisplayedComplexValue');

done();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
overflow: hidden;
text-overflow: ellipsis;
cursor: default;
white-space: nowrap;
}

.None {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import EditableValue from './EditableValue';
import ExpandCollapseToggle from './ExpandCollapseToggle';
import {InspectedElementContext} from './InspectedElementContext';
import KeyValue from './KeyValue';
import {serializeHooksForCopy} from '../utils';
import {getMetaValueLabel, serializeHooksForCopy} from '../utils';
import styles from './HooksTree.css';
import useContextMenu from '../../ContextMenu/useContextMenu';
import {meta} from '../../../hydration';
Expand Down Expand Up @@ -202,6 +202,9 @@ function HookView({canEditHooks, hook, id, inspectPath, path}: HookViewProps) {
className={name !== '' ? styles.Name : styles.NameAnonymous}>
{name || 'Anonymous'}
</span>
<span className={styles.Value} onClick={toggleIsOpen}>
{isOpen || getMetaValueLabel(value)}
</span>
</div>
<div className={styles.Children} hidden={!isOpen}>
<KeyValue
Expand Down

0 comments on commit 756e1ea

Please sign in to comment.