简体   繁体   English

使用酶测试React Component className

[英]Testing React Component className with enzyme

When testing a react component which uses className to set the css class using enzyme (mount or shallow) I'm able to test correctly when it's a div but unable to get it to work on an h1 tag. 当测试一个使用className来设置css类的react组件时使用酶(mount或浅)我能够正确测试它是div还是无法让它在h1标签上工作。

Is this some 这是一些吗?

  • thing to do with mount or shallow? 与山或浅的事情有关?
  • Is it something I'm missing? 这是我缺少的东西吗?
  • Is it a bug? 这是一个错误吗?

Any thoughts appreciated! 任何想法赞赏!

JSX: JSX:

import React from 'react'

export const PageNotFound = ({heading, content, wrapperCSS, headingCSS, contentCSS}) => (
<div className={ wrapperCSS }>
  <div className={ contentCSS }>
    { content }
  </div>
  <h1 className={ headingCSS }>{ heading }</h1>
</div>
)

PageNotFound.propTypes = {
    heading: React.PropTypes.string,
    content: React.PropTypes.string,
    wrapperCSS: React.PropTypes.string,
    headingCSS: React.PropTypes.string,
    contentCSS: React.PropTypes.string
};

PageNotFound.defaultProps = {
    heading: '404',
    content: 'Page Not Found',
    wrapperCSS: 'wrapper',
    headingCSS: 'heading',
    contentCSS: 'content'
};

export default PageNotFound

Spec: 规格:

import React from 'react'
import { expect } from 'chai'
import { shallow, mount } from 'enzyme'

import PageNotFound from './PageNotFound'

describe('<PageNotFound/>', function() {

let wrapper;

beforeEach(() => {
    wrapper = mount(<PageNotFound contentCSS="mycontent" headingCSS="myheader" content="Message" heading="My Title" />);
})

it('Uses heading prop', () => {
    expect(wrapper.find('h1').text()).to.eq('My Title')
});

it('Uses headingCSS prop', () => {
    console.log(wrapper.html());
    expect(wrapper.find('h1.myheader').length).to.eq(1)
});

it('Uses content prop', () => {
    expect(wrapper.find('div.mycontent').text()).to.eq('Message')
});


});

Test Results: 检测结果:

Notice the debug log which shows the h1 with class myheader, but the test fails with zero elements found for h1.myheader 请注意调试日志,它显示带有myheader类的h1,但测试失败,找到了h1.myheader的零元素

<PageNotFound/>
    ✓ Uses heading prop
LOG LOG: '<div class="_2t--u"><h1 class="myheader">My Title</h1><div class="mycontent">Message</div></div>'
    ✗ Uses headingCSS prop
    expected 0 to equal 1
    r@tests.webpack.js:11:24087
    assert@tests.webpack.js:14:52974
    a@tests.webpack.js:14:55858
    tests.webpack.js:15:17123
    tests.webpack.js:14:10442

    ✓ Uses content prop

Weird. 奇怪的。 It should work. 它应该工作。

Anyway, you could try taking advantage of Enzyme's API instead. 无论如何,您可以尝试利用Enzyme的API。

For this particular test, .hasClass() should do the job and is clearer about its intent: 对于这个特定的测试, .hasClass()应该完成这项工作,并且更清楚它的意图:

expect(wrapper.find('h1').hasClass('myheader')).to.eq(true)

The problem here is that your import import styles from './styles.module.css' is not actually being loaded. 这里的问题是实际上没有加载import styles from './styles.module.css'导入import styles from './styles.module.css'

It is likely that you have something in a test setup file to mock out anything with a css extension: 您可能在测试设置文件中有一些东西可以模拟任何带有css扩展名的东西:

require.extensions['.css'] = function () {
  return null;
};

I don't have enough rep, or I would have just commented this. 我没有足够的代表,或者我会对此进行评论。 Unfortunately, I don't yet know a way to actually import these styles, as you can tell from my question here: WebPack LESS imports when testing with Mocha 不幸的是,我还不知道实际导入这些样式的方法,正如你在我的问题中可以看出的那样: 使用Mocha测试时WebPack LESS导入

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

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