最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - update state of component using jest - Stack Overflow

programmeradmin1浏览0评论

I am trying to update the state of my ponent in jest.

What I am trying to do is check that when the state value of updated is set to true then the new props should not change the value of the state. From answers I have read on here I assumed I could do the following to update the state of a ponent.

Game.setState({
    updated: true
});

Here is my code below.

const props = {
  goals: "0"
};

it("should should not update the state of goals when the value prop is changed if updated value in state is set to true", () => {
    renderer.render(<Game {...props} />, div);

    Game.setState({
        updated: true
    });

    GameponentWillReceiveProps({
        goals: "2"
    });

    expect(Game.state.goals).toBe("0");    
});

I am trying to update the state of my ponent in jest.

What I am trying to do is check that when the state value of updated is set to true then the new props should not change the value of the state. From answers I have read on here I assumed I could do the following to update the state of a ponent.

Game.setState({
    updated: true
});

Here is my code below.

const props = {
  goals: "0"
};

it("should should not update the state of goals when the value prop is changed if updated value in state is set to true", () => {
    renderer.render(<Game {...props} />, div);

    Game.setState({
        updated: true
    });

    Game.ponentWillReceiveProps({
        goals: "2"
    });

    expect(Game.state.goals).toBe("0");    
});
Share Improve this question edited Aug 22, 2017 at 13:24 Andreas Köberle 111k58 gold badges280 silver badges307 bronze badges asked Aug 22, 2017 at 9:24 peter flanaganpeter flanagan 9,85027 gold badges82 silver badges140 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

you can work with the instance

const gameRender = renderer.create(<Game {...props} />)
const gameInstance = gameRender.getInstance()

gameInstance.setState({ updated: true })

etc etc. your code references the constructor, not the instance.

发布评论

评论列表(0)

  1. 暂无评论