我正在构建一个 HoC,以便轻松创建可选择的表行。我正在尝试编写测试以确保它使用正确传递的道具呈现包装的组件。不幸的是,我无法让我的测试正常工作,因为酶似乎没有将所有成分都渲染出来(或者,更有可能的是,我在做一些有点傻的事情)。
霍克
import React, { PropTypes, Component } from "react";
import { omit } from "lodash/fp";
const propsFilter = omit(["onSelect"]);
export default function selectable(onSelect, isSelected) {
return (component) => {
const wrappedName = component.displayName || component.name || "Component";
const displayName = `Selectable(${wrappedName})`;
const onClick = () => onSelect && onSelect(!isSelected);
class SelectableWrapper extends Component {
render() {
return <component onClick={ onClick } { ...propsFilter(this.props) } />;
}
}
SelectableWrapper.displayName = displayName;
SelectableWrapper.propTypes = Object.assign({
onSelect: PropTypes.func,
isSelected: PropTypes.bool,
}, component.propTypes);
return SelectableWrapper;
};
}
测试
/* eslint-env mocha */
"use strict";
import React from "react";
import { expect } from "chai";
import { spy } from "sinon";
import { mount } from "enzyme";
import selectable from "../../../src/js/components/tables/selectable";
describe("<SelectableHOC />", () => {
let onSelect, isSelected;
const TestElement = () => <p className="test">Hi</p>;
const el = () => selectable(onSelect, isSelected)(TestElement);
beforeEach("setup spy", () => onSelect = new spy());
it("renders the wrapped component, passing through props", () => {
const hoc = el();
const wrapper = mount(<hoc name="foo" />);
expect(wrapper).to.contain("p.test");
});
it("doesn't pass through onSelect");
it("sets onClick on the child component, which triggers onSelect");
});
当我wrapper.debug()
在测试中尝试时,我得到了<hoc data-reactroot="" name="foo"></hoc>
.
测试的输出(失败)是:
1) <SelectableHOC /> renders the wrapped component, passing through props:
AssertionError: expected <HTMLUnknownElement /> to contain p.test
HTML:
<hoc data-reactroot="" name="foo"></hoc>
at Context.<anonymous> (test/components/tables/selectable.spec.js:43:39)