Material-ui'yeMyStyledButton dayalı özel bir düğme ( ) yazdım . 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;
Bir tema kullanılarak stilize edilir ve bu backgroundColor, sarı bir gölge olacağını belirtir (Özellikle #fbb900)
import { createMuiTheme } from "@material-ui/core/styles";
export const myYellow = "#FBB900";
export const theme = createMuiTheme({
overrides: {
MuiButton: {
containedPrimary: {
color: "black",
backgroundColor: myYellow
}
}
}
});
Bileşen ana bölümümde somutlaştırılır index.jsve içine sarılır theme.
<MuiThemeProvider theme={theme}>
<MyStyledButton variant="contained" color="primary">
Primary Click Me
</MyStyledButton>
</MuiThemeProvider>
Chrome DevTools'ta düğmeyi incelersem, background-colorbeklendiği gibi "hesaplanır". Bu aynı zamanda Firefox DevTools'ta da geçerlidir.
Ancak kontrol etmek için bir JEST sınaması yazdığımda background-colorve DOM düğümü stilini backf geri getComputedStyles()aldığım transparentve sınamayı kullanarak düğmeyi sorguladığımda .
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);
Ben tam sorun, yeniden üretmek için minimum kod ve başarısız JEST testi ile bir CodeSandbox dahil ettik.
themeTestte kullanılması gerekmez mi ? İçinde olduğu <MyStyledButton>gibi <MuiThemeProvider theme={theme}>? Ya da temayı tüm bileşenlere eklemek için bir sarmalayıcı işlevi mi kullanıyorsunuz?
