I'm using enzyme, sinon and expect to unit test my react component.
import React from 'react';
import expect from 'expect.js';
import { shallow } from 'enzyme';
import ExampleComponent from './../../../src/js/components/example-component';
describe('Test <ExampleComponent />', function() {
beforeEach(function() {
this._sandbox = sinon.sandbox.create();
this.constructorSpy = this._sandbox.spy(ExampleComponent.prototype, 'constructor');
});
afterEach(function() {
this._sandbox.restore();
this.constructorSpy = null;
});
it('Should set the state with the correct data [constructor]', function() {
const wrapper = shallow(<ExampleComponent />);
console.log(' - callCount: ', this.constructorSpy.callCount);
expect(this.constructorSpy.calledOnce).to.be(true);
expect(Immutable.is(wrapper.state('shownData'), Immutable.Map())).to.be(true);
});
I have some logic in my component constructor that sets the state depending on what I pass in as props. However, this test keeps telling me that the constructor call count is 0 and it is not called.
What's the correct way to spy on a component constructor? What am I doing wrong?
I am using a sandbox because there are other functions I want to add to the sandbox to spy on in the future.