【问题标题】:Enzyme ignores certain prop values when testing React ComponentEnzyme 在测试 React 组件时会忽略某些 prop 值
【发布时间】:2017-02-23 21:37:25
【问题描述】:

谁能帮我解释一下jest中的以下测试?

StateIdText.jsx

import React, { Component, PropTypes } from 'react';

class StateIdTxt extends Component {
    propTypes: {
        x: PropTypes.number.isRequired,
        y: PropTypes.number.isRequired,
        stateId: PropTypes.number.isRequired
    }

    render() {
        const {x, y, stateId} = this.props;
        return (
                <text x={x} y={y}
                      textAnchor="end"
                      dominantBaseline="text-before-edge"
                      fontSize=".7em" fill="blue">
                    {stateId}
                </text>
        );
    }
}

export default StateIdTxt;

StateIdTxt.test.js

import React from 'react';
import {mount, render, shallow} from 'enzyme';

import StateIdTxt from './StateCoordTxt.jsx';

it('shallow <StateIdTxt />', () => {
    const wrapper = shallow(<StateIdTxt x={1.5} y={2.1} stateId={99} />);
    expect(wrapper.prop('x')).toEqual(1.5);
    expect(wrapper.prop('y')).toEqual(2.1);
    expect(wrapper.prop('stateId')).toBeUndefined();

    expect(wrapper.prop('textAnchor')).toBe('start'); // how come not end?
    expect(wrapper.prop('dominantBaseline')).toBe('text-before-edge');
    expect(wrapper.prop('fontSize')).toBe('.7em');
    expect(wrapper.prop('fill')).toBe('blue');
});

为什么wrapper.prop('textAnchor') 期望它通过的“开始”而不是“结束”?我实际上可以将“结束”更改为其他随机文本,并且测试仍然通过。

【问题讨论】:

    标签: javascript reactjs jestjs enzyme


    【解决方案1】:

    根据您在帖子中提到的文件名,我认为您在StateIdTxt.test.js 的导入中有错字:

    import StateIdTxt from './StateCoordTxt.jsx';
    

    应该是这样的

    import StateIdTxt from './StateIdTxt.jsx';
    

    【讨论】:

      猜你喜欢
      • 2019-07-04
      • 2017-02-12
      • 1970-01-01
      • 2018-12-16
      • 2018-08-20
      • 2013-08-10
      • 2020-12-04
      • 2010-12-08
      • 2016-02-25
      相关资源
      最近更新 更多