简体   繁体   English

如何断言Jest / Enzyme中的元素“数组属性”?

[英]How to assert on Element 'Array attribute' in Jest/Enzyme?

I have the following rendered HTML from my React component 我有以下来自我的React组件的渲染HTML

<Blueprint2.Select
        disabled={false}
        filterable={false}
        itemRenderer={[Function]}
        items={
          Array [
            "JK",
          ]
        }
        onItemSelect={[MockFunction]}
      >

What I want to assert on is that the array inside the items element contains the correct list, ie just "JK" 我想断言的是,items元素内的数组包含正确的列表,即只是“ JK”

I have tried 我努力了

expect(wrapper.find("Blueprint2.Select").find("[items=['JK']").exists()).toEqual(true);  

But this doesn't work. 但这是行不通的。 How can I do this assertion? 我该如何断言?

UPDATE: In fact even just checking for the presence of Blueprint2.Select fails 更新:实际上,即使只是检查Blueprint2.Select的存在也会失败

expect(wrapper.find("Blueprint2.Select").exists()).toEqual(true);  

Even though I have generated a snapshot and can see the above HTML in it. 即使我已经生成了快照并且可以在其中看到上面的HTML。 Is there a different convention for checking an element with a two part name (separated by a dot)? 检查具有两部分名称(由点分隔)的元素是否存在不同的约定?

You should break your complicated statement into multiple statements, just like in any other programming: 就像在其他任何编程中一样,您应该将复杂的语句分解为多个语句:

const expectedItems = ["JK",]
const component = wrapper.find("Blueprint2.Select");
expect(component.exists()).toBeTruthy();

The key to writing an expect for the array is to realize that they are passed in as props. 为数组写一个期望值的关键是要意识到它们是作为props传入的。 So you can extract the prop you want directly: 因此,您可以直接提取所需的道具:

expect(component.props().items).toEqual(expectedItems);

Or if you want to expect the value of more than one prop: 或者,如果您希望获得多个道具的价值,请执行以下操作:

expectedProps = {
    items: ['JK',],
    disabled: false,
    filterable: false,
}
expect(compoennt.props()).toMatchObject(expectedProps);

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

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