Skip to content

Instantly share code, notes, and snippets.

@paulsturgess
Last active October 8, 2018 11:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsturgess/1cc50c28c6a0b622f9fddceb4d268c41 to your computer and use it in GitHub Desktop.
Save paulsturgess/1cc50c28c6a0b622f9fddceb4d268c41 to your computer and use it in GitHub Desktop.
Testing a stateless component
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