【问题标题】:Test that a component does not render when passed a certain prop value with enzyme使用酶测试组件在传递某个道具值时不渲染
【发布时间】:2017-06-10 00:02:06
【问题描述】:

我想测试我的 LoadingIndicator 组件是否在 status 属性设置为 FETCHING 时加载到 DOM 中,并且在它准备好时不加载。现在我有这个:

import React from 'react';
import { assert } from 'chai';
import { shallow, mount } from 'enzyme';
import LoadingIndicator from '../../src/components/LoadingIndicator';
import { STATUSES } from '../../src/reducers/status';

describe('LoadingIndicator', () => {
  it('should render when the status is FETCHING', () => {
    const wrapper = shallow(<LoadingIndicator status={STATUSES.FETCHING} />);
    assert.ok(wrapper);
  });

  it('should not render when the status is READY', () => {
    const wrapper = mount(<LoadingIndicator status={STATUSES.READY} />);
    assert.isNotOkay(wrapper);
  });
});

组件本身如下所示:

import React, { PropTypes } from 'react';
import CircularProgress from 'material-ui/CircularProgress';
import { STATUSES } from '../reducers/status';
import '../styles/LoadingIndicator.scss';

export default class LoadingIndicator extends React.Component {
  render() {
    return (
      this.props.status === STATUSES.READY ? null :
      <div className="loader-div">
        <CircularProgress className="loader" />
      </div>
    );
  }
}

LoadingIndicator.propTypes = {
  status: PropTypes.string.isRequired,
};

这似乎不是检查它是否呈现的正确方法,但我明白了

  1) LoadingIndicator should not render when the status is READY:
     AssertionError: expected { Object (component, root, ...) } to be falsy

知道如何根据给定的属性正确测试此组件是否加载吗?

谢谢。

【问题讨论】:

    标签: javascript reactjs mocha.js chai enzyme


    【解决方案1】:

    嗨,所以我最终自己解决了这个问题。我应该想到这样一个事实,因为我正在做一个浅渲染,我可以只看 div 是否渲染,所以我最终得到:

    import React from 'react';
    import { expect } from 'chai';
    import { shallow } from 'enzyme';
    import LoadingIndicator from '../../src/components/LoadingIndicator';
    import { STATUSES } from '../../src/reducers/status';
    
    describe('LoadingIndicator', () => {
      it('should render when the status is FETCHING', () => {
        const wrapper = shallow(<LoadingIndicator status={STATUSES.FETCHING} />);
        expect(wrapper.find('div')).to.have.length(1);
      });
    
      it('should not render when the status is READY', () => {
        const wrapper = shallow(<LoadingIndicator status={STATUSES.READY} />);
        expect(wrapper.find('div')).to.have.length(0);
      });
    });
    

    并且有效(对两者都进行了正面/负面测试)。

    【讨论】:

      【解决方案2】:

      您可以查看The Component Lifecycle。 尤其是shouldComponentUpdate()

      由此,您可以简单地添加

      shouldComponentUpdate(nextProps, nextState) {
          if (...) {
            return true;
          }
          else {
            return false;
          }
        }
      

      决定是否渲染这个组件。

      【讨论】:

      • 我不相信 Enzyme 可以支持这一点,我真正想做的是看看它是否在测试中返回元素或'null'
      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      相关资源
      最近更新 更多