Skip to content

Commit a691661

Browse files
committed
test(react-devtools-shared): useDebugValue with complex types
1 parent 1a6d817 commit a691661

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

packages/react-devtools-shared/src/__tests__/__snapshots__/inspectedElementContext-test.js.snap

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`InspectedElementContext display complex values of useDebugValue: DisplayedComplexValue 1`] = `
4+
{
5+
"id": 3,
6+
"owners": null,
7+
"context": null,
8+
"hooks": [
9+
{
10+
"id": null,
11+
"isStateEditable": false,
12+
"name": "DebuggableHook",
13+
"value": {
14+
"foo": 2
15+
},
16+
"subHooks": []
17+
},
18+
{
19+
"id": 0,
20+
"isStateEditable": true,
21+
"name": "State",
22+
"value": 1,
23+
"subHooks": []
24+
}
25+
],
26+
"props": {},
27+
"state": null
28+
}
29+
`;
30+
31+
exports[`InspectedElementContext display complex values of useDebugValue: IgnoredComplexValue 1`] = `
32+
{
33+
"id": 2,
34+
"owners": null,
35+
"context": null,
36+
"hooks": [
37+
{
38+
"id": null,
39+
"isStateEditable": false,
40+
"name": "NotDebuggableHook",
41+
"value": {
42+
"foo": 2
43+
},
44+
"subHooks": [
45+
{
46+
"id": 0,
47+
"isStateEditable": true,
48+
"name": "State",
49+
"value": 1,
50+
"subHooks": []
51+
}
52+
]
53+
}
54+
],
55+
"props": {},
56+
"state": null
57+
}
58+
`;
59+
360
exports[`InspectedElementContext should dehydrate complex nested values when requested: 1: Initially inspect element 1`] = `
461
{
562
"id": 2,

packages/react-devtools-shared/src/__tests__/inspectedElementContext-test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,4 +1571,90 @@ describe('InspectedElementContext', () => {
15711571

15721572
done();
15731573
});
1574+
1575+
it('display complex values of useDebugValue', async done => {
1576+
let getInspectedElementPath: GetInspectedElementPath = ((null: any): GetInspectedElementPath);
1577+
let inspectedElement = null;
1578+
function Suspender({target}) {
1579+
const context = React.useContext(InspectedElementContext);
1580+
getInspectedElementPath = context.getInspectedElementPath;
1581+
inspectedElement = context.getInspectedElement(target);
1582+
return null;
1583+
}
1584+
1585+
const container = document.createElement('div');
1586+
1587+
function useNotDebuggableHook() {
1588+
React.useDebugValue({foo: 2});
1589+
React.useState(1);
1590+
return 1;
1591+
}
1592+
function IgnoredComplexValue() {
1593+
useNotDebuggableHook();
1594+
return null;
1595+
}
1596+
1597+
function useDebuggableHook() {
1598+
React.useDebugValue({foo: 2});
1599+
return 1;
1600+
}
1601+
function DisplayedComplexValue() {
1602+
useDebuggableHook();
1603+
React.useState(1);
1604+
return null;
1605+
}
1606+
1607+
await utils.actAsync(() =>
1608+
ReactDOM.render(
1609+
<React.Fragment>
1610+
<IgnoredComplexValue />
1611+
<DisplayedComplexValue />
1612+
</React.Fragment>,
1613+
container,
1614+
),
1615+
);
1616+
1617+
const ignoredComplexValueIndex = 0
1618+
const ignoredComplexValueId = ((store.getElementIDAtIndex(ignoredComplexValueIndex): any): number);
1619+
await utils.actAsync(
1620+
() =>
1621+
TestRenderer.create(
1622+
<Contexts
1623+
defaultSelectedElementID={ignoredComplexValueId}
1624+
defaultSelectedElementIndex={ignoredComplexValueIndex}>
1625+
<React.Suspense fallback={null}>
1626+
<Suspender target={ignoredComplexValueId} />
1627+
</React.Suspense>
1628+
</Contexts>,
1629+
),
1630+
false,
1631+
);
1632+
expect(getInspectedElementPath).not.toBeNull();
1633+
expect(inspectedElement).not.toBeNull();
1634+
expect(inspectedElement).toMatchSnapshot('IgnoredComplexValue');
1635+
1636+
inspectedElement = null;
1637+
1638+
1639+
const displayedComplexValueIndex = 1
1640+
const displayedComplexValueId = ((store.getElementIDAtIndex(displayedComplexValueIndex): any): number);
1641+
await utils.actAsync(
1642+
() =>
1643+
TestRenderer.create(
1644+
<Contexts
1645+
defaultSelectedElementID={displayedComplexValueId}
1646+
defaultSelectedElementIndex={displayedComplexValueIndex}>
1647+
<React.Suspense fallback={null}>
1648+
<Suspender target={displayedComplexValueId} />
1649+
</React.Suspense>
1650+
</Contexts>,
1651+
),
1652+
false,
1653+
);
1654+
expect(getInspectedElementPath).not.toBeNull();
1655+
expect(inspectedElement).not.toBeNull();
1656+
expect(inspectedElement).toMatchSnapshot('DisplayedComplexValue');
1657+
1658+
done();
1659+
});
15741660
});

0 commit comments

Comments
 (0)