ReactDOM.findDOMNode无法按预期工作

问题描述 投票:1回答:1

我被分配来修复一些失败的Jest测试。这是一个例子:

这是checkboxGroup.spec.js:

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import CheckboxGroup from '../components/core/CheckboxGroup';
import Checkbox from '../components/core/Checkbox';

describe('CheckboxGroup', () => {
it('should exist', () => {
    expect(CheckboxGroup).toBeDefined();
});

it('should add checkboxes as children', () => {
    class CheckboxGroupSample extends React.Component {
        selected = [];
        render() {
            return (
                <div>
                    <CheckboxGroup selected={ this.selected }>
                        <Checkbox value={ 1 }></Checkbox>
                        <Checkbox value={ 2 }></Checkbox>
                        <Checkbox value={ 3 }></Checkbox>
                    </CheckboxGroup>
                    Selected: { this.selected.toString() }
                </div>
            );
        }
    }

    const checkboxGroup = TestUtils.renderIntoDocument(<CheckboxGroupSample />);

    const checkboxGroupNode = ReactDOM.findDOMNode(checkboxGroup);

    // Verify the correct number of children are created
    expect(checkboxGroupNode.children.length).toEqual(3);
});

这是我的CheckboxGroup.jsx:

import React, { PropTypes } from 'react';
import valueCompare from './internal/valueCompare';
import Checkbox from './Checkbox';

export default class CheckboxGroup extends React.Component {
    static propTypes = {
        defaultSelected: PropTypes.any,
        selected: PropTypes.any,
        children: PropTypes.node,
        onSelect: PropTypes.func,
        onChange: PropTypes.func,
        name: PropTypes.string
    }

    handleSelect(event, value) {
        if (this.props.onSelect) {
            this.props.onSelect(event, value);
        }
    }

    render() {
        const {
            selected,
            name,
            onChange
        } = this.props;

        return (
            <div>
                { React.Children.map(this.props.children, child =>
                    React.cloneElement(child, {
                        onChange: this.handleSelect,
                        checked: valueCompare(selected, child.props.value),
                        name: name
                    })
                ) }
            </div>
        );
    }
}

第二个测试失败:期望值为3,但返回值为1.当我使用console.log(checkboxGroupNode)时,它看起来像是从元素开始。如果我可以到达ReactDomComponent的_renderedChildren,我会有3个孩子。

HTMLDivElement {
      '__reactInternalInstance$2oq0ezu6d76f7k2ux2n0e2vs4i':
       ReactDOMComponent {
         _currentElement:
          { '$$typeof': Symbol(react.element),
            type: 'div',
            key: null,
            ref: null,
            props: [Object],
            _owner: [Object],
            _store: {} },
         _tag: 'div',
         _namespaceURI: 'http://www.w3.org/1999/xhtml',
         _renderedChildren: { '.0': [Object], '.1': [Object], '.2': [Object] },
         _previousStyle: null,
         _previousStyleCopy: null,
         _hostNode: [Circular],
         _hostParent: null,
         _rootNodeID: 1,
         _domID: 1,
         _hostContainerInfo:
          { _topLevelWrapper: [Object],
            _idCounter: 17,
            _ownerDocument: [Object],
            _node: HTMLDivElement {},
            _tag: 'div',
            _namespaceURI: 'http://www.w3.org/1999/xhtml',
            _ancestorInfo: [Object] },
         _wrapperState: null,
         _topLevelWrapper: null,
         _flags: 1,
         _ancestorInfo:
          { current: [Object],
            formTag: null,
            aTagInScope: null,
            buttonTagInScope: null,
            nobrTagInScope: null,
            pTagInButtonScope: null,
            listItemTagAutoclosing: null,
            dlItemTagAutoclosing: null },
         _contentDebugID: null,
         _mountIndex: 0,
         _mountImage: null,
         _debugID: 2 } }  

该代码大约在一年前编写 - 失败可能是因为React和/或Jest的更新,或其他原因。我确信测试在一个时间点过去了。

javascript reactjs jestjs
1个回答
0
投票

经过进一步调查,我发现期望应该是:

expect(checkboxGroupNode.childNodes[0].children.length).toEqual(3);

这是Jest / React更改的结果,还是首先测试错误?

© www.soinside.com 2019 - 2024. All rights reserved.