我正在使用涉及登录的karma + jasmine对React Components进行单元测试。测试总是抛出错误
? LoginForm test > ValidatedInput test > should validate password
Run Code Online (Sandbox Code Playgroud)
TypeError:this.props.onChange不是一个函数
at ValidatedInput.handleChangeValue (src/components/ValidatedInput.js:14:24)
at node_modules/enzyme/build/ShallowWrapper.js:844:23
at ReactDefaultBatchingStrategyTransaction.perform (node_modules/react-test-renderer/lib/shallow/Transaction.js:143:20)
at Object.batchedUpdates (node_modules/react-test-renderer/lib/shallow/ReactDefaultBatchingStrategy.js:62:26)
at Object.batchedUpdates (node_modules/react-test-renderer/lib/shallow/ReactUpdates.js:97:27)
at ReactShallowRenderer.unstable_batchedUpdates (node_modules/react-test-renderer/lib/shallow/ReactShallowRenderer.js:130:25)
at performBatchedUpdates (node_modules/enzyme/build/ShallowWrapper.js:103:21)
at node_modules/enzyme/build/ShallowWrapper.js:843:13
at withSetStateAllowed (node_modules/enzyme/build/Utils.js:284:3)
at ShallowWrapper.simulate (node_modules/enzyme/build/ShallowWrapper.js:840:42)
at Object.<anonymous> (src/__tests__/login-test.js:93:37)
at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
at process._tickCallback (internal/process/next_tick.js:109:7)
Run Code Online (Sandbox Code Playgroud)
以下是我的文件:
LoginForm.js呈现组件
<ValidatedInput
name={'userId'}
type={'text'}
title={'User ID'}
value={this.state.userId}
placeholder={'Enter User ID'}
onChange={this.handleUserIdChange}
onComponentMounted={this.handleRegisterFormFields}
validations={/^[0-9]{5,10}$/}
validationError={'This is not valid user Id'}
isRequired={true}
/>
Run Code Online (Sandbox Code Playgroud)
我在构造函数中绑定了handleUserIdChange,并在类主体中将其定义为
handleUserIdChange(value) { …Run Code Online (Sandbox Code Playgroud)