Created
April 3, 2017 12:13
-
-
Save keyserfaty/72ce394032871efd8b3b101dfe47b357 to your computer and use it in GitHub Desktop.
Testing some todos with Jest
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 renderer from 'react-test-renderer' | |
import Todos from '../Todos' | |
it('should render an empty component', () => { | |
const tree = renderer.create( | |
<Todos /> | |
).toJSON() | |
expect(tree).toMatchSnapshot() | |
}) | |
it('should render a list of todos', () => { | |
const todos = [ | |
{ | |
text: 'something to do', | |
done: false | |
}, | |
{ | |
text: 'something else', | |
done: false | |
}, | |
] | |
const tree = renderer.create( | |
<Todos todos={todos} /> | |
).toJSON() | |
expect(tree).toMatchSnapshot() | |
}) | |
it('should add a todo to a list of todos', () => { | |
const component = renderer.create( | |
<Todos /> | |
) | |
// Creates a first snapshot | |
let tree = component.toJSON() | |
expect(tree).toMatchSnapshot() | |
// Select input field | |
const todoInputContainer = tree.children.filter(child => child.type === 'div')[0] | |
const todoInput = todoInputContainer.children.filter(child => child.type === 'input')[0] | |
// Select button | |
const todoButton = todoInputContainer.children.filter(child => child.type === 'button')[0] | |
todoInput.props.onChange({ target: { name: 'input', value: 'Some new todo' } }) | |
todoButton.props.onClick() | |
tree = component.toJSON() | |
expect(tree).toMatchSnapshot() | |
}) | |
it('should mark an item as done on item click', () => { | |
const todos = [ | |
{ | |
text: 'something to do', | |
done: false | |
}, | |
{ | |
text: 'something else', | |
done: true | |
}, | |
] | |
const component = renderer.create( | |
<Todos todos={todos} /> | |
) | |
const tree = component.toJSON() | |
expect(tree).toMatchSnapshot() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment