我找到了一个解决方案——通过在材质ui组件中指定自定义类,然后在样式中引用它,它在附加了修饰符的情况下测试良好
实例
印刷术。风格js公司
import React from 'react'
import { withTheme } from 'material-ui/styles'
import styled, { css } from 'styled-components'
import Typography from 'material-ui/Typography'
export const StyledTypography = withTheme()(styled(({emphasized, strong, inlined, color, ...other}) => {
return <Typography {...other} classes={{root: 'typography-root'}} />
})`
&.typography-root {
line-height: 1.2em;
${props => !props.variant && css`
font-size: 1em;
`}
${props => props.color && props.theme.palette.typography[props.color] && css`
color: ${props.theme.palette.typography[props.color]};
`}
${props => props.strong && css`
font-weight: 500;
`}
${props => props.emphasized && css`
font-style: italic;
`}
${props => props.inlined && css`
display: inline-block;
`}
}
`)
测验
it('does something', () => {
const typography = mount(
<MuiThemeProvider theme={praxisTheme}>
<Typography />
</MuiThemeProvider>
)
expect(typography.find(MaterialUITypography)).toHaveStyleRule('line-height', '1.2em', { modifier: '&.typography-root' })
})