Sim*_*ong 21 javascript css reactjs jestjs enzyme
我已经MyStyledButton基于material-ui 编写了一个自定义按钮 ( ) Button。
import React from "react";
import { Button } from "@material-ui/core";
import { makeStyles } from "@material-ui/styles";
const useStyles = makeStyles({
root: {
minWidth: 100
}
});
function MyStyledButton(props) {
const buttonStyle = useStyles(props);
const { children, width, ...others } = props;
return (
<Button classes={{ root: buttonStyle.root }} {...others}>
{children}
</Button>
);
}
export default MyStyledButton;
Run Code Online (Sandbox Code Playgroud)
它使用主题进行样式设置,这指定backgroundColor为黄色阴影(特别是#fbb900)
import { createMuiTheme } from "@material-ui/core/styles";
export const myYellow = "#FBB900";
export const theme = createMuiTheme({
overrides: {
MuiButton: {
containedPrimary: {
color: "black",
backgroundColor: myYellow
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
该组件在 my main 中实例化index.js并包装在theme.
<MuiThemeProvider theme={theme}>
<MyStyledButton variant="contained" color="primary">
Primary Click Me
</MyStyledButton>
</MuiThemeProvider>
Run Code Online (Sandbox Code Playgroud)
如果我检查 Chrome DevTools 中的按钮,它background-color会按预期“计算”。在 Firefox DevTools 中也是如此。
但是,当我编写一个 JEST 测试来检查background-color并查询按钮的 DOM 节点样式时,getComputedStyles()我transparent回来了,但测试失败了。
const wrapper = mount(
<MyStyledButton variant="contained" color="primary">
Primary
</MyStyledButton>
);
const foundButton = wrapper.find("button");
expect(foundButton).toHaveLength(1);
//I want to check the background colour of the button here
//I've tried getComputedStyle() but it returns 'transparent' instead of #FBB900
expect(
window
.getComputedStyle(foundButton.getDOMNode())
.getPropertyValue("background-color")
).toEqual(myYellow);
Run Code Online (Sandbox Code Playgroud)
我已经包含了一个带有确切问题的 CodeSandbox,要重现的最少代码和失败的 JEST 测试。
我已经接近了,但还没有找到解决方案。
主要问题是 MUIButton 向元素注入一个标签来支持样式。这在您的单元测试中不会发生。我能够通过使用材质测试使用的createMount来使其工作。
修复后,样式可以正确显示。然而,计算的样式仍然不起作用。看起来其他人已经遇到了酶正确处理这个问题的问题 - 所以我不确定这是否可能。
要达到我所在的位置,请获取您的测试片段,将其复制到顶部,然后将测试代码更改为:
const myMount = createMount({ strict: true });
const wrapper = myMount(
<MuiThemeProvider theme={theme}>
<MyStyledButton variant="contained" color="primary">
Primary
</MyStyledButton>
</MuiThemeProvider>
);
Run Code Online (Sandbox Code Playgroud)
class Mode extends React.Component {
static propTypes = {
/**
* this is essentially children. However we can't use children because then
* using `wrapper.setProps({ children })` would work differently if this component
* would be the root.
*/
__element: PropTypes.element.isRequired,
__strict: PropTypes.bool.isRequired,
};
render() {
// Excess props will come from e.g. enzyme setProps
const { __element, __strict, ...other } = this.props;
const Component = __strict ? React.StrictMode : React.Fragment;
return <Component>{React.cloneElement(__element, other)}</Component>;
}
}
// Generate an enhanced mount function.
function createMount(options = {}) {
const attachTo = document.createElement('div');
attachTo.className = 'app';
attachTo.setAttribute('id', 'app');
document.body.insertBefore(attachTo, document.body.firstChild);
const mountWithContext = function mountWithContext(node, localOptions = {}) {
const strict = true;
const disableUnnmount = false;
const localEnzymeOptions = {};
const globalEnzymeOptions = {};
if (!disableUnnmount) {
ReactDOM.unmountComponentAtNode(attachTo);
}
// some tests require that no other components are in the tree
// e.g. when doing .instance(), .state() etc.
return mount(strict == null ? node : <Mode __element={node} __strict={Boolean(strict)} />, {
attachTo,
...globalEnzymeOptions,
...localEnzymeOptions,
});
};
mountWithContext.attachTo = attachTo;
mountWithContext.cleanUp = () => {
ReactDOM.unmountComponentAtNode(attachTo);
attachTo.parentElement.removeChild(attachTo);
};
return mountWithContext;
}
Run Code Online (Sandbox Code Playgroud)