Skip to content

Instantly share code, notes, and snippets.

@CurtisHumphrey
Last active October 17, 2016 07:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CurtisHumphrey/c0788ac43cac234cdd3b to your computer and use it in GitHub Desktop.
Save CurtisHumphrey/c0788ac43cac234cdd3b to your computer and use it in GitHub Desktop.
Testing a React component that is a wrapper with enzyme with chai-enzymes
import React from 'react'
import {
shallow,
} from 'enzyme'
import WrapMany from './WrapMany'
describe('<WrapMany />', () => {
let sandbox
beforeEach(() => {
sandbox = sinon.sandbox.create()
sandbox.stub(console, 'error', (message) => {
throw new Error(message)
})
})
afterEach(() => {
sandbox.restore()
})
it('with normal props it should render without errors', () => {
const wrapper = shallow(<WrapMany />)
expect(wrapper).to.exist
})
it('should render a child when passed to it', () => {
const node = <div>Hi</div>
const wrapper = shallow(<WrapMany>{node}</WrapMany>)
expect(wrapper.contains([node, node])).to.eql(true) // cannot be done with chai-enyzme
})
})
import React from 'react'
export default class WrapOne extends React.Component {
static propTypes = {
children: React.PropTypes.node,
};
render () {
return (
<div>
{this.props.children}
</div>
)
}
}
import React from 'react'
import {
shallow,
} from 'enzyme'
import WrapOne from './WrapOne'
describe('<WrapOne />', () => {
let sandbox
beforeEach(() => {
sandbox = sinon.sandbox.create()
sandbox.stub(console, 'error', (message) => {
throw new Error(message)
})
})
afterEach(() => {
sandbox.restore()
})
it('with normal props it should render without errors', () => {
const wrapper = shallow(<WrapOne />)
expect(wrapper).to.exist
})
it('should render a child when passed to it', () => {
const node = <div>Hi</div>
const wrapper = shallow(<WrapOne>{node}</WrapOne>)
expect(wrapper).to.contain(node)
})
})
import React from 'react'
export default class WrapOne extends React.Component {
static propTypes = {
children: React.PropTypes.children,
};
render () {
return (
<div>
{this.props.children}
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment