Reputation: 126
Foo.jsx
import React from 'react';
export default (props) => {
let {val} = props;
if (val) {
return 'ReactComponentFail';
} else {
return 'ReactComponent';
}
};
Foo.test.js
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import Foo from './Foo';
Enzyme.configure({ adapter: new Adapter() });
let wrapper;
describe('foo test', () => {
let props = { val: 0 }
wrapper = shallow(<Foo {...props} />);
test('component test', () => {
expect(wrapper.text()).toEqual('ReactComponent');
});
});
How can i mock props
? so, that i can get 100% code coverage any help would be highly appreciated.
I can't find anything on google regarding how to mock props.
Upvotes: 1
Views: 9668
Reputation: 102237
Create mocked props
and pass it to your SFC.
E.g.
foo.jsx
:
import React from 'react';
export default (props) => {
let { val } = props;
if (val) {
return 'ReactComponentFail';
} else {
return 'ReactComponent';
}
};
foo.test.jsx
:
import React from 'react';
import { shallow } from 'enzyme';
import Foo from './foo';
describe('foo test', () => {
test('should return ReactComponent', () => {
let props = { val: 0 };
let wrapper = shallow(<Foo {...props} />);
expect(wrapper.text()).toEqual('ReactComponent');
});
test('should return ReactComponentFail', () => {
let props = { val: 1 };
let wrapper = shallow(<Foo {...props} />);
expect(wrapper.text()).toEqual('ReactComponentFail');
});
});
unit test result with 100% coverage:
PASS stackoverflow/62694921/foo.test.jsx (10.41s)
foo test
✓ should return ReactComponent (6ms)
✓ should return ReactComponentFail
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
foo.jsx | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 11.845s, estimated 12s
Upvotes: 3