groovy
groovy

Reputation: 275

Get updated state of component when calling method on class via enzyme

How can I get my test to reflect the new state of my component after calling updateItem on it. I did try rendered.update() but I must be missing on how something works. Thanks.

Method on class:

updateItem (id) {
    return updateItem(id)
    .then((result) => {
        this.setState({
            item: {blah:'blah'}
        });
    }, (error) => {
        throw error;
    });
}

Test:

it("should update item accordingly", () => {
    const rendered = shallow(React.createElement(Item));
    const result = rendered.instance().updateItem();
    expect(rendered.state('item')).toEqual({blah:'blah'});
})

Upvotes: 2

Views: 183

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 111102

You have to get the function from the childs prop and call it. As it returns a promise you need to use async/await or return the promise from your test, have a look at docs for more infos. Then test the state of the rendered component.

it("should update item accordingly", async() => {
    const rendered = shallow(React.createElement(Item));
    await rendered.find('someChild).prop('updateItem')('someId')
    expect(item.state).toEqual({
      item: {blah:'blah'}
    })
})

Upvotes: 1

Related Questions