我有一个组件,其工作是向其子项添加某些属性:
const Parent = React.createClass({
doStuff() {
// ...
},
render() {
const child = React.cloneElement(this.props.children, {doStuff: this.doStuff});
return <div>{child}</div>;
}
});
0.13我可以这样测试:
const {renderIntoDocument, findRenderedDOMComponentWithClass} = TestUtils;
const parent = renderIntoDocument(<Parent><span className="test" /></Parent>);
const child = findRenderedDOMComponentWithClass(parent, "test");
expect(child.props.doStuff).to.equal(parent.doStuff);
测试此内容的“0.14方式”是什么?
PS。我在其他地方测试Parent.doStuff的行为,但我还需要确保给定的子进程获得对此方法的引用。
PPS。我读过How to check props of a DOM node in an unit test in React 0.14?但它并不适用于我的问题,因为我没有测试我可以用domNode.getAttribute()
阅读的道具。
答案 0 :(得分:0)
最后我明白了。以下是我现在可以使用的内容(IntentFilter intentFilter = new IntentFilter("android.intent.action.AIRPLANE_MODE");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!isAirplaneModeOn(this)) {
mDoneBtn.setEnabled(false);
} else {
mDoneBtn.setEnabled(true);
}
}
};
context.registerReceiver(receiver, intentFilter);
@Override
public void onDestroy() {
super.onDestroy();
unregisterRecievers();
}
private void unregisterRecievers() {
try {
unregisterReceiver(receiver);
} catch (Exception e) {
e.printStackTrace();
}
}
是被测单位):
Parent
const TestChild = () => <div />;
const TestContainer = React.createClass({
getParent() {
return this.refs.parent;
},
getChild() {
return this.refs.testChild;
},
render() {
return <Parent ref="parent"><TestChild ref="testChild" /></Parent>;
}
});
const {renderIntoDocument, findRenderedDOMComponentWithClass} = TestUtils;
const testContainer = renderIntoDocument(<TestContainer />);
const parent = testContainer.getParent();
const child = testContainer.getChild();
expect(child.props.doStuff).to.equal(parent.doStuff);
必须是自定义组件,否则TestChild
仅返回DOM节点而不是组件实例。