mercredi 15 février 2017

scryRenderedComponentsWithType returns an empty array

I have the following test for a React app:

it('has 7 NavBarItems', () => {
    const component = ReactTestUtils.renderIntoDocument(<App />);
    const navBarItems = ReactTestUtils.scryRenderedComponentsWithType(
        component,
        NavBarItem);

    expect(navBarItems.length).toEqual(7);
});

When the App component is rendered in a non-test environment, a NavBar with 7 NavBarItem child components is created at the top of the screen, to facilitate navigation throughout the app.

The NavBarItem class is as follows:

class NavBarItem extends Component {
    render() {
        return (
            <div id={this.props.id}
                className={"NavBarItem" + (this.props.active ? " active" : "") + (this.props.visible ? "" : " hidden")}
                onClick={e => this.props.navBarClick(e.target.id)}>
                {this.props.title}
            </div>
        );
    }
}

However, the test always fails, because scryRenderedComponentsWithType always returns an empty array, even when I put add calls to jest.dontMock for both App and the file from which NavBarItem is imported. Am I using it wrong, or is what I want just not possible?

Aucun commentaire:

Enregistrer un commentaire