How to spy componentWillMount using jest and enzyme

后端 未结 5 1524
無奈伤痛
無奈伤痛 2020-12-31 13:07

I am trying to test whether componentWillMount was called and for that my test is

test(\'calls `componentWillMount` before rendering\', () => {
  let fn =         


        
5条回答
  •  青春惊慌失措
    2020-12-31 13:40

    I would first spy on the component's componentWillMount method but also use .and.CallThrough() to prevent it from mocking its contents. Hope this helps:

    it('should check that the componentWillMount method is getting called', () => {
        spyOn(SomeComponent.prototype, 'componentWillMount').and.callThrough();
    
        const wrapper = mount();
    
        expect(wrapper).toBeDefined();
        expect(SomeComponent.prototype.componentWillMount).toHaveBeenCalledTimes(1);
    });
    

提交回复
热议问题