Skip to content

Commit c831387

Browse files
committed
test: Failing test for complex debug values in custom hooks
1 parent d35f8a5 commit c831387

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"glob-stream": "^6.1.0",
6464
"google-closure-compiler": "^20200112.0.0",
6565
"gzip-size": "^5.1.1",
66+
"identity-obj-proxy": "^3.0.0",
6667
"jasmine-check": "^1.0.0-rc.0",
6768
"jest": "^25.1.0",
6869
"jest-diff": "^25.1.0",
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
describe('HooksTree', () => {
2+
let React;
3+
let ReactDOM;
4+
let bridge: FrontendBridge;
5+
let store: Store;
6+
let utils;
7+
let HooksTree;
8+
9+
let BridgeContext;
10+
let InspectedElementContextController;
11+
let StoreContext;
12+
let TreeContextController;
13+
14+
beforeEach(() => {
15+
utils = require('./utils');
16+
utils.beforeEachProfiling();
17+
18+
bridge = global.bridge;
19+
store = global.store;
20+
store.collapseNodesByDefault = false;
21+
22+
React = require('react');
23+
ReactDOM = require('react-dom');
24+
25+
BridgeContext = require('react-devtools-shared/src/devtools/views/context')
26+
.BridgeContext;
27+
InspectedElementContextController = require('react-devtools-shared/src/devtools/views/Components/InspectedElementContext')
28+
.InspectedElementContextController;
29+
StoreContext = require('react-devtools-shared/src/devtools/views/context')
30+
.StoreContext;
31+
TreeContextController = require('react-devtools-shared/src/devtools/views/Components/TreeContext')
32+
.TreeContextController;
33+
34+
HooksTree = require('react-devtools-shared/src/devtools/views/Components/HooksTree')
35+
.HooksTreeView;
36+
});
37+
38+
const Contexts = ({
39+
children,
40+
defaultSelectedElementID = null,
41+
defaultSelectedElementIndex = null,
42+
}) => (
43+
<BridgeContext.Provider value={bridge}>
44+
<StoreContext.Provider value={store}>
45+
<TreeContextController
46+
defaultSelectedElementID={defaultSelectedElementID}
47+
defaultSelectedElementIndex={defaultSelectedElementIndex}>
48+
<InspectedElementContextController>
49+
{children}
50+
</InspectedElementContextController>
51+
</TreeContextController>
52+
</StoreContext.Provider>
53+
</BridgeContext.Provider>
54+
);
55+
56+
it('shows the complex value of custom hooks with sub-hooks', () => {
57+
// props from `InspectedElementContext display complex values of useDebugValue: DisplayedComplexValue 1` snapshot
58+
59+
const container = document.createElement('div');
60+
ReactDOM.render(
61+
<Contexts>
62+
<HooksTree
63+
hooks={[
64+
{
65+
id: null,
66+
isStateEditable: false,
67+
name: 'DebuggableHook',
68+
value: {
69+
foo: 2,
70+
},
71+
subHooks: [
72+
{
73+
id: 0,
74+
isStateEditable: true,
75+
name: 'State',
76+
value: 1,
77+
subHooks: [],
78+
},
79+
],
80+
},
81+
]}
82+
/>
83+
</Contexts>,
84+
container,
85+
);
86+
87+
const hook = container.querySelector('.NameValueRow');
88+
// it's actually DebuggableHook: {foo:2} but the first colon is added by css
89+
// which isn't available in the test
90+
expect(hook.textContent).toEqual('DebuggableHook{foo: 2}');
91+
});
92+
});

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

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

3+
exports[`InspectedElementContext display complex values of useDebugValue: DisplayedComplexValue 1`] = `
4+
{
5+
"id": 2,
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+
"id": 0,
19+
"isStateEditable": true,
20+
"name": "State",
21+
"value": 1,
22+
"subHooks": []
23+
}
24+
]
25+
}
26+
],
27+
"props": {},
28+
"state": null
29+
}
30+
`;
31+
332
exports[`InspectedElementContext should dehydrate complex nested values when requested: 1: Initially inspect element 1`] = `
433
{
534
"id": 2,

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,4 +1571,54 @@ 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 useDebuggableHook() {
1588+
React.useDebugValue({foo: 2});
1589+
React.useState(1);
1590+
return 1;
1591+
}
1592+
function DisplayedComplexValue() {
1593+
useDebuggableHook();
1594+
return null;
1595+
}
1596+
1597+
await utils.actAsync(() =>
1598+
ReactDOM.render(<DisplayedComplexValue />, container),
1599+
);
1600+
1601+
const ignoredComplexValueIndex = 0;
1602+
const ignoredComplexValueId = ((store.getElementIDAtIndex(
1603+
ignoredComplexValueIndex,
1604+
): any): number);
1605+
await utils.actAsync(
1606+
() =>
1607+
TestRenderer.create(
1608+
<Contexts
1609+
defaultSelectedElementID={ignoredComplexValueId}
1610+
defaultSelectedElementIndex={ignoredComplexValueIndex}>
1611+
<React.Suspense fallback={null}>
1612+
<Suspender target={ignoredComplexValueId} />
1613+
</React.Suspense>
1614+
</Contexts>,
1615+
),
1616+
false,
1617+
);
1618+
expect(getInspectedElementPath).not.toBeNull();
1619+
expect(inspectedElement).not.toBeNull();
1620+
expect(inspectedElement).toMatchSnapshot('DisplayedComplexValue');
1621+
1622+
done();
1623+
});
15741624
});

scripts/jest/config.build-devtools.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ packages.forEach(name => {
4343
] = `<rootDir>/build/node_modules/${name}/$1`;
4444
});
4545

46+
// https://jestjs.io/docs/en/webpack#mocking-css-modules
47+
moduleNameMapper['\\.(css|less)$'] = 'identity-obj-proxy';
48+
4649
module.exports = Object.assign({}, baseConfig, {
4750
// Redirect imports to the compiled bundles
4851
moduleNameMapper,

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6591,6 +6591,11 @@ har-validator@~5.1.0:
65916591
ajv "^6.5.5"
65926592
har-schema "^2.0.0"
65936593

6594+
harmony-reflect@^1.4.6:
6595+
version "1.6.1"
6596+
resolved "https://registry.yarnpkg.com/harmony-reflect/-/harmony-reflect-1.6.1.tgz#c108d4f2bb451efef7a37861fdbdae72c9bdefa9"
6597+
integrity sha512-WJTeyp0JzGtHcuMsi7rw2VwtkvLa+JyfEKJCFyfcS0+CDkjQ5lHPu7zEhFZP+PDSRrEgXa5Ah0l1MbgbE41XjA==
6598+
65946599
has-ansi@^2.0.0:
65956600
version "2.0.0"
65966601
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91"
@@ -6938,6 +6943,13 @@ icss-utils@^2.1.0:
69386943
dependencies:
69396944
postcss "^6.0.1"
69406945

6946+
identity-obj-proxy@^3.0.0:
6947+
version "3.0.0"
6948+
resolved "https://registry.yarnpkg.com/identity-obj-proxy/-/identity-obj-proxy-3.0.0.tgz#94d2bda96084453ef36fbc5aaec37e0f79f1fc14"
6949+
integrity sha1-lNK9qWCERT7zb7xarsN+D3nx/BQ=
6950+
dependencies:
6951+
harmony-reflect "^1.4.6"
6952+
69416953
ieee754@^1.1.12, ieee754@^1.1.4:
69426954
version "1.1.13"
69436955
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.13.tgz#ec168558e95aa181fd87d37f55c32bbcb6708b84"

0 commit comments

Comments
 (0)