What is the proper way to test a method within a react component, in this case componentDidMount. I want to test the setTimeOut function inside the compoenent. Should I use stub? For example the code below:
componentDidMount() {
setTimeout(() => this.setState({ isOpen: true }), 1);
}
How can I test the setTimeout being called?
I tried with the following and didn't work. What am I missing?
my imports:
import test from 'ava';
import React from 'react';
import { ad } from 'components/Ad/Ad';
import { shallow, mount } from 'enzyme';
import { stub } from 'sinon';
import { expect } from 'chai';
import sinon from 'sinon';
let info;
test.beforeEach(() => {
info = shallow(<ad {...props} />)
});
test('is active: true after mounting', done => {
info.instance().componentDidMount()
setTimeout(() => {
info.state('active').should.be.true <--line:42
done()
}, 0)
})
I get the following error: TypeError: Cannot read property 'be' of undefined null._onTimeout (test/Components/Ad.unit.js:42:5)