2017-09-15 86 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)。被稱爲();

相關問題