Skip to content

Instantly share code, notes, and snippets.

@herbowicz
Created February 22, 2018 22:26
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/2d2c698b5683248408404e61137d4264 to your computer and use it in GitHub Desktop.
Save herbowicz/2d2c698b5683248408404e61137d4264 to your computer and use it in GitHub Desktop.
Jest and Enzyme - shallow rendering
import React from 'react';
const Bar = (props) => (
<div className="hi">
This is Bar
</div>
);
export default Bar;
import React from 'react';
import Bar from './Bar';
const Foo = (props) => (
<div className="hi">
<Bar/>
</div>
);
export default Foo;
import React from 'react';
import {mount, shallow} from 'enzyme';
import Foo from '../Foo';
import Bar from '../Bar';
describe('A suite', function() {
it('has one child component', function() {
expect(mount(<Foo/>).children.length).toEqual(1);
});
it('has one class "hi"', function() {
expect(mount(<Foo/>).find('.hi').length).toBe(1); // test fails with mount
});
it('has one class "hi"', function() {
expect(shallow(<Foo/>).find('.hi').length).toBe(1); // test passed with shallow rendering
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment