我是新来的反应,玩笑和酶,现在我正在向我的反应应用程序添加一些单元测试,并且遇到了困扰我一段时间的问题。
我正在使用酶中的悬液来渲染组件,当我尝试'expect(wrapper).toMatchSnapshot()'时,问题就发生了,应用程序陷入了无限循环。我注意到两件事:当我使用浅表时,不会发生无限循环,但是在这种特定情况下,我需要使用mount,因为我在DetailsCustomer.js组件和 snapshots 文件中使用了ref。不在测试文件夹中创建。
这是我的测试
DetailsCustomer.test.js
import React from 'react'
import { configure, mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import { DetailsCostumer } from 'containers/SignUp/DetailsCostumer/DetailsCostumer'
configure({ adapter: new Adapter() })
describe('DetailsCostumer', () => {
const clickFn = jest.fn();
it ('should render correctly <DetailsCostume ./> component', ()=> {
const wrapper = mount(<DetailsCostumer isDuplicated={true} />)
expect(wrapper).toMatchSnapshot();
wrapper.unmount()
})
it('should render correctly <DetailsCostumer /> when is duplicated', () => {
const wrapper = mount(<DetailsCostumer isDuplicated={true} />)
expect(wrapper.contains(<label>Code mobile</label>)).toEqual(true)
wrapper.unmount()
})
it('should click send details button', () => {
const wrapper = mount(<DetailsCostumer isDuplicated={true} />)
wrapper.instance().handleSendDetails = clickFn
wrapper.find('button#sendDetails').simulate('keydown', { keyCode: 32 });
expect(wrapper).toMatchSnapshot();
wrapper.unmount()
})
})
我的组件:
DetailCustomer.js
export class DetailsCostumer extends Component {
componentDidUpdate(prevProps) {
if (this.props.signUpFinished) {
this.props.history.push('/Signup/EmailConfirmation')
}
if (this.props.isDuplicated && (prevProps.isDuplicated !== this.props.isDuplicated)) {
let userFormToUpdate = { ...this.state.userForm }
userFormToUpdate['codeMobile'] = {
value: '',
validation: {
required: true
},
valid: false,
touched: false
}
this.setState({ formIsValid: false, userForm: userFormToUpdate })
this.refs.btnRegister.removeAttribute("disabled")
const domNode = ReactDOM.findDOMNode(this.codeMobileElement)
domNode.scrollIntoView()
}
}
render() {
let codeMobile = null
if (this.props.isDuplicated) {
codeMobile = (
<div className="flex-row">
<label>
Code mobile
</label>
<input min="0"
type="number"
name="codeMobile"
ref={el => this.codeMobileElement = el}
onChange={this.handleInputs} />
</div>)
}
return (
<div id="login4" >
<div className="modal-content">
<ModalError
errorMessage={this.props.errorMessage}
showModal={this.props.error}
closeModal={this.props.onCloseModalError}
styleContent={{ backgroundColor: 'gainsboro' }}
style={{ zIndex: '99999', position: 'absolute', margin: '5% 20%' }} />
<ModalSuccess
showModal={this.props.showModalIsDuplicated}
message={'Please check your phone, we sent a code mobile to confirm your register.'}
closeModal={this.props.onCloseCodeMobileModal} />
<button onClick={this.handleCloseDetailConstumer}
title="Close (Esc)"
type="button"
className="mfp-close lb-fechar fa fa-times" />
<div className="lb-padrao-singup4 mfp-content-login">
<img src={require('../../../img/logo-lunch-team.svg')} alt="Lunch Team" />
<h3 className="b">Please provide</h3>
<div>
<label>
First name
</label>
<input className={!this.state.userForm['firstName'].valid && this.state.userForm['firstName'].touched ?
'Invalid' : null}
type="text"
name="firstName"
onChange={this.handleInputs} />
<label>
Last name
</label>
<input className={!this.state.userForm['lastName'].valid && this.state.userForm['lastName'].touched ?
'Invalid' : null}
type="text"
name="lastName"
onChange={this.handleInputs} />
<div className="flex-row">
<label>
Mobile
</label>
<input className={!this.state.userForm['mobile'].valid && this.state.userForm['mobile'].touched ?
'Invalid' : null}
min="0"
type="number"
name="mobile"
onChange={this.handleInputs} />
</div>
<div className="flex-row">
<label>
Phone
</label>
<input min="0"
type="number"
name="phone"
onChange={this.handleInputs} />
</div>
{codeMobile}
<div>
<input style={{ display: 'inline-flex' }}
onChange={evt => this.toggleSubscribeCheckbox(evt)}
type="checkbox"
name="contact"
id="contact1" />
<label htmlFor="delivery1">
<b>I would like the day's restaurant list sent to my inbox</b>
</label>
</div>
<div className="m-center">
<button
id="sendDetails"
disabled={!this.state.formIsValid}
ref="btnRegister"
className={!this.state.formIsValid ?
'btn orange-bold btn-register btn-disabled' :
'btn orange-bold btn-register'}
onClick={this.handleSendDetails}>Register</button>
</div>
</div>
</div>
</div>
</div>)
}
}
结果: