Skip to content

Commit c1b2a34

Browse files
timjacobigaearon
authored andcommitted
Rewrite ReactTreeTraversal-test.js using public APIs (#11664)
* rewrite two phase traversal tests with public APIs * rewrite enter/leave tests * lift render into beforeEach, organise variables * move getLowestCommonAncestor test * remove internal tree traversal test * fix linter errors * move creation of outer nodes into {before,after}Each * explain why getLowestCommonAncestor test was moved * remove unnessecary ARG and ARG2 token these were used for testing the internal API to simulate synthetic events passed to traverseEnterLeave. since we're now dealing with actual synthetic events we can remove them. * run prettier
1 parent b097a34 commit c1b2a34

File tree

3 files changed

+364
-241
lines changed

3 files changed

+364
-241
lines changed

packages/events/__tests__/ResponderEventPlugin-test.internal.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,4 +1360,90 @@ describe('ResponderEventPlugin', () => {
13601360
run(config, three, nativeEvent);
13611361
expect(ResponderEventPlugin._getResponder()).toBe(null);
13621362
});
1363+
1364+
it('should determine the first common ancestor correctly', () => {
1365+
// This test was moved here from the ReactTreeTraversal test since only the
1366+
// ResponderEventPlugin uses `getLowestCommonAncestor`
1367+
var React = require('react');
1368+
var ReactTestUtils = require('react-dom/test-utils');
1369+
var ReactTreeTraversal = require('shared/ReactTreeTraversal');
1370+
var ReactDOMComponentTree = require('../../react-dom/src/client/ReactDOMComponentTree');
1371+
1372+
class ChildComponent extends React.Component {
1373+
render() {
1374+
return (
1375+
<div ref="DIV" id={this.props.id + '__DIV'}>
1376+
<div ref="DIV_1" id={this.props.id + '__DIV_1'} />
1377+
<div ref="DIV_2" id={this.props.id + '__DIV_2'} />
1378+
</div>
1379+
);
1380+
}
1381+
}
1382+
1383+
class ParentComponent extends React.Component {
1384+
render() {
1385+
return (
1386+
<div ref="P" id="P">
1387+
<div ref="P_P1" id="P_P1">
1388+
<ChildComponent ref="P_P1_C1" id="P_P1_C1" />
1389+
<ChildComponent ref="P_P1_C2" id="P_P1_C2" />
1390+
</div>
1391+
<div ref="P_OneOff" id="P_OneOff" />
1392+
</div>
1393+
);
1394+
}
1395+
}
1396+
1397+
var parent = ReactTestUtils.renderIntoDocument(<ParentComponent />);
1398+
1399+
var ancestors = [
1400+
// Common ancestor with self is self.
1401+
{
1402+
one: parent.refs.P_P1_C1.refs.DIV_1,
1403+
two: parent.refs.P_P1_C1.refs.DIV_1,
1404+
com: parent.refs.P_P1_C1.refs.DIV_1,
1405+
},
1406+
// Common ancestor with self is self - even if topmost DOM.
1407+
{one: parent.refs.P, two: parent.refs.P, com: parent.refs.P},
1408+
// Siblings
1409+
{
1410+
one: parent.refs.P_P1_C1.refs.DIV_1,
1411+
two: parent.refs.P_P1_C1.refs.DIV_2,
1412+
com: parent.refs.P_P1_C1.refs.DIV,
1413+
},
1414+
// Common ancestor with parent is the parent.
1415+
{
1416+
one: parent.refs.P_P1_C1.refs.DIV_1,
1417+
two: parent.refs.P_P1_C1.refs.DIV,
1418+
com: parent.refs.P_P1_C1.refs.DIV,
1419+
},
1420+
// Common ancestor with grandparent is the grandparent.
1421+
{
1422+
one: parent.refs.P_P1_C1.refs.DIV_1,
1423+
two: parent.refs.P_P1,
1424+
com: parent.refs.P_P1,
1425+
},
1426+
// Grandparent across subcomponent boundaries.
1427+
{
1428+
one: parent.refs.P_P1_C1.refs.DIV_1,
1429+
two: parent.refs.P_P1_C2.refs.DIV_1,
1430+
com: parent.refs.P_P1,
1431+
},
1432+
// Something deep with something one-off.
1433+
{
1434+
one: parent.refs.P_P1_C1.refs.DIV_1,
1435+
two: parent.refs.P_OneOff,
1436+
com: parent.refs.P,
1437+
},
1438+
];
1439+
var i;
1440+
for (i = 0; i < ancestors.length; i++) {
1441+
var plan = ancestors[i];
1442+
var firstCommon = ReactTreeTraversal.getLowestCommonAncestor(
1443+
ReactDOMComponentTree.getInstanceFromNode(plan.one),
1444+
ReactDOMComponentTree.getInstanceFromNode(plan.two),
1445+
);
1446+
expect(firstCommon).toBe(ReactDOMComponentTree.getInstanceFromNode(plan.com));
1447+
}
1448+
});
13631449
});

packages/react-dom/src/__tests__/ReactTreeTraversal-test.internal.js

Lines changed: 0 additions & 241 deletions
This file was deleted.

0 commit comments

Comments
 (0)