5

如果我的<Text />标签正确接收props.header为文本,我想用 Jest 和 Enzyme 进行单元测试。

通常我能够<Text />像这样测试标签的内容:

it("should render a label", () => {
  expect(wrapper.find(Text).contains("submit")).toBe(true);
});

但是,一旦我通过一个对象,这将不再可能。我来给你展示:

const createTestProps = props => ({
  header: SOME_CONSTANT,
  ...props
});

...

  let wrapper;
  let props;
  beforeEach(() => {
    props = createTestProps();
    wrapper = shallow(<MyList {...props} loaded={false} />);
  });

  it("should render a header", () => {
    expect(wrapper.find(Text).contains(props.header)).toBe(true);
  });

这失败并显示以下错误消息:

● MyList › rendering › still loading › should render a header

    expect(received).toBe(expected) // Object.is equality

    Expected: true
    Received: false

我如何使用 Jest 和 Enzyme 进行测试?

编辑

我发现将常量传递给props. 如果我硬编码这样的值props

const createTestProps = props => ({
  header: "Some hardcoded value",
  ...props
});

测试也通过了。有没有办法让这个工作即使是常数?

4

0 回答 0