Ad
How To Mock Inline Refs In Jest & Enzyme
I am new to jest and enzyme and I am trying to mock the refs of a component. I have seen many solutions but none seems to be related they way I created the refs.
my ref declaration :
<input type=email ref={ (input) => { this.email = input; } } />
and later when I am using ref in my component as :
this.email.focus();
, i am getting error in test case : focus of undefined
.
Test Case:
it('details', () => {
wrapper = shallow(<Componentes />);
const instance = wrapper.instance();
wrapper.email = {
getRenderedComponent: jest.fn(() => ({
focus: jest.fn
}))
};
wrapper.find('.signin-button').simulate('click');
});
Here I tries implementing one of the solution to my test case but that does not seem to be working. Any help on this would be greatly appreciated.
Thanks.
Ad
Answer
You'll want to add the mock to the instance of the component itself, like so:
it('details', () => {
wrapper = shallow(<Componentes />);
const instance = wrapper.instance();
instance.email = { focus: jest.fn() };
wrapper.find('.signin-button').simulate('click');
});
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad