У меня возникла проблема с тестированием формы submit
с использованием React, TestUtils и Jest.Подача заявки на тестовое оформление с помощью Jest & TestUtils
У меня есть компонент, который отображает элемент DOM <form>
; тот же компонент также имеет метод, который обрабатывает событие onSubmit
и регистрирует инструкцию. Моя цель - высмеять обработчик 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()
Вопросы, относящиеся: