【发布时间】:2019-07-20 06:44:30
【问题描述】:
[Create-React-App] Jest 和 Enzyme(3.9.0) 似乎无法从 Auth.jx 容器中找到我的 <Button/> 元素。
应用程序应呈现 Auth 容器 if(!isAuthernticated),如果未提供输入,则应禁用该按钮。
我试过 ShallowWrapper::dive() 但我得到一个 TypeError
TypeError: SHallowWrapper::dive() can only be called on components
Auth.jsx
//...
let errorMessage = null;
let button=<Button id='Disabled' btnType="Success" disabled={false}>Disabled</Button>;
let authRedirect = null;
if (this.props.isAuthenticated) {
authRedirect = <Redirect to={this.props.authRedirectPath}/>
}
if (this.state.controls.username.value && this.state.controls.password.value){
button=<Button id='Login' btnType="Success">Login</Button>
}
return (
<div>
{authRedirect}
<form onSubmit={this.handleSubmit}>
{form}
{button}
</form>
</div>
)
}
//...
Auth.test.js
import React from 'react';
import {shallow} from 'enzyme';
import Auth from '../containers/Auth/Auth';
import Button from '../components/Button/button';
import Input from '../components/Input/input';
describe('<Auth/>',() =>{
let wrapper;
beforeEach(()=>{
wrapper=shallow(<Auth authRedirectPath='/' isAuthenticated={false}/>).dive()
})
//Test 1
it('should render disabled button if no input has been specified ',()=>{
expect(wrapper.find(Button).text()).toEqual('Disabled')
});
})
【问题讨论】:
-
你也可以发布你的
Auth.test.js代码吗? -
张贴@alvinLee
-
您在
Auth组件上使用任何HOC 吗?例如withRouter? -
我用的是redux的
connect@zsgomori
标签: reactjs unit-testing tdd jestjs babel-jest