Skip to content

Commit 6647836

Browse files
committed
update cm tests @dai-shi
1 parent eb6f8cf commit 6647836

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

test/cm/__tests__/all_spec.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const names = [
88

99
const sleep = ms => new Promise(r => setTimeout(r, ms));
1010
jest.setTimeout(15 * 1000);
11-
const REPEAT = 10;
11+
const REPEAT = 5;
12+
const DOUBLE = 2; // two clicks to increment one
1213
const NUM_COMPONENTS = 50; // defined in src/common.js
1314

1415
names.forEach((name) => {
@@ -30,7 +31,7 @@ names.forEach((name) => {
3031

3132
it('check1: updated properly', async () => {
3233
delays = [];
33-
for (let loop = 0; loop < REPEAT; ++loop) {
34+
for (let loop = 0; loop < REPEAT * DOUBLE; ++loop) {
3435
const start = Date.now();
3536
// click buttons three times
3637
await Promise.all([
@@ -41,7 +42,7 @@ names.forEach((name) => {
4142
delays.push(Date.now() - start);
4243
}
4344
console.log(name, delays);
44-
// check if all counts become 15 = REPEAT * 3
45+
// check if all counts become REPEAT * 3
4546
await Promise.all([...Array(NUM_COMPONENTS).keys()].map(async (i) => {
4647
await expect(page).toMatchElement(`.count:nth-of-type(${i + 1})`, {
4748
text: `${REPEAT * 3}`,
@@ -70,8 +71,11 @@ names.forEach((name) => {
7071
page.click('#remoteIncrement'),
7172
page.click('#remoteIncrement'),
7273
page.click('#localIncrement'),
74+
page.click('#remoteIncrement'),
75+
page.click('#remoteIncrement'),
76+
page.click('#localIncrement'),
7377
]);
74-
// check if all counts become 18 = REPEAT * 3 + 3
78+
// check if all counts become REPEAT * 3 + 2
7579
await Promise.all([...Array(NUM_COMPONENTS).keys()].map(async (i) => {
7680
await expect(page).toMatchElement(`.count:nth-of-type(${i + 1})`, {
7781
text: `${REPEAT * 3 + 2}`,

test/cm/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</head>
88
<body>
99
<p>This is a button to trigger action from outside of React</p>
10-
<button id="remoteIncrement">Increment remote count</button>
10+
<button id="remoteIncrement">Increment remote count (two clicks to increment one)</button>
1111
<div id="app"></div>
1212
</body>
1313
</html>

test/cm/src/common.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ export const useRegisterIncrementDispatcher = (listener) => {
1818
}, [listener]);
1919
};
2020

21-
export const initialState = { count: 0 };
21+
export const initialState = {
22+
count: 0,
23+
dummy: 0,
24+
};
2225

2326
export const reducer = (state = initialState, action) => {
2427
switch (action.type) {
2528
case 'increment':
26-
return { ...state, count: state.count + 1 };
29+
return {
30+
...state,
31+
dummy: state.dummy + 1,
32+
// only update once in two
33+
count: state.dummy % 2 === 1 ? state.count + 1 : state.count,
34+
};
2735
default:
2836
return state;
2937
}
@@ -45,3 +53,14 @@ export const useCheckTearing = () => {
4553
}
4654
});
4755
};
56+
57+
// naive shallowEqual for React.memo
58+
// a hack until the issue is resolved
59+
// https://github.com/facebook/react/issues/17314
60+
// https://github.com/facebook/react/issues/17318
61+
export const shallowEqual = (prevProps, nextProps) => {
62+
const prevKeys = Object.keys(prevProps);
63+
const nextKeys = Object.keys(nextProps);
64+
return prevKeys.every(key => prevProps[key] === nextProps[key])
65+
&& nextKeys.every(key => prevProps[key] === nextProps[key]);
66+
};

test/cm/src/react-redux/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,16 @@ import {
88
reducer,
99
ids,
1010
useCheckTearing,
11+
shallowEqual,
1112
} from '../common';
1213

1314
const store = createStore(reducer);
1415

15-
const _Counter = () => {
16+
const Counter = React.memo(() => {
1617
const count = useSelector(state => state.count);
1718
syncBlock();
1819
return <div className="count">{count}</div>;
19-
};
20-
21-
_Counter.defaultProps={};
22-
//https://github.com/facebook/react/issues/17318 currently 2019.11.10 SimpleMemoCompnent is buggy on CM
23-
24-
const Counter=React.memo(_Counter);
20+
}, shallowEqual);
2521

2622
const Main = () => {
2723
const dispatch = useDispatch();

0 commit comments

Comments
 (0)