I've got problem with testing react component. Here's my code.
// Component textfield-input.jsx
import React, { PropTypes } from 'react'
import HelpComponent from '../../atoms/help-component'
export default function TextField(props) {
const { helpMessage, label, error, ...rest } = props
return (
<div className="text-field">
<label htmlFor={props.name} className="name-label">
{label}
{helpMessage ?
<HelpComponent message={helpMessage} /> : null
}
</label>
{props.required ? <span className="dot">*</span> : null}
<input className={`input ${error ? 'error' : ''}`} {...rest} />
{error && (<span className="error-text">{error}</span>)}
</div>
)
}
TextField.propTypes = {
name: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
helpMessage: PropTypes.string,
error: PropTypes.string,
required: PropTypes.bool,
}
// Help Component
import React, { PropTypes } from 'react'
export default function HelpComponent(props) {
const direction = props.direction || 'right'
return (
<i
className="fa fa-question-circle"
data-toggle="tooltip"
data-placement={direction}
title={props.message}
aria-hidden="true"
/>
)
}
HelpComponent.propTypes = {
direction: PropTypes.string,
message: PropTypes.string.isRequired
}
// Test textfield-input.spec.jsx
import React from 'react'
import { shallow } from 'enzyme'
import TextField from '../../src/components/molecules/form/textfield-input'
describe('Unit: TextField component', () => {
let component
let props = {
name: 'text',
label: 'Text',
}
const COMPONENT = <TextField {...props} />
beforeEach(() => {
component = shallow(COMPONENT)
})
it('should be react component', () => {
expect(component.exists()).to.be.true
})
it('should render proper TextField', () => {
expect(component.find('.text-field').exists()).to.be.true
expect(component.find('.name-label').contains(props.label)).to.be.true
expect(component.find(`input[name="${props.name}"]`).exists()).to.be.true
})
it('should render help message', () => {
component.setProps({ helpMessage: 'help message' })
expect(component.find('.fa').exists()).to.be.true
})
})
expect(component.find('.name-label').text()).to.equal(props.label)
expect(component.find('.name-label').contains(props.label)).to.be.true
You are using shallow
rendering, that is why it is failing. When using shallow rendering the child components won't be rendered but will stay as components.
So in your case, it won't go down into the child component and test whether the class exists there, but test immediate nodes in render method and raw components without rendering them.
So if you need to test child components too, you need full dom rendering