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

Can't find React Component. #536

Closed
windyGex opened this issue Aug 11, 2016 · 5 comments
Closed

Can't find React Component. #536

windyGex opened this issue Aug 11, 2016 · 5 comments

Comments

@windyGex
Copy link

Consider the following code,Why wrapper can't find Content

it will be error expected 0 to equal 1.

import React from 'react';
import {mount} from 'enzyme';
import ReactDOM from 'react-dom';
import expect from 'expect.js';

class Content extends React.Component {
    render() {
        return <span>content</span>
    }
}

class Gateway extends React.Component {
    render() {
        return null
    }

    componentDidMount() {
        this.renderOverlay()
    }

    componentDidUpdate() {
        this.renderOverlay()
    }

    renderOverlay() {
        if (!this.wrapper) {
            this.wrapper = document.createElement('div');
            document.body.appendChild(this.wrapper);
        }
        ReactDOM.unstable_renderSubtreeIntoContainer(this, this.props.children, this.wrapper);
    }
}

class App extends React.Component {
    render() {
        return <Gateway><Content/></Gateway>
    }
}

describe('test', () => {
    it('shoud support find Content', () => {
        const wrapper = mount(<App/>)
        expect(wrapper.find(Gateway).length).to.equal(1);
       // Will be error 
        expect(wrapper.find(Content).length).to.equal(1);
    });
})
@travisperson
Copy link

travisperson commented Aug 11, 2016

I ran into this issue when using React Bootstrap and their modals / portals. My understanding is that Enzyme only looks for rendered children. In this case Gateway is not rendering anything.

Instead I had to grab the child from the props.

const dialog = wrapper.find('Portal');·                                                                          
const confirmDialog = new ReactWrapper(dialog.node.props.children);

Note: dialog.prop('children') may also work for this, didn't test that.

@aweary
Copy link
Collaborator

aweary commented Aug 12, 2016

Yeah I'm pretty sure using renderSubtreeIntoContainer is not going to work with enzyme out of the box. In most cases you'll have to find out of the component holds a reference to the portal and see if you can use that.

@windyGex in your example it doesn't seem like anything is calling the renderOverlay method so it seems like it's working as expected.

Closing as this is essentially a duplicate of #252

@aweary aweary closed this as completed Aug 12, 2016
@alexdong
Copy link

I've been searching for the right solution for quite a while and @travisperson's solution is the only one that works with Enzyme. Just in case others are looking for some solutions.

@oliviertassinari
Copy link

oliviertassinari commented Sep 27, 2017

@travisperson's solution seems to no longer work with enzyme v3.

My bad it does :).

@davetorre
Copy link

@travisperson 's solution worked for me once I changed node to getNode(). Last line becomes const confirmDialog = new ReactWrapper(dialog.getNode().props.children).

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

No branches or pull requests

6 participants