Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: ShallowWrapper::state() can only be called on the root #814

Closed
joetidee opened this issue Feb 17, 2017 · 6 comments
Closed

Error: ShallowWrapper::state() can only be called on the root #814

joetidee opened this issue Feb 17, 2017 · 6 comments

Comments

@joetidee
Copy link

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?

@ljharb
Copy link
Member

ljharb commented Feb 17, 2017

I think #431 might be relevant here.

@ljharb
Copy link
Member

ljharb commented Sep 26, 2017

You want wrapper.dive() so that it re-shallow-renders the SignUpForm.

@dabit1
Copy link

dabit1 commented Feb 1, 2018

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 :)

@jumpinjan
Copy link

@dabit1 Thank you very much your tip worked like a charm for me too

@paddotk
Copy link

paddotk commented Aug 15, 2018

@dabit1 When I do that I just get a bunch of errors:
Warning: Failed prop type: Invalid prop 'Component' supplied to WrapperComponent.
Error: Uncaught [TypeError: Cannot read property 'hasOwnProperty' of null][..]

@rnagy90
Copy link

rnagy90 commented Oct 2, 2019

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants