我遇到問題,使用React,TestUtils和Jest測試表單submit
事件。測試反應表單提交使用Jest&TestUtils
我有一個組件呈現<form>
DOM元素;同一個組件也有一個方法來處理事件並記錄一條語句。我的目標是嘲笑onSubmit
處理程序並聲明它被調用。
形狀component.cjsx
module.exports = React.createClass
# Handle form submissions
handleSubmit: (e) ->
console.log 'Make async call'
# Render a form
render: ->
<form onSubmit={@handleSubmit}>
<input type="submit" />
</form>
__tests __/test-form-component.coffee
jest
.dontMock '../form-component'
React = require 'react/addons'
TestUtils = React.addons.TestUtils
FormComponent = require '../form-component'
describe 'FormComponent', ->
it 'creates a log statement upon form submission', ->
# Render a FormComponent into the dom
formInstance = TestUtils.renderIntoDocument(<FormComponent />)
# Mock the `handleSubmit` method
formInstance.handleSubmit = jest.genMockFunction()
# Simulate a `submit` event on the form
TestUtils.Simulate.submit(formInstance)
# TestUtils.Simulate.submit(formInstance.getDOMNode()) ???
# I would have expected the mocked function to have been called
# What gives?!
expect(formInstance.handleSubmit).toBeCalled()
相關問題: