【发布时间】:2021-01-11 09:21:48
【问题描述】:
我试图用 Jest 和 Enzyme 测试我的组件样式。我使用 TailwindCSS 作为我的 CSS 框架和非弹出的 create-react-app。所以这是我的组件:
import React from 'react'
import './tailwind.css' // the tailwind css
export function Alert({ children, className, icon, ...resProps }) {
return (
<div data-testid="qa-alert" className="my-2 p-3 text-lg bg-red-200 rounded-md flex items-center justify-center} {...resProps}>
{icon && React.createElement(icon, { className: 'mr-2 text-xl' })}
{children}
</div>
);
}
我想通过 getComputedStyle 方法测试背景颜色是否为红色(bg-red-200 或#fed7d7),这是我的测试:
it('Should have red background color', () => {
const wrapper = mount(<Alert />);
const alert = wrapper.find('div');
const style = getComputedStyle(alert.getDOMNode());
console.log(style);
expect(style.backgroundColor).toBe('#fed7d7');
});
但测试失败,因为我没有得到背景样式。接收到的值为空字符串。而style的日志是
CSSStyleDeclaration {
'0': 'display',
_values: { display: 'block' },
_importants: { display: '' },
_length: 1,
_onChange: [Function]
}
我怎样才能得到样式?
【问题讨论】:
标签: reactjs jestjs create-react-app enzyme tailwind-css