gpt4 book ai didi

reactjs - 使用 Jest-Enzyme 测试样式组件

转载 作者:行者123 更新时间:2023-12-03 13:24:17 29 4
gpt4 key购买 nike

如何测试我的 styled-component 是否具有特定的 CSS 属性值对?

假设我的组件如下:

const myColor = '#111';

const Button = styled.button`
background-color: ${myColor};
`;

该测试检查 'background-color' 属性的值为 '#111'

测试运行程序是Jest,我使用测试实用程序库Enzyme

最佳答案

jest-styled-componentstoHaveStyleRule 函数解决了我的问题!

import 'jest-styled-components'

describe('<Button> component', () => {
it('should have myColor', () => {
const wrapper = mount(<Button />);
expect(wrapper.find('Button')).toHaveStyleRule('background-color', myColor)
});
});

关于reactjs - 使用 Jest-Enzyme 测试样式组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54037054/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com