I am getting this error:
Error: ShallowWrapper::state() can only be called on the root
Here is my test:
it('default "errors" state is an empty object', () => {
const router = {'react-router': {}};
const wrapper = shallow(<IntlProvider><SignUpForm router={router} /></IntlProvider>);
expect(wrapper.find(SignUpForm).state('errors')).to.equal({});
});
Would anyone be able to tell me how I can access/test the state of the SignUpForm component?
I think #431 might be relevant here.
You want wrapper.dive() so that it re-shallow-renders the SignUpForm.
I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use dive() but there is no similar method for mounted components. This is the solution I did:
const wrapper = mount(shallow(<MyComponent />).get(0))
Works like a charm :)
@dabit1 Thank you very much your tip worked like a charm for me too
@dabit1 When I do that I just get a bunch of errors:
Warning: Failed prop type: Invalid prop 'Component' supplied toWrapperComponent.
Error: Uncaught [TypeError: Cannot read property 'hasOwnProperty' of null][..]
Hi!
I can verify that it still works in 2019 (in contrast of the react intl team's enzyme + jest tutorial which doesn't :( )
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-intl": "^2.9.0",
"jest": "^24.8.0",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0"
Most helpful comment
I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use
dive()but there is no similar method for mounted components. This is the solution I did:Works like a charm :)