I'm not an expert about unit testing, and i'm trying to achieve 100% coverage on my dummy todoapp project, its easy for simple components like a TodoList component, but what about the AddTodo component?
import React, {PropTypes} from 'react'
import {compose, withState, withProps} from 'recompose'
/**
* Form to create new todos.
*/
const enhance = compose(
withState('value', 'setValue', ''),
withProps(({value, setValue, addTodo}) => ({
handleSubmit: e => (
e.preventDefault(),
addTodo(value),
setValue('')
),
handleChange: e => setValue(e.target.value),
}))
)
const Component = ({value, handleSubmit, handleChange}) =>
<form onSubmit={handleSubmit}>
<input
type="text"
value={value}
onChange={handleChange}
/>
<input type="submit" value="add"/>
</form>
Component.displayName = 'FormNewTodo'
Component.propTypes = {
value: PropTypes.string.isRequired,
handleSubmit: PropTypes.func.isRequired,
handleChange: PropTypes.func.isRequired,
}
export default enhance(Component)
This is my current AddTodo test:
import React from 'react'
import {shallow} from 'enzyme'
import FormNewTodo from './index'
test('it should render properly', () => {
const wrapper = shallow(<FormNewTodo value="something"/>)
expect(wrapper).toMatchSnapshot()
})
That test give produce the following coverage: Stmts 62.5, Branch 100, Funcs 25, Lines 62.5.
The uncovered Lines are: 12, 16, 21.
How should I test them correctly? What i'm missing? There are some resources out there about the topic?
I've finally solved my problem, note that the goal was to achieve 100% coverage and nothing else.
This is my solution:
import React from 'react'
import {shallow} from 'enzyme'
import FormNewTodo from './index'
test('<FormNewTodo/>', () => {
const preventDefault = jest.fn()
const addTodo = jest.fn()
const subject = shallow(
<FormNewTodo
addTodo={addTodo}
/>
)
subject.dive()
.find('[type="text"]')
.simulate('change', {target: {value: 'woot'}})
subject.dive()
.simulate('submit', {preventDefault})
expect(preventDefault).toHaveBeenCalled()
expect(addTodo).toHaveBeenCalled()
})
The
handleSubmitandhandleChangefunctions are not called, so the coverage report says these lines are not covered.Because you've already
enzyme, you can use it to simulate the events which trigger those handlers.For example: