繁体   English   中英

测试React-Bootstrap组件的问题

[英]Issues Testing React-Bootstrap Components

在测试使用React-Bootstrap与Mocha,Chai和Enzyme的组件时,我遇到了一些困难。 希望有人可以让我知道我做错了什么。

在测试不使用React-Bootstrap的组件时,我注意到输出是原始的html(),当测试React-Bootstrap时,我只返回组件而不是原始的html()。 这在尝试测试时引起了巨大的麻烦。 如果我使用浅,mount和render,就会发生这种情况。

如果有人有一个线索,为什么我不能得到原始的HTML测试时将非常感谢! 我已经包含了一些代码来展示我在说什么。

ReactTest.jsx

import React from 'react';

const ReactTest = () => (
  <div>
    <button>ReactTest</button>
  </div>
);

export default ReactTest;

ReactBootstrapTest.jsx

import React from 'react';
import { Button } from 'react-bootstrap';

const ReactBootstrapTest = () => (
  <div>
    <Button>ReactBootstrapTest</Button>
  </div>
);

export default ReactBootstrapTest;

reactTest.js

import React from 'react';
import { expect } from 'chai';
import { shallow } from 'enzyme';

import ReactTest from '../../../../../../components/ReactTest';
import ReactBootstrapTest from '../../../../../../components/ReactBootstrapTest';

const reactTestEnzymeWrapper = () => (
  shallow(<ReactTest />)
);

const reactBootstrapTestEnzymeWrapper = () => (
  shallow(<ReactBootstrapTest />)
);

describe.only('ReactTest', () => {
  it('should output debug', () => {
    const reactTest = reactTestEnzymeWrapper();
    console.log('ReactTest', reactTest.debug());
  });

  it('should find the <button>', () => {
    const reactButton = reactTestEnzymeWrapper().find('button');
    expect(reactButton.at(0).text()).to.equal('ReactTest');
  });
});

describe.only('ReactBootstrapTest', () => {
  it('should output debug', () => {
    const reactBootstrapTest = reactBootstrapTestEnzymeWrapper();
    console.log('ReactBootstrapTest', reactBootstrapTest.debug());
  });

  it('should find the <button>', () => {
    const bootstrapButton = reactBootstrapTestEnzymeWrapper().find('button');
    expect(bootstrapButton.at(0).text()).to.equal('ReactBoostrapTest');
  });
})

如果我正确地理解了这个问题,问题就是如果

item = shallow(<MyReactComponent>)

MyReactComponent包含一个React Bootstrap项目,比如id = itemId

item.find('#itemId').text() 

返回类似的东西

"<Button/>" 

而不是Button中的实际文本。 如果您按类或组件名称找到,则同样适用。

我使用以下方法解决了这个问题:

item.find('#itemId').html()

然后使用辅助函数从html中解析出文本:

chai.expect(getBootstrapText(item.find('#butRemove').html())).to.equal('Remove Design');

我对这个解决方案不是很满意,所以很高兴听到一个更好的解决方案,但它有效......

暂无
暂无

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

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