Skip to content

Instantly share code, notes, and snippets.

@herbowicz
Created February 22, 2018 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herbowicz/d836c6da77b294da337f5dcf544033e9 to your computer and use it in GitHub Desktop.
Save herbowicz/d836c6da77b294da337f5dcf544033e9 to your computer and use it in GitHub Desktop.
Jest and Enzyme - shallow rendering
import React from 'react';
const Bar = (props) => (
<div className="bar">
This is Bar
</div>
);
export default Bar;
import React from 'react';
import Bar from './Bar';
const Foo = (props) => (
<div className="foo">
<Bar/>
</div>
);
export default Foo;
import React from 'react';
import {mount, shallow} from 'enzyme';
import Foo from '../Foo';
describe('A suite', function() {
it('has one child component', function() {
expect(mount(<Foo/>).children.length).toEqual(1);
});
it('has class "foo"', function() {
expect(mount(<Foo/>).find('.foo').length).toBe(1);
});
it('does not have class "bar"', function() {
expect(mount(<Foo/>).find('.bar').length).toBe(0); // test failed using mount
});
it('does not have class "bar"', function() {
expect(shallow(<Foo/>).find('.bar').length).toBe(0); // test passed with shallow rendering
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment