用于 React 的 JS 测试工具:Enzyme

jopen 8年前

Enzyme 来自 airbnb 公司,是一个用于 React 的 JavaScript 测试工具,方便你判断、操纵和历遍 React Components 输出。

Enzyme 的 API 通过模仿 jQuery 的 API ,使得 DOM 操作和历遍很灵活、直观。Enzyme 兼容所有的主要测试运行器和判断库,文档和例子使用 mochachai

使用示例:

import { shallow } from 'enzyme';describe('<MyComponent />', () => {       it('should render three <Foo /> components', () => {      const wrapper = shallow(<MyComponent />);      expect(wrapper.find(Foo)).to.have.length(3);    });       it('should render an `.icon-star`', () => {      const wrapper = shallow(<MyComponent />);      expect(wrapper.find('.icon-star')).to.have.length(1);    });       it('should render children when passed in', () => {      const wrapper = shallow(        <MyComponent>          <div className="unique" />        </MyComponent>      );      expect(wrapper.contains(<div className="unique" />)).to.be.true;    });       it('simulates click events', () => {      const onButtonClick = sinon.spy();      const wrapper = shallow(        <Foo onButtonClick={onButtonClick} />      );      wrapper.find('button').simulate('click');      expect(onButtonClick.calledOnce).to.be.true;    });});

项目主页:http://www.open-open.com/lib/view/home/1450054855047