How can I test if my styled-component
has a specific CSS attribute-value pair?
Let's say my component is the following :
const myColor = '#111';
const Button = styled.button`
background-color: ${myColor};
`;
And the test checks that the 'background-color'
attribute has the value '#111'
.
The test runner is Jest and I use the testing utilities library Enzyme.
The toHaveStyleRule
function of jest-styled-components solved my problem!
import 'jest-styled-components'
describe('<Button> component', () => {
it('should have myColor', () => {
const wrapper = mount(<Button />);
expect(wrapper.find('Button')).toHaveStyleRule('background-color', myColor)
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With