Unit testing 酶';查找';不';无法通过类名找到div
我用酶来测试一种反应成分。但是,“find”方法无法找到任何元素。不按类名、元素等查找。这肯定是我做错了,因为我可以使用。在单独的项目中查找,但我似乎找不到我做错了什么Unit testing 酶';查找';不';无法通过类名找到div,unit-testing,jestjs,enzyme,Unit Testing,Jestjs,Enzyme,我用酶来测试一种反应成分。但是,“find”方法无法找到任何元素。不按类名、元素等查找。这肯定是我做错了,因为我可以使用。在单独的项目中查找,但我似乎找不到我做错了什么 节点:6.10.0 jest cli:12.1.1 巴别塔笑话:12.1.0 酶:2.7.1 笑话:19.0.2 jest酶:2.1.2 反应:15.1.0 反应组分 从“React”导入React 从“../../components/flipable\u two\u窗格/模板”导入可翻转 类FLippableWidget
- 节点:6.10.0
- jest cli:12.1.1
- 巴别塔笑话:12.1.0
- 酶:2.7.1
- 笑话:19.0.2
- jest酶:2.1.2
- 反应:15.1.0
从“React”导入React
从“../../components/flipable\u two\u窗格/模板”导入可翻转
类FLippableWidget扩展了React.Component{
建造师(道具){
超级(道具)
this.defaultState()
}
//设置默认状态
默认状态(){
常量状态={
已选:0
}
//如果状态存在,请重置它。
if(typeof this.state==='object'){
this.setState(状态)
//否则,创建状态。
}否则{
this.state=状态
}
}
//单击处理程序。
handleClick(e){
const keyPress=e.keyCode
const keycenter=keyPress==13
//切换当前选定的
让index=this.state.selected==0?1:0
//退出,如果不是“进入”键。
如果(按键&&!键输入){
返回
}
这是我的国家({
选定:索引
})
if(this.props.clickCallback的类型!==“函数”){
返回
}
this.props.clickCallback(e,index)
}
//渲染方法。
渲染(){
常量类='flippable widget'+this.props.className
var isFlipped=this.props.isFlipped | | this.state.selected?真:假
const handleClick=this.handleClick.bind(this)
让孩子们=这个.props.children | |[]
返回(
{this.props.title}
{
childrence.length==3
?儿童[2]
:
- {handleClick(e)}
onKeyDown={(e)=>{handleClick(e)}
>
}
{/*前窗格*/}
{children[0]}
{/*后窗格*/}
{儿童[1]}
)
}
}
FLippableWidget.propTypes={
子项:React.PropTypes.node,
类名:React.PropTypes.string,
标题:React.PropTypes.string,
高度:React.PropTypes.string
}
导出默认FlipperWidget
组件测试
从“../source/components/widget_flipable/template”导入flipablewidget;
从“React”导入React;
从“酶”导入{shall,mount};
开玩笑。自动模拟();
描述(“”,()=>{
让包装纸;
让道具={
子项:['mock-front-pane','mock-back-pane'],
className:'mockClassName',
标题:“mockTitle”,
高度:“300px”
};
在每个之前(()=>{
包装器=mount();
});
//***************************************
//测试结构
//***************************************
它('加载时接收道具',()=>{
expect(wrapper.prop('className')).toEqual('mockClassName');
expect(wrapper.prop('title')).toEqual('mockTitle');
expect(wrapper.prop('height')).toEqual('300px');
expect(wrapper.prop('children')).toEqual(['mock-front-pane','mock-back-pane']);
});
它('呈现具有正确类名的div',()=>{
//console.log(wrapper.debug();
expect(wrapper.find('.widget').length).toBe(1);
});
});
道具测试通过,但第二个测试“…呈现一个div…”没有通过。它显示一个错误“预期0为1”。我明白这个错误的意思,但我不明白为什么会发生
console.log(wrapper.debug())
原来是React的版本导致了问题。通过更新到15.4.2来修复注意:我知道测试挂载有某些道具对酶来说是毫无意义的新东西,所以我只是测试一下。
ReactWrapper {
component:
ReactClassComponent {
setChildProps:
{ [Function: bound setChildProps]
__reactBoundContext: [Circular],
__reactBoundMethod: [Function: setChildProps],
__reactBoundArguments: null,
bind: [Function] },
setChildContext:
{ [Function: bound setChildContext]
__reactBoundContext: [Circular],
__reactBoundMethod: [Function: setChildContext],
__reactBoundArguments: null,
bind: [Function] },
getInstance:
{ [Function: bound getInstance]
__reactBoundContext: [Circular],
__reactBoundMethod: [Function: getInstance],
__reactBoundArguments: null,
bind: [Function] },
getWrappedComponent:
{ [Function: bound getWrappedComponent]
__reactBoundContext: [Circular],
__reactBoundMethod: [Function: getWrappedComponent],
__reactBoundArguments: null,
bind: [Function] },
props: { Component: [Object], props: [Object], context: null },
context: {},
refs: {},
updater:
{ isMounted: [Function: isMounted],
enqueueCallback: [Function: enqueueCallback],
enqueueCallbackInternal: [Function: enqueueCallbackInternal],
enqueueForceUpdate: [Function: enqueueForceUpdate],
enqueueReplaceState: [Function: enqueueReplaceState],
enqueueSetState: [Function: enqueueSetState],
enqueueElementInternal: [Function: enqueueElementInternal],
validateCallback: [Function: validateCallback] },
state: { mount: true, props: [Object], context: null },
_reactInternalInstance:
ReactCompositeComponentWrapper {
_currentElement: [Object],
_rootNodeID: 0,
_compositeType: 0,
_instance: [Circular],
_hostParent: null,
_hostContainerInfo: [Object],
_updateBatchNumber: null,
_pendingElement: null,
_pendingStateQueue: null,
_pendingReplaceState: false,
_pendingForceUpdate: false,
_renderedNodeType: 1,
_renderedComponent: [Object],
_context: {},
_mountOrder: 8,
_topLevelWrapper: [Object],
_pendingCallbacks: null,
_calledComponentWillUnmount: false,
_warnedAboutRefsInRender: false,
_mountIndex: 0,
_mountImage: null,
_debugID: 5 } },
root: [Circular],
node:
FLippableWidget {
defaultState:
{ [Function: defaultState]
_isMockFunction: true,
_getMockImplementation: [Function],
mock: [Object],
mockClear: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImpl: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
_protoImpl: [Object] },
handleClick:
{ [Function: handleClick]
_isMockFunction: true,
_getMockImplementation: [Function],
mock: [Object],
mockClear: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImpl: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
_protoImpl: [Object] },
render:
{ [Function: render]
_isMockFunction: true,
_getMockImplementation: [Function],
mock: [Object],
mockClear: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImpl: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
_protoImpl: [Object] },
setState:
{ [Function: mockConstructor]
_isMockFunction: true,
_getMockImplementation: [Function],
mock: [Object],
mockClear: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImpl: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
_protoImpl: [Object] },
forceUpdate:
{ [Function: mockConstructor]
_isMockFunction: true,
_getMockImplementation: [Function],
mock: [Object],
mockClear: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImpl: [Function],
mockImplementation: [Function],
mockReturnThis: [Function],
_protoImpl: [Object] },
props:
{ children: [Object],
className: 'mockClassName',
title: 'mockTitle',
height: '300px' },
context: {},
refs: {},
updater:
{ isMounted: [Function: isMounted],
enqueueCallback: [Function: enqueueCallback],
enqueueCallbackInternal: [Function: enqueueCallbackInternal],
enqueueForceUpdate: [Function: enqueueForceUpdate],
enqueueReplaceState: [Function: enqueueReplaceState],
enqueueSetState: [Function: enqueueSetState],
enqueueElementInternal: [Function: enqueueElementInternal],
validateCallback: [Function: validateCallback] },
_reactInternalInstance:
ReactCompositeComponentWrapper {
_currentElement: [Object],
_rootNodeID: 0,
_compositeType: 0,
_instance: [Circular],
_hostParent: null,
_hostContainerInfo: [Object],
_updateBatchNumber: null,
_pendingElement: null,
_pendingStateQueue: null,
_pendingReplaceState: false,
_pendingForceUpdate: false,
_renderedNodeType: 2,
_renderedComponent: [Object],
_context: {},
_mountOrder: 9,
_topLevelWrapper: null,
_pendingCallbacks: null,
_calledComponentWillUnmount: false,
_warnedAboutRefsInRender: false,
_mountIndex: 0,
_mountImage: null,
_debugID: 6 },
state: null },
nodes:
[ FLippableWidget {
defaultState: [Object],
setIconColorByClassName: [Object],
handleClick: [Object],
render: [Object],
setState: [Object],
forceUpdate: [Object],
props: [Object],
context: {},
refs: {},
updater: [Object],
_reactInternalInstance: [Object],
state: null } ],
length: 1,
options: {},
complexSelector:
ComplexSelector {
buildPredicate: [Function: buildInstPredicate],
findWhereUnwrapped: [Function: findWhereUnwrapped],
childrenOfNode: [Function: childrenOfInst] } }