简体   繁体   English

在酶中测试DOM

[英]Testing DOM in Enzyme

Let's say I have a tiny component like this: 假设我有一个像这样的小组件:

Button.js 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>
    );
  }
}

And there's some super basic styling like this: 并且有一些像这样的超级基本样式:

Button.css 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;
}

And let's say I want to write some tests for this: 假设我要为此编写一些测试:

Button.test.js 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);
});

I've set this up using react-create-app and all the above works perfectly. 我已经使用react-create-app进行了设置,以上所有方法都可以完美地工作。

My question is : how do I test that what is getting rendered looks correct? 我的问题是 :如何测试呈现的内容看起来正确? For example, in this case I would want to make sure that the buttons have the correct background colours defined in the CSS file and that they have the correct border radius. 例如,在这种情况下,我要确保按钮具有CSS文件中定义的正确背景色,并具有正确的边框半径。 This will prevent other developers accidentally overriding critical styling for example. 例如,这将防止其他开发人员意外覆盖关键样式。

I was under the impression that Enzyme did this out of the box, but I cannot understand how to interrogate the virtual DOM which I assume is happening in the background? 我给人的印象是酶是开箱即用的,但是我不明白如何询问我认为在后台发生的虚拟DOM? I thought that JSDOM was automatically running and I'm executing this from the CLI which is a Node environment. 我以为JSDOM是自动运行的,并且我正在从Node环境的CLI中执行此操作。

I've tried this so far: 到目前为止,我已经尝试过了:

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');
});

But background is returned blank, in fact if I do console.log(getComputedStyle(domNode)) I get this returned which seems to be missing the styles: 但是background返回为空白,实际上,如果我执行console.log(getComputedStyle(domNode))我得到的返回结果似乎缺少样式:

  console.log src/modules/Button/Button.test.js:42
      CSSStyleDeclaration {
        _values: {},
        _importants: {},
        _length: 0,
        _onChange: [Function] }

The getDOMNode of an enzyme wrapper gets you the corresponding DOM node. 酶包装器的getDOMNode为您提供相应的DOM节点。

You can then use getComputedStyle to get the style of that DOM: 然后,您可以使用getComputedStyle获取该DOM的样式:

const renderedComponent = mount(<MyComponent /);
const domNode = renderedComponent.find('div').at(0).getDOMNode();
const background = getComputedStyle(domNode).getPropertyValue('background');
expect(background).toBe('red');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM