reactjs - 如何使用酶和玩笑测试 css 属性?
问题描述
我正在测试组件的 css 属性。例如,我需要检查 <p> 标签的默认“颜色”属性是否为黑色
我尝试使用 props().style 但如果样式不是内联的,它就不起作用。我也尝试过 toHaveStyleRule() 但它也无济于事
//imports----------------------------------------------
import React from 'react';
import {shallow,configure,mount,render} from 'enzyme';
import toJson from 'enzyme-to-json';
import Adapter from 'enzyme-adapter-react-16';
import "jest-styled-components";
//-----------------------------------------------------
configure({adapter: new Adapter()});
//-----------------------------------------------------
describe ('Checking if default color of paragraph is black',() =>{
it("doesn't work",()=>{
expect(
mount(<p>Lorem Ipsum</p>).find('p')
).toHaveStyleRule('color','black');
});
it("also doesn't work",()=>{
expect(
mount(<p>Lorem Ipsum</p>).find('p').props().style.color
).toEqual('black');
});
});
我希望测试应该通过(因为段落的默认颜色确实是黑色)。
第一个测试失败并显示消息:
No style rules found on passed Component
第二个失败并显示消息
TypeError: Cannot read property 'color' of undefined
解决方案
推荐阅读
- postgresql - 如何在 Postgres 中完全禁用`client_min_messages`?
- postgresql - 可以使用 barman 设置 postgresql 流复制吗?
- vue.js - Vue - 表过滤器的性能问题?
- javascript - 我需要在两个不同的 DIV 中使用相同的 ID 内容。我该怎么做?
- html - 猫头鹰轮播版本 1 在最后一项后自动播放/循环
- .net - 无法复制文件 deps.json,因为找不到它。Microsoft.Common.CurrentVersion.targets 4919
- reactjs - 如何检查密码是否与 yup 匹配
- reactjs - ReactJS - 如何在将数据发布到数据库后重新渲染功能组件
- node.js - 如何从 salesforce 获取元数据并使用该元数据(列名)在 postgresql 中创建表
- python - 循环遍历特定(比如前 3 个)值的列表,然后再次循环下一个(下 3 个)值并在 django 模板中迭代直到结束