[英]How can I test styles coming from sass with jest and react-testing-library?
I need to test styles of a component from sass, but only inline styles are loaded in the dom我需要测试来自 sass 的组件的 styles,但在 dom 中只加载了内联 styles
my test case:我的测试用例:
describe('Danger', () => {
const dangerLabel = <DangerLabel>TESTE</DangerLabel>;
it('deve ter class -danger', () => {
const { container } = render(dangerLabel);
expect(container.firstChild).toHaveClass('-danger');
});
it('deve ter a cor vermelho', () => {
const { container } = render(dangerLabel);
const style = window.getComputedStyle(container.firstElementChild);
expect(style.backgroundColor).toBe('#e74c3c');
});
});
jest-config玩笑配置
module.exports = {
roots: ['<rootDir>'],
transform: {
'\\.(js|jsx)?$': 'babel-jest',
},
testMatch: [
'<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}',
],
moduleFileExtensions: ['js', 'jsx', 'json', 'node'],
testPathIgnorePatterns: ['/node_modules/', '/public/'],
setupFilesAfterEnv: [
'@testing-library/jest-dom/extend-expect',
],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/config/jest/__mocks__/fileMock.js',
'.+\\.(css|scss)$': 'identity-obj-proxy',
},
};
component export:组件导出:
import LabelContainer from './label_container';
import Label from './DefaultLabel';
import DangerLabel from './DangerLabel';
import PrimaryLabel from './PrimaryLabel';
import WarningLabel from './WarningLabel';
import SuccessLabel from './SuccessLabel';
import InfoLabel from './InfoLabel';
import '../assets/styles/label.scss';
export default Label;
export {
LabelContainer,
PrimaryLabel,
DangerLabel,
WarningLabel,
SuccessLabel,
InfoLabel,
};
exec:执行:
expect(received).toBe(expected) // Object.is equality
Expected: "#e74c3c"
Received: ""
30 | const { container } = render(dangerLabel);
31 | const style = window.getComputedStyle(container.firstElementChild);
> 32 | expect(style.backgroundColor).toBe('#e74c3c');
| ^
33 | });
34 | });
35 |
I have tried some packages like jest-transform-css and jest-transform-scss but with no success, it is possible to do that test?我已经尝试了一些包,比如 jest-transform-css 和 jest-transform-scss 但没有成功,可以做那个测试吗?
As requested DangerLabel Code:根据要求危险标签代码:
import React from 'react';
import DefaultLabel from './DefaultLabel';
const DangerLabel = props => (
<DefaultLabel {...props} className="-danger" />
);
export default DangerLabel;
With toHaveStyle:使用 toHaveStyle:
it('deve ter a cor vermelho', () => {
const { container } = render(dangerLabel);
expect(container.firstElementChild).toHaveStyle(`
background-color: rgb(231, 76, 60)};
`);
});
Result:结果:
You had been missing the visible
prop when rendering DangerLabel
in the test file which is why it was not rendering and failing test #1.在测试文件中渲染DangerLabel
时,您错过了visible
道具,这就是为什么它没有渲染并且测试 #1 失败的原因。
const dangerLabel = <DangerLabel visible>TESTE</DangerLabel>;
The next error that you'll face is the empty string for all CSS properties in the style
object that you get from the window.getComputedStyle
(in test #2).您将面临的下一个错误是您从window.getComputedStyle
(在测试 #2 中)获得的style
object 中的所有 CSS 属性的空字符串。 There is a reason to it.这是有原因的。
So, I went ahead with an alternate implementation of asserting the background colour CSS property using toHaveStyle from the jest-dom lib.因此,我继续使用 jest-dom 库中的toHaveStyle断言背景颜色 CSS 属性的替代实现。 itself.本身。
I had to use an npm module hex-rgb for converting the hex color to a rgba value.我必须使用 npm 模块hex-rgb将十六进制颜色转换为 rgba 值。
See the solution working here .请参阅此处的解决方案。 To view the test results in codesandbox, read this要在 codesandbox 中查看测试结果,请阅读此
Bonus: Using style
object did not work but there is a declarative way of accessing the CSS properties from the return value of getComputedStyle
- const backgroundColor = style. getPropertyValue ('background-color)
奖励:使用style
object 不起作用,但有一种声明性方式可以从 getComputedStyle 的返回值访问getComputedStyle
属性 - const backgroundColor = style. getPropertyValue ('background-color)
const backgroundColor = style. getPropertyValue ('background-color)
. const backgroundColor = style. getPropertyValue ('background-color)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.