Answer the question
In order to leave comments, you need to log in
Why doesn't Simulate.click fire in TestUtils?
I'm playing with tests (karma, mocha, chai) and can't figure out why click emulation doesn't work. What am I doing wrong?
Test:
import React from 'react'
import TestUtils from 'react-addons-test-utils'
import Home from '../app/views/home/home'
describe('home page', () => {
let home
beforeEach(() => {
home = TestUtils.renderIntoDocument(<Home />)
})
it('should be abble to hide the content by clicking', () => {
let title = TestUtils.scryRenderedDOMComponentsWithClass(home, 'title')
TestUtils.Simulate.click(home)
expect(title.length).to.equal(0)
})
})
import React, { Component } from 'react'
class Home extends Component {
constructor (props) {
super(props)
this.state = {
show: true
}
}
handleClick () {
this.setState({
show: !this.state.show
})
}
render () {
let getTitle = () => {
return this.state.show ? (
<h1 className='title'>Home page</h1>
) : ''
}
return (
<div className='page' onClick={this.handleClick}>
{getTitle()}
</div>
)
}
}
export default Home
Answer the question
In order to leave comments, you need to log in
Found errors. The click does not work, because you need to "click" on the node, and not on the composite component. Because this code:
let title = TestUtils.scryRenderedDOMComponentsWithClass(home, 'title')
TestUtils.Simulate.click(home)
expect(title.length).to.equal(0)
let homeNode = TestUtils.findRenderedDOMComponentWithTag(home, 'div')
TestUtils.Simulate.click(homeNode)
title = TestUtils.scryRenderedDOMComponentsWithClass(home, 'title')
expect(title.length).to.equal(0)
<div className='page' onClick={this.handleClick.bind(this)}>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question