【发布时间】:2015-01-22 18:50:01
【问题描述】:
我在使用 React、TestUtils 和 Jest 测试表单 submit 事件时遇到问题。
我有一个渲染<form> DOM 元素的组件;同一个组件还有一个处理onSubmit 事件并记录语句的方法。我的目标是模拟 onSubmit 处理程序并断言它已被调用。
form-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()
相关问题:
【问题讨论】:
标签: coffeescript reactjs jestjs reactjs-testutils