检查复选框是否被开玩笑/酶

时间:2019-02-16 08:47:45

标签: javascript jestjs enzyme

如何测试是否选中了多个复选框?

    <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent" 
   android:layout_height="match_parent">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

我尝试过

render()
    ...
        <React.Fragment>
              <div
                className='foo'>
                <label>
                  <input
                    className='checkbox'
                    name='bar'
                    type='checkbox'
                    checked={this.props.checked}
                    onChange={() => { } }
                  />
                </label>
              </div>
        </React.Fragment>
    ...

it('checks all checkboxes', () => {
    const component = shallow(
        <Foo
            ... />
    )

    expect(component
        .find('input[type="checkbox"][checked="checked"]'))
        .toHaveLength(content.length);
});

component
    .find({ type: 'checkbox' })
    .forEach(node => {
        expect(node
            .props
            .checked)
            .toEqual(true);
    });

component
    .find('input')
    .forEach(node => {
        expect(node
            .props
            .checked)
            .toEqual(true);
    });

最后三个给了我component .find('.checkbox') .forEach(node => { expect(node .props .checked) .toEqual(true); }); ,而第三个给了我一个巨大的物体(undefined

2 个答案:

答案 0 :(得分:2)

通过jest-dom,您可以使用expect(node).toBeChecked();

答案 1 :(得分:1)

您只是想念props是一个返回道具(而不是直接访问道具)的函数,因此您必须调用它:

component
    .find('input')
    .forEach(node => {
        expect(node
            .props()
            .checked)
            .toEqual(true);
    });