- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个像这样的小组件:
Button.js
import React from 'react';
import './Button.css';
export default class Button extends React.Component {
render() {
return (
<a href={ this.props.url } className={`button button-${ this.props.type }`}>
{ this.props.content }
</a>
);
}
}
还有一些像这样的 super 基本样式:
Button.css
.button {
color: white;
padding: 1em;
border-radius: 5px;
text-decoration: none;
}
.button-primary {
background-color: red;
}
.button-primary:hover {
background-color: darkred
}
.button-secondary {
background-color: aqua;
color: black;
}
.button-secondary:hover {
background-color: darkcyan;
color: white;
}
假设我想为此编写一些测试:
Button.test.js
import React from 'react';
import Enzyme, {shallow, mount} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({adapter: new Adapter()});
import Button from './Button';
import './Button.css';
// Render buttons
const primaryButton = mount(
<Button
content="Primary button"
url="http://www.amazon.co.uk"
type="primary"
/>
);
const secondaryButton = mount(
<Button
content="Secondary button"
url="http://www.ebay.co.uk"
type="secondary"
/>
);
it('should exist', () => {
expect(primaryButton).toBeDefined();
expect(secondaryButton).toBeDefined();
});
it('should display text in the button', () => {
expect(primaryButton.text()).toEqual('Primary button');
});
it('should have the correct CSS classes', () => {
expect(primaryButton.find('.button').hasClass('button-primary')).toEqual(true);
expect(secondaryButton.find('.button').hasClass('button-secondary')).toEqual(true);
});
我已经使用 react-create-app
进行了设置,以上所有内容都完美运行。
我的问题是:如何测试渲染的内容看起来是否正确?例如,在本例中,我想确保按钮具有 CSS 文件中定义的正确背景颜色,并且具有正确的边框半径。例如,这将防止其他开发人员意外覆盖关键样式。
我的印象是 Enzyme 是开箱即用的,但我无法理解如何询问我认为在后台发生的虚拟 DOM?我认为 JSDOM 会自动运行,并且我从作为 Node 环境的 CLI 执行此操作。
到目前为止我已经尝试过:
it('should have the correct background colours', () => {
const domNode = primaryButton.find('.button').at(0).getDOMNode();
const background = getComputedStyle(domNode).getPropertyValue('background');
expect(background).toBe('red');
});
但是 background
返回空白,事实上,如果我执行 console.log(getCompulatedStyle(domNode))
我得到的返回值似乎缺少样式:
console.log src/modules/Button/Button.test.js:42
CSSStyleDeclaration {
_values: {},
_importants: {},
_length: 0,
_onChange: [Function] }
最佳答案
enzyme 包装器的 getDOMNode
可以获取相应的 DOM 节点。
然后您可以使用 getCompulatedStyle
获取该 DOM 的样式:
const renderedComponent = mount(<MyComponent /);
const domNode = renderedComponent.find('div').at(0).getDOMNode();
const background = getComputedStyle(domNode).getPropertyValue('background');
expect(background).toBe('red');
关于javascript - 在 Enzyme 中测试 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51024736/
我的页面上有多个按钮: Home Action 1 Action 2 如何通过其文本选择“操作 2”按钮,以便单击它?我知道可能还有其他方法来选择该按钮,但我专门寻找通过文
我目前正在学习使用 Enzyme 进行单元测试。 似乎有一种奇怪的行为: enzyme 似乎随机地不识别某些成分。让我给你举个例子: 这是我的SafeContainer组件: import React
我在使用 Jest/Enzyme 编写的测试用例时遇到了一些问题。我有一个 React/Redux 组件,正在尝试编写一个基本测试,但出现以下错误: Invariant Violation: Reac
在我的项目中使用 enzyme 3 时出现以下错误(之前使用 enzyme 2 并且一切正常): Enzyme Internal Error: configured enzyme adapter di
我正在致力于使用摩卡、 enzyme 创建 react 组件的单元测试。下面是一个示例组件。 Foo.js class Foo extends React.Component { custom
我已经设置了 jest 和 enzyme,但无法运行。它提示 EnzymeAdapter 基类。 我试过了 configure({}) with import { configure } from '
使用 React+Enzyme+Jest 您好,我使用 .innerText 属性来获取特定元素的值,请参阅我的代码的第 5 行: _modifyProfileField (event) {
我正在使用 airbnb 的 enzyme 库进行 react 测试,我只想检索当前节点的文本,不包括来自任何子节点的任何文本。 const component = hellothere 如果我做
我一直在查看很多使用 Enzyme 和 Jest 测试 React 和 Redux 应用程序的示例,但很少有人提到代码清理。当使用 shallow 或 mount 时,您是否不需要明确调用 unmou
从文档来看,这样的事情应该是可能的: const wrapper = mount(); expect(wrapper.find('.foo')).to.have.length(1); 但就我而言,这会
有关enzyme 测试库中adapter 用途的任何文档。 import { configure } from 'enzyme'; import Adapter from 'enzyme-adapte
我需要访问内部组件的状态,使其在单击事件时处于事件状态,我的问题是 Enzyme 在使用 mount 时不允许这样做,这只能通过 来实现如 here 中提到的 enzyme 的浅渲染,也如上所述,我尝
我正在尝试使用 Enzyme 来测试组件的方法。我知道执行此操作的典型方法是使用 Enzyme 的 instance() 方法。 问题是,这只适用于root组件,我的组件需要包装在两个上下文提供程序中
我正在尝试测试当通过简单的 bool 值更新状态时,在父组件中其子组件是否正确呈现。在父组件下方: class Parent extends Component { ... render()
我的渲染函数中有一个 FileInput } className= 'fileInput' /> 我需要编写一个文件上传测试,当我模拟更改函数时,它调用函数file
我通过 create-react-app 创建了一个新的 React 应用程序,我想为我在应用程序中创建的名为“MessageBox”的组件编写单元测试。这是我编写的单元测试: import Mess
昨天我将React项目升级到了v16.0,但是我发现Enzyme有一些问题 Error: Enzyme Internal Error: Enzyme expects an adap
我有一个具有多种渲染方法的组件。我怎样才能测试它们呢?使用 enzyme 和 Jest 进行 react 。 一些伪代码作为我的组件结构的示例,因为该组件非常大。 class MyComponent
我正在使用 Jest 和 Enzyme 来测试我的 react 组件。其中一项测试如下所示: import DropDown from './DropdDown'; import SelectButt
我正在尝试测试轮子事件。我在加载事件中添加滚轮监听器。 this.domElement = document.getElementById('myImg'); if (this.domElement)
我是一名优秀的程序员,十分优秀!