【发布时间】:2021-02-22 07:31:57
【问题描述】:
我有一个如下所示的 Button 组件:
const Button = ({
children,
type,
btnStyles,
onClick,
}: any) => {
//...
}
export default Button
我不想确保该按钮仅与上面定义的允许道具一起使用。我的测试目前看起来像这样:
it('should only have the allowed props', () => {
const tree = shallow(
<Button
type='button'
buttonStyles='a-custom-button'
onClick={() => {
console.log('Custom Button Clicked')
}}
>
<i></i>
Button
</Button>
)
expect(tree.prop('type')).toBeDefined()
expect(tree.prop('buttonStyles')).toBeDefined()
})
我还想确保按钮样式只能使用允许的类名。例如按钮只能包含.a-custom-button 和.a-custom-button-styles 类。
我怎样才能用 Jest 做到这一点?
【问题讨论】:
-
如果您提供正确的道具类型而不是任何类型,则无需对其进行测试。
标签: reactjs unit-testing jestjs jasmine react-testing-library