Last active
October 8, 2018 11:22
-
-
Save paulsturgess/1cc50c28c6a0b622f9fddceb4d268c41 to your computer and use it in GitHub Desktop.
Testing a stateless component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import TestUtils from 'react/lib/ReactTestUtils'; | |
import Todo from './Todo'; | |
describe('Todo', () => { | |
let instance, li; | |
let Wrapper = React.createClass({ | |
render: function() { | |
return this.props.children; | |
} | |
}); | |
let onClick = jest.fn(); | |
describe('when completed is true', () => { | |
beforeEach(() => { | |
instance = TestUtils.renderIntoDocument( | |
<Wrapper> | |
<Todo onClick={onClick} completed={true} text='The Todo' /> | |
</Wrapper> | |
) | |
li = TestUtils.findRenderedDOMComponentWithTag(instance, 'li'); | |
}); | |
it('outputs the todo text', () => { | |
expect(li.innerHTML).toEqual('The Todo'); | |
}); | |
it('sets up the onClick', () => { | |
TestUtils.Simulate.click(li) | |
expect(onClick).toHaveBeenCalled(); | |
}); | |
it('sets the text decoration to line-through', () => { | |
expect(li.style.cssText).toEqual('text-decoration: line-through;'); | |
}); | |
}); | |
describe('when completed is false', () => { | |
beforeEach(() => { | |
instance = TestUtils.renderIntoDocument( | |
<Wrapper> | |
<Todo onClick={onClick} completed={false} text='The Todo' /> | |
</Wrapper> | |
) | |
li = TestUtils.findRenderedDOMComponentWithTag(instance, 'li'); | |
}); | |
it('sets the text decoration to none', () => { | |
expect(li.style.cssText).toEqual('text-decoration: none;'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment