Skip to content

Instantly share code, notes, and snippets.

@jstejada
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstejada/904b94e34f352d5933fe to your computer and use it in GitHub Desktop.
Save jstejada/904b94e34f352d5933fe to your computer and use it in GitHub Desktop.
Testing handlers for events with TestUtils.Simulate failing
React = require 'react/addons'
TestUtils = React.addons.TestUtils
# Component
Comp = React.createClass(
handleSubmit: (e)->
@props.handleSubmit()
handleInputChange: (e)->
render: ->
React.DOM.form onSubmit: @handleSubmit,
React.DOM.input type: "text", onChange: @handleInputChange
)
# Test
ddescribe "Comp", ->
beforeEach ->
@submitHandler = jasmine.createSpy "submitHandler"
@comp = TestUtils.renderIntoDocument React.createFactory(Comp)(handleSubmit: @submitHandler)
it 'assigns callbacks correctly', ->
form = TestUtils.findRenderedDOMComponentWithTag @comp, "form"
input = TestUtils.findRenderedDOMComponentWithTag @comp, "input"
expect(form.props.onSubmit).toEqual @comp.handleSubmit # SUCCEEDS
expect(input.props.onChange).toEqual @comp.handleInputChange # FAILS
it 'calls handleSubmit when form submitted', ->
spyOn(@comp, "handleSubmit").and.callThrough()
form = TestUtils.findRenderedDOMComponentWithTag @comp, "form"
TestUtils.Simulate.submit form.getDOMNode()
expect(@submitHandler).toHaveBeenCalled() # SUCCEEDS
expect(@comp.handleSubmit).toHaveBeenCalled() # FAILS
it 'calls handleInputChange when input changes', ->
spyOn(@comp, "handleInputChange").and.callThrough()
input = TestUtils.findRenderedDOMComponentWithTag @comp, "input"
TestUtils.Simulate.change input.getDOMNode()
expect(@comp.handleInputChange).toHaveBeenCalled() # FAILS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment