简体   繁体   English

测试React Component时,酶会忽略某些道具值

[英]Enzyme ignores certain prop values when testing React Component

Can anyone explain the following test in jest for me? 谁能解释在下面的测试笑话我吗?

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');
});

Why does wrapper.prop('textAnchor') expect a 'start', which it passes, instead of 'end'? 为什么wrapper.prop('textAnchor')期望传递的是“开始”,而不是“结束”? I can actually change 'end' to other random text, and the test still passes. 我实际上可以将“结束”更改为其他随机文本,并且测试仍然可以通过。

According to the filenames you mentioned in your post, I think you have a typo in an import in StateIdTxt.test.js : 根据您在帖子中提到的文件名,我认为您在StateIdTxt.test.js的导入过程中StateIdTxt.test.js了错字:

import StateIdTxt from './StateCoordTxt.jsx';

Looks like it should be 看起来应该是

import StateIdTxt from './StateIdTxt.jsx';

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

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