我正在尝试为编写的简单组件编写单元测试。
这是我的组件:
const ErrorWrpper = (props) =>(
<div className={props.class} style={props.validateForm(props.inputType)?
{display: 'none'}:{}}>
<span>{props.message}</span></div>
)
export default ErrorWrpper;
这是我的测试:
import React from 'react';
import { expect } from '../../test_helper';
import { shallow } from 'enzyme';
import { it, describe, beforeEach } from 'mocha';
import ErrorWrapper from '../../../src/app/components/login/ErrorWrapper';
let errorWrapper;
describe("ErrorWrapper component unit tests", () => {
function validateForm(test){
}
before(() => {
errorWrapper = shallow(<ErrorWrapper class="test" inputType="all" validateForm={this.validateForm}/>);
});
// these tests check that each className that should exist, exists
describe("className check", () => {
it('should have className test', () => {
expect(errorWrapper.find('test')).to.exist;
});
})
})
现在当我运行它时,我得到了这个:
ErrorWrapper component unit tests "before all" hook:
TypeError: Cannot read property 'validateForm' of undefined
如您所见,我正在尝试将 validateError 函数作为道具提供,但仍然出现错误。任何想法?