Enzyme wrapped.instance() doesn't have react component method property because of .bind(this)

Viewed 16

I have a component which looks like this:

class SomeComponent extends Component {
    constructor() {
        super(){
            this.state = {
               height: 0,
               width: 0
            }
            this.updateDimensions = this.updateDimensions.bind(this);
        }
        componentDidMount(){
            window.addEventListener("resize", this.updateDimensions);
            this.updateDimensions();
        }
        updateDimensions(dimensions) {
             this.setState({
                 height: dimensions.height,
                 width: dimensions.width
             })
        }
     }
}

And I have test for it:

test("dimensions update correctly", () => {
    let wrapper = shallow(<SomeComponent/>)
    let instance = wrapper.instance();
    jest.spyOn(instance, 'updateDimensions');
    instance.componentDidMount();
    expect(instance.updateDimensions).toHaveBeenCalledTimes(1);
})

But I am getting error Cannot spy on updateDimensions property because it is not a function; undefined given instead. All my googled results show this approach to test it but I think the problem is that .bind(this) was used on the method in constructor()

0 Answers
Related