Skip to content

Commit 28afa05

Browse files
authored
Merge pull request alibaba#471 from alibaba/test/render-core-leaf
test: add renderer-core hoc/leaf single test
2 parents bf2c1e9 + 11319e1 commit 28afa05

8 files changed

Lines changed: 728 additions & 173 deletions

File tree

packages/renderer-core/jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const jestConfig = {
2323
collectCoverageFrom: [
2424
'src/**/*.ts',
2525
'src/**/*.tsx',
26+
'!src/utils/logger.ts',
27+
'!src/types/index.ts',
2628
],
2729
};
2830

packages/renderer-core/src/hoc/leaf.tsx

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,6 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
216216
this.recordTime();
217217
}
218218

219-
get childrenMap(): any {
220-
const map = new Map();
221-
222-
if (!this.hasChildren) {
223-
return map;
224-
}
225-
226-
this.children.forEach((d: any) => {
227-
if (Array.isArray(d)) {
228-
map.set(d[0].props.componentId, d);
229-
return;
230-
}
231-
map.set(d.props.componentId, d);
232-
});
233-
234-
return map;
235-
}
236-
237219
get defaultState() {
238220
const {
239221
hidden = false,
@@ -253,18 +235,18 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
253235
super(props, context);
254236
// 监听以下事件,当变化时更新自己
255237
__debug(`${schema.componentName}[${this.props.componentId}] leaf render in SimulatorRendererView`);
256-
clearRerenderEvent(this.props.componentId);
238+
clearRerenderEvent(componentCacheId);
257239
const _leaf = this.leaf;
258240
this.initOnPropsChangeEvent(_leaf);
259241
this.initOnChildrenChangeEvent(_leaf);
260242
this.initOnVisibleChangeEvent(_leaf);
261243
this.curEventLeaf = _leaf;
262244

263-
cache.ref.set(props.componentId, {
245+
cache.ref.set(componentCacheId, {
264246
makeUnitRender: this.makeUnitRender,
265247
});
266248

267-
let cacheState = cache.state.get(props.componentId);
249+
let cacheState = cache.state.get(componentCacheId);
268250
if (!cacheState || cacheState.__tag !== props.__tag) {
269251
cacheState = this.defaultState;
270252
}
@@ -275,7 +257,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
275257
private curEventLeaf: Node | undefined;
276258

277259
setState(state: any) {
278-
cache.state.set(this.props.componentId, {
260+
cache.state.set(componentCacheId, {
279261
...this.state,
280262
...state,
281263
__tag: this.props.__tag,
@@ -489,7 +471,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
489471
// TODO: 缓存同级其他元素的 children。
490472
// 缓存二级 children Next 查询筛选组件有问题
491473
// 缓存一级 children Next Tab 组件有问题
492-
const nextChild = getChildren(leaf?.export?.(TransformStage.Render) as types.ISchema, scope, Comp); // this.childrenMap
474+
const nextChild = getChildren(leaf?.export?.(TransformStage.Render) as types.ISchema, scope, Comp);
493475
__debug(`${schema.componentName}[${this.props.componentId}] component trigger onChildrenChange event`, nextChild);
494476
this.setState({
495477
nodeChildren: nextChild,
@@ -531,7 +513,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
531513
}
532514

533515
get leaf(): Node | undefined {
534-
return this.props._leaf || getNode(this.props.componentId);
516+
return this.props._leaf || getNode(componentCacheId);
535517
}
536518

537519
render() {
Lines changed: 128 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,146 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`leafWrapper base 1`] = `
4-
<div
5-
_leaf={
6-
Node {
7-
"emitter": EventEmitter {
8-
"_events": Object {
9-
"onChildrenChange": [Function],
10-
"onPropChange": [Function],
11-
"onVisibleChange": [Function],
12-
},
13-
"_eventsCount": 3,
14-
"_maxListeners": undefined,
15-
Symbol(kCapture): false,
16-
},
17-
"hasLoop": false,
18-
"schema": Object {},
19-
}
20-
}
21-
>
22-
<div>
3+
exports[`children this.props.children is array 1`] = `
4+
<div>
5+
<div
6+
content="content"
7+
>
8+
content
9+
</div>
10+
<div
11+
content="content"
12+
>
13+
content
14+
</div>
15+
</div>
16+
`;
17+
18+
exports[`lifecycle leaf change and make componentWillReceiveProps 1`] = `
19+
<div>
20+
<div
21+
__tag="222"
22+
content="content new leaf"
23+
>
24+
content new leaf
25+
</div>
26+
</div>
27+
`;
28+
29+
exports[`lifecycle props change and make componentWillReceiveProps 1`] = `
30+
<div>
31+
<div
32+
content="content"
33+
>
34+
content
35+
</div>
36+
</div>
37+
`;
38+
39+
exports[`lifecycle props change and make componentWillReceiveProps 2`] = `
40+
<div>
41+
<div
42+
content="content 123"
43+
>
44+
content 123
45+
</div>
46+
</div>
47+
`;
48+
49+
exports[`lifecycle props change and make componentWillReceiveProps 3`] = `
50+
<div>
51+
<div
52+
__tag="111"
53+
content="content 123"
54+
>
55+
content 123
56+
</div>
57+
</div>
58+
`;
59+
60+
exports[`mini unit render leaf has a loop, render from parent 1`] = `
61+
<div>
62+
this is a new children
63+
</div>
64+
`;
65+
66+
exports[`mini unit render make text props change 1`] = `
67+
<div>
68+
<div
69+
content="content"
70+
>
2371
content
2472
</div>
2573
</div>
2674
`;
2775

28-
exports[`leafWrapper change ___condition___ props 1`] = `
76+
exports[`mini unit render make text props change 2`] = `
2977
<div
30-
_leaf={
31-
Node {
32-
"emitter": EventEmitter {
33-
"_events": Object {
34-
"onChildrenChange": [Function],
35-
"onPropChange": [Function],
36-
"onVisibleChange": [Function],
37-
},
38-
"_eventsCount": 3,
39-
"_maxListeners": undefined,
40-
Symbol(kCapture): false,
41-
},
42-
"hasLoop": false,
43-
"schema": Object {},
44-
}
45-
}
78+
newPropKey="newPropValue"
4679
/>
4780
`;
4881

49-
exports[`leafWrapper change ___condition___ props, but not hidden component 1`] = `
50-
<div
51-
_leaf={
52-
Node {
53-
"emitter": EventEmitter {
54-
"_events": Object {
55-
"onChildrenChange": [Function],
56-
"onPropChange": [Function],
57-
"onVisibleChange": [Function],
58-
},
59-
"_eventsCount": 3,
60-
"_maxListeners": undefined,
61-
Symbol(kCapture): false,
62-
},
63-
"hasLoop": false,
64-
"schema": Object {},
65-
}
66-
}
67-
>
68-
<div>
69-
new content
82+
exports[`mini unit render parent is a mock leaf 1`] = `
83+
<div>
84+
<div
85+
content="new content to mock"
86+
>
87+
new content to mock
7088
</div>
7189
</div>
7290
`;
7391

74-
exports[`leafWrapper change props 1`] = `
75-
<div
76-
_leaf={
77-
Node {
78-
"emitter": EventEmitter {
79-
"_events": Object {
80-
"onChildrenChange": [Function],
81-
"onPropChange": [Function],
82-
"onVisibleChange": [Function],
83-
},
84-
"_eventsCount": 3,
85-
"_maxListeners": undefined,
86-
Symbol(kCapture): false,
87-
},
88-
"hasLoop": false,
89-
"schema": Object {},
90-
}
91-
}
92-
>
93-
<div>
94-
new content
92+
exports[`mini unit render props has new children 1`] = `
93+
<div>
94+
children 01
95+
children 02
96+
</div>
97+
`;
98+
99+
exports[`onChildrenChange children is array string 1`] = `
100+
<div>
101+
onChildrenChange content 01
102+
onChildrenChange content 02
103+
</div>
104+
`;
105+
106+
exports[`onPropChange change textNode [key:___condition___] props, but not hidden component 1`] = `
107+
<div>
108+
<div
109+
content="content"
110+
>
111+
content
112+
</div>
113+
</div>
114+
`;
115+
116+
exports[`onPropChange change textNode [key:___condition___] props, hide textNode component 1`] = `<div />`;
117+
118+
exports[`onPropChange change textNode [key:content], content in this.props but not in leaf.export result 1`] = `
119+
<div>
120+
<div
121+
content="content"
122+
>
123+
content
124+
</div>
125+
</div>
126+
`;
127+
128+
exports[`onPropChange change textNode [key:content], content in this.props but not in leaf.export result 2`] = `
129+
<div>
130+
<div
131+
content={null}
132+
/>
133+
</div>
134+
`;
135+
136+
exports[`onVisibleChange visible is false 1`] = `<div />`;
137+
138+
exports[`onVisibleChange visible is true 1`] = `
139+
<div>
140+
<div
141+
content="content"
142+
>
143+
content
95144
</div>
96145
</div>
97146
`;

0 commit comments

Comments
 (0)