I'm trying to check the default state of my component in my Enzyme unit tests. For this, I have the following component:
import React, { Component } from 'react';
class Picker extends Component {
constructor(props) {
super(props);
this.state = {
sources: true
};
}
}
...
export default Picker;
Finally, my unit test looks like this:
it('should contain `everything` as a default value', () => {
const wrapper = mount(<Picker name='cnn' />);
expect(wrapper.state('sources')).to.exist()
});
The problem I'm facing here is that, I'm unable to get the component default state. The function wrapper.state('sources') should be returning 'true' if I'm not wrong.
Is there any other step I'm missing? I'm using:
- "react": "^16.2.0"
- "chai": "^4.1.2",
- "enzyme": "^3.2.0",
- "enzyme-adapter-react-16": "^1.1.1",
- "jsdom": "^11.5.1",
- "mocha": "^4.0.1",
- "react-addons-test-utils": "^15.6.2"
wrapper.state()
and see if it prints out{ source: true }
. If that's the case, then yourchai
assertion is wrong. I think it should beexpect(wrapper.state('sources')).to.be.true;
instead. – Sam R.