Enzyme - Mock non-instance variables

Clash Royale CLAN TAG#URR8PPPEnzyme - Mock non-instance variables
I have the following React Component:
const titleText = translate('header.about');
const About = ({onClose}) => (
<div>
<ModalHeader title={titleText} onClose={onClose} />
<Content />
</div>
);
I also have a test file that checks the onClose functionality, which works fine:
onClose
const spy = sinon.spy();
const wrapper = shallow(<About onClose={spy} />); // I also tried "mount()"
...
assert.equal(spy.callCount, 1, 'onClose was called exactly once');
However, I get the following warning in the terminal when running the test:
Warning: Failed prop type: The prop `title` is marked as required in `ModalHeader`, but its value is `undefined`
I don't get the warning in the browser, only when running the test. While it's only a warning, I'd rather fix this if possible.
Any way to give a mock value to title? I am only interested in possible solutions for mocking local variables (i.e titleText in this example).
title
titleText
I don't think it is possible to mock such variables.
– Hardik Modha
42 mins ago
1 Answer
1
Using jest, you can mock the translate package like so:
// this will mock the translate package
jest.mock('translate', () => () => 'some test title');
const spy = sinon.spy();
const wrapper = shallow(<About onClose={spy} />);
...
assert.equal(spy.callCount, 1, 'onClose was called exactly once');
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why not accept it as a prop?
– Hardik Modha
52 mins ago