Reputation: 23
Is there a way to test a non inline style CSS color of the word 'Hello' in a jest?
.textStyle {
color: red;
}
<p class="textStyle">Hello</p>
test('test text color', () => {
const wrapper = mount(<React />);
const p = wrapper.find('p').text();
//insert code here
});
Upvotes: 2
Views: 716
Reputation: 8422
For that, you would use a regular getBy
or findBy
or any selectors and use a toHaveStyle
accordingly
expect(getByTestId('elementId')).toHaveStyle('color: red')
You will also need to install jest-dom to have toHaveStyle available
Upvotes: 2
Reputation: 445
You might find some success with the window.getComputedStyle API. Calling this with an element gives you an object of that elements styles, which you can then verify have the correct values.
Upvotes: 0