Ale*_*dro 0 javascript ecmascript-6 reactjs jestjs enzyme
我有以下无状态组件:
import React from 'react';
const Input = ({ name, onChange, type }) => (
<input
name={name}
onChange={onChange}
type={type} />
);
export default Input
Run Code Online (Sandbox Code Playgroud)
像这样的util函数:
export const getValue = e => ({
name: e.target.getAttribute('name'),
value: e.target.value
})
Run Code Online (Sandbox Code Playgroud)
我的测试是这样的:
it('should call the onChange for the input element', () => {
const event = {target: {name: "pollName", value: "spam"}};
const input = shallow(<Input onChange={getValue} />)
input.simulate('change', event)
})
Run Code Online (Sandbox Code Playgroud)
我从测试中得到的错误是:
TypeError: e.target.getAttribute is not a function
Run Code Online (Sandbox Code Playgroud)
我没想到通过传递模拟事件会得到这个错误
传递给simulate函数的事件对象没有target.getAttribute函数.您可以在那里添加它:
const attrs = {name: "pollName", value: "spam"}
const event = {target: { getAttribute: name => attrs[name], ...attrs }};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |