javascript - 使用 materialUI 测试 className
问题描述
所以我试图测试我的组件是否有一个类。问题是,它是使用 MaterialUI 生成的。因此,当我尝试检查我的组件是否具有 类时spinningIconCenter
,测试失败,因为该类是:makeStyles-spinningIconCenter-9
。我该如何测试呢?
我基本上试过这个。但我知道这种方法是错误的,因为它比较了整个班级。
const createComponent = (text?: string, center?: boolean) => {
component = render(<Component text={text} center={center} />);
};
it('should render icon with center class name', () => {
createComponent('Some test text', true);
const iconClassName = component.getByTestId("spinningIcon");
expect(iconClassName).toHaveClass("spinningIconCenter");
});
现在我看到我需要这样做:
it('should render icon with center class name', () => {
createComponent('Some test text', true);
expect(iconClassName.classList.contains('makeStyles-spinningIconCenter-9')).toEqual(true);
});
问题是我不知道结局会是什么。我该如何处理?
解决方案
好的,你需要这样做:
it('should render icon with center class name', () => {
createComponent('Some test text', true);
const iconClassName = component.getByTestId('spinningIcon');
expect(iconClassName.classList.value.includes('spinningIconCenter')).toEqual(true);
});
推荐阅读
- ruby-on-rails - 更改嵌套内容的顺序
- amazon-web-services - AWS 参数存储:AWSSimpleSystemsManagementException:超出速率
- json - 即使我的令牌是正确的,我收到的输出消息是“缺少身份验证令牌”
- c# - WinCe 6 上的 Windows 窗体应用程序
- html - 如何使这部分全屏高度?无论我们做什么,它都会在底部留下空白
- sqlite - SQLite 错误外键不匹配 - 股票引用变体
- c - 如何创建输入的每个单词以开始新行
- ruby-on-rails - 如何通过指定控制器和动作来生成路径?
- javascript - 如何使用javascript隐藏过去的时间?
- apache - AH01114: HTTP: 无法连接到后端: localhost (apache 作为 docker 容器)