Skip to content
Ben edited this page Nov 5, 2015 · 11 revisions
  1. Find
  2. SetState
  3. Simulate
  4. renderToString

Importing bundled middleware is easy:

import {Find} from 'legit-tests/middleware'

###Find Finds dom nodes and adds them to the helpers array

  • .find('div') finds all divs
  • .find('.test') finds all elements with the class test
  • .find(MyComponent) finds all React Components of that class

Inside the test method it is accessible via this.helpers.elements.tagName

.use(Find, 'div')
.test(function({div}) {
  this.helpers.elements.div //array of divs, or a single element if there's only one
  this.helpers.elements.mycomponent //lowercases component names
  div.props.children //if you're only doing .find, you can access the elements this way
})

###SetState changes the component's state

.setState({test: 'changed!'})
.test(function() {
  expect(this.component.state.test).to.be.equal('changed!')
})

###Simulate Simulates events on elements

arguments Object

method - Simulate method to be called

element - element name from this.helpers.elements

options - options to be passed to Simulate method

example

let spy = sinon.spy();

Test(<TestComponent onClick={spy}/>)
.use(Find, 'button')
.simulate({method: 'click', element: 'button'})
.test(function() {
    expect(spy.called).to.be.true;
})
let spy = sinon.spy();

Test(<MyComponent />)
.find('.thumbs-up-button')
.simulate({ method: 'click', element: 'thumbs-up-button' })
.test(() => {
    expect(spy.called).to.be.true;
})  

###renderToString

Returns the rendered version of the component as a string

Test(<TestComponent test="wow"/>)
.renderToString(string => {
  expect(string).to.match(/Click Me/)
})
Clone this wiki locally