I'm testing the css attributes of a component. For instance, I need to check if the default 'color' attribute of a <p> tag is black
I tried using props().style but it doesn't work if styling is not inline. I also tried toHaveStyleRule() but it also doesn't help
//imports----------------------------------------------
import React from 'react';
import {shallow,configure,mount,render} from 'enzyme';
import toJson from 'enzyme-to-json';
import Adapter from 'enzyme-adapter-react-16';
import "jest-styled-components";
//-----------------------------------------------------
configure({adapter: new Adapter()});
//-----------------------------------------------------
describe ('Checking if default color of paragraph is black',() =>{
it("doesn't work",()=>{
expect(
mount(<p>Lorem Ipsum</p>).find('p')
).toHaveStyleRule('color','black');
});
it("also doesn't work",()=>{
expect(
mount(<p>Lorem Ipsum</p>).find('p').props().style.color
).toEqual('black');
});
});
I expect that the test should pass (as default color of paragraph is indeed black).
The first test fails with message:
No style rules found on passed Component
The second one fails with message
TypeError: Cannot read property 'color' of undefined
Lorem Ipsum
).find('p').get(0).style.color).toEqual('black'); But it says it cannot read color of undefined – Abhay Aravinda Jun 25 '19 at 11:08Lorem Ipsum
).find('p').instance().style.color).toEqual('black')` gives the error Expected value to equal: "black" Received: "" – Abhay Aravinda Jun 25 '19 at 13:15Lorem Ipsum
` won't have color black, there is no style definition here... – kidney Jun 25 '19 at 13:21