2017-09-15 188 views
1

我是React和测试的新手,因此原谅了这个问题的天真性。我有一个React表单组件,其输入上的onChance运行功能handleChange。试图用Jest来测试它,但不能使它工作。React Js中的Jest测试函数

这里的登录组件:

class Login extends React.Component { 

    constructor() { 
    super(); 
    this.state = {username: '', password: ''} 
    this.disableSubmit = this.disableSubmit.bind(this); 
    this.handleChange = this.handleChange.bind(this); 
    } 

    handleChange(e) { 
    this.setState({ 
     [e.target.name]: e.target.value 
    }); 
    } 

    render() { 

    return(
     <div className="login"> 
     <form> 
      <h3 className="login__title">LOGIN</h3> 
      <div className="input-group"> 
      <input onChange={this.handleChange} value={this.state.username} className="form-control login__input username" type="text" placeholder="user name" name={'username'} autoFocus/> 
      </div> 
      <div className="input-group"> 
      <input onChange={this.handleChange} value={this.state.password} className="form-control login__input password" type="password" placeholder="password" name={'password'}/> 
      </div> 
      <div> 
      <button className="btn btn-primary btn-block login__button" type="submit">Login</button> 
      </div> 
     </form> 
     </div> 

    ) 
    } 
} 

export default Login; 

这里是我的测试:

import React from 'react' 
import { shallow, mount } from 'enzyme' 
import { shallowToJson } from 'enzyme-to-json' 


import {Login} from '../../../src/base/components/index' 


describe('Given the Login component is rendered',() => { 

    describe('Snapshots',() => { 
    let component 

    beforeEach(() => { 
     component = shallow(<Login />) 
    }) 

    it('should be as expected',() => { 
     expect(shallowToJson(component)).toMatchSnapshot() 
    }) 
    }) 

}) 


test('Submitting the form should call handleSubmit',() => { 

    const startState = {username: ''}; 
    const handleChange = jest.fn(); 
    const login = mount(<Login />); 
    const userInput = login.find('.username'); 

    userInput.simulate('change'); 

    expect(handleChange).toBeCalled(); 

}) 

快照测试通过罚款,但在这最后一次尝试我的功能测试失败:

TypeError: Cannot read property 'target' of undefined 

猜猜我需要传递一些东西给函数?有点困惑!

在此先感谢您的帮助。

UPDATE:

改变了如下测试,但测试失败:expect(jest.fn()).toBeCalled() Expected mock function to have been called.

测试更新:

test('Input should call handleChange on change event',() => { 

    const login = mount(<Login />); 
    const handleChange = jest.spyOn(login.instance(), 'handleChange'); 
    const userInput = login.find('.username'); 
    const event = {target: {name: "username", value: "usertest"}}; 

    userInput.simulate('change', event); 

    expect(handleChange).toBeCalled(); 

}) 
+0

'handleChange'目前没有被模拟。 –

回答

0

找到了解决办法在这里:Enzyme simulate an onChange event

test('Input should call handleChange on change event',() => { 

    const event = {target: {name: 'username', value: 'usertest'}}; 
    const login = mount(<Login />); 
    const handleChange = jest.spyOn(login.instance(), 'handleChange'); 
    login.update(); // <--- Needs this to force re-render 
    const userInput = login.find('.username'); 

    userInput.simulate('change', event); 

    expect(handleChange).toBeCalled(); 

}) 

它为了工作需要这种login.update();

感谢大家的帮助!

0

是的,你需要一个事件对象传递给你simulate功能。

const event = {target: {name: "special", value: "party"}}; 

    element.simulate('change', event); 

编辑:哦,你也需要做这样的事情:

jest.spyOn(login.instance(), 'handleChange') 

但是这无关你的错误

0

handleChange当前没有被嘲笑。几种方法:

通过更改事件处理程序作为prop到Login组件。

<div className="input-group"> 
    <input 
    onChange={this.props.handleChange} 
    value={this.state.username} 
    className="form-control login__input username" 
    type="text" 
    placeholder="user name" 
    name={'username'} 
    autoFocus 
    /> 
</div> 

login.spec.js

... 
const handleChange = jest.fn(); 
const login = mount(<Login handleChange={handleChange}/>); 
... 

与模拟功能替换handleChange。

... 
const handleChange = jest.fn(); 
const login = mount(<Login />); 
login['handleChange'] = handleChange // replace instance 
... 
expect(handleChange).toBeCalled(); 

用开玩笑spyOn,以创建一个包装的原始功能的模拟功能。

... 
const handleChange = jest.spyOn(object, 'handleChange') // will call the original method 
expect(handleChange).toBeCalled(); 

与模拟功能的登录组件替换handleChange。 ... const handleChange = jest.spyOn(object,'handleChange')。mock //将调用原始方法 expect(handleChange)。被称为();

相关问题