Jest Not Able To Verify Presence For A Proeprty Using Tohaveproperty
I am using Jest and enzyme, I have a react component, below its structure when performing .debug(). console.log src\shared\navigation\Navigations.test.js:20
Solution 1:
You could try to use .get()
(or .first()
in the second example) to take a specific element from the array returned by .find()
and use .props
to check for a property or passing and object to search directly in .find()
.
Docs:
http://airbnb.io/enzyme/docs/api/ReactWrapper/get.htmlhttp://airbnb.io/enzyme/docs/api/ShallowWrapper/find.html
it('should have property home', () => {
const wrapper = shallow(<Navigations />)
expect(wrapper.find(NavLink).get(0).props.to).toEqual('/')
})
or
it('should have property home', () => {
const wrapper = shallow(<Navigations />)
expect(wrapper.find(NavLink).find({ to: '/' }).first().length === 1).toEqual(true)
})
Solution 2:
It is probably a race condition, where the test is checking it before NavLink
has the property. For more info, look at Jest's documentation on testing asynchronous code
it('shouldhave property home', (done) => {
const wrapper = shallow(
<Navigations />,
);
const test = wrapper.find(NavLink).first();
console.log(test.debug());
expect(test).toHaveProperty('to', '/');
done();
});
Post a Comment for "Jest Not Able To Verify Presence For A Proeprty Using Tohaveproperty"