How to test a prop update on React component

前端 未结 8 552
鱼传尺愫
鱼传尺愫 2020-12-14 05:36

What is the correct way of unit testing a React component prop update.

Here is my test fixture;

describe(\'updating the value\', function(){
                 


        
8条回答
  •  伪装坚强ぢ
    2020-12-14 06:20

    If you re-render the element with different props in the same container node, it will be updated instead of re-mounted. See React.render.

    In your case, you should use ReactDOM.render directly instead of TestUtils.renderIntoDocument. The later creates a new container node every time it is called, and thus a new component too.

    var node, component;
    beforeEach(function(){
        node = document.createElement('div');
        component = ReactDOM.render(, node);
    });
    
    it('should update the state of the component when the value prop is changed', function(){
        // `component` will be updated instead of remounted
        ReactDOM.render(, node);
        // Assert that `component` has updated its state in response to a prop change
        expect(component.state.value).toBe(false);
    });
    

提交回复
热议问题