Last active
October 17, 2016 07:12
-
-
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
This file contains 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 { | |
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 | |
}) | |
}) |
This file contains 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' | |
export default class WrapOne extends React.Component { | |
static propTypes = { | |
children: React.PropTypes.node, | |
}; | |
render () { | |
return ( | |
<div> | |
{this.props.children} | |
</div> | |
) | |
} | |
} |
This file contains 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 { | |
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) | |
}) | |
}) |
This file contains 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' | |
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