【问题标题】:React Test case for the button disabled or not on the basis of state value根据状态值对按钮是否禁用反应测试用例
【发布时间】:2019-02-11 05:28:33
【问题描述】:

我正在尝试编写启用或禁用按钮的测试用例。

<button type="button" id="addBtn" className={`btn border-0 create-job-btn ${enabled ? 'add-btn' : 'jd-button'}`} disabled={!enabled} >Add/Paste Jd</button>
<span className="pl-1 pr-1"> or </span>
<button type="button" id="uploadBtn" className={`btn border-0 create-job-btn ${enabled ? "upload-btn" : "jd-button"}`} disabled={!enabled} >Upload Jd File</button>

现在,我尝试的是,

it('should have buttons', () => {
        const wrapper = shallow(<SelectCriteraNewJob />);
        expect(wrapper.find('button#addBtn')).toBeTruthy();
        expect(wrapper.find('button#uploadBtn')).toBeTruthy();
    });

现在,

const enabled = (!!selectedTechnology && selectedTechnology.length > 0) && (!!userCompany && userCompany.length > 0)

所以,我在为按钮 enable and disable 编写测试用例时感到困惑。

那么,任何人都可以帮我解决这个问题吗?谢谢。

【问题讨论】:

  • 你可以窥探对象 selectedTechnology 和 userCompany 并检查它
  • 被选中的Technology 和userCompany 存储在您所在的州吗?或者它是作为道具传递还是作为常量给出?
  • 这是存储状态
  • const { selectedTechnology } = this.state;常量 { userCompany } = this.props; const enabled = (!!selectedTechnology && selectedTechnology.length > 0) && (!!userCompany && userCompany.length > 0)
  • @Harvey 这是我使用的方式

标签: reactjs unit-testing jestjs


【解决方案1】:

观察你的组件呈现什么,似乎有三件事要测试:

  1. 它应该将 2 个按钮呈现为子项
  2. 这些按钮何时启用或禁用
  3. 这些组件的类名

主要测试用例是“启用”用例。是否测试 classNames 取决于您,几乎不需要测试内联样式或 css 类。

如果没有给出userCompany 和/或selectedTechnology,似乎两个按钮都被禁用,如果它们都给出,则两个按钮都被启用。

你可以根据你给组件的 state & prop 观察 html 按钮的 'disabled' 属性。

const wrapper = shallow(<SelectCriteraNewJob userCompany='Some Company' />);

it('SelectCriteraNewJob should render two buttons as children', () => {
    expect(wrapper.childAt(0).type()).toEqual('button');
    // expect(wrapper.childAt(1).type()).toEqual('span'); if you want to, not necessary to test
    expect(wrapper.childAt(2).type()).toEqual('button');
});

describe('If technology and company are not empty, both buttons are enabled. Otherwise disabled.',() => {
    expect(wrapper.find('button#addBtn')).toBeTruthy();
    expect(wrapper.find('button#uploadBtn')).toBeTruthy();

    test('Company and technology given', () => {
        wrapper.setState({selectedTechnology: 'React'});
        const addBtn = wrapper.find('button#addBtn').prop('disabled');
        const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
        expect(addBtn).toBeFalsy();
        expect(uploadBtn).toBeFalsy();
    });
    test('Only technology given', () => {
        wrapper.setProps({userCompany:''});
        const addBtn = wrapper.find('button#addBtn').prop('disabled');
        const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
        expect(addBtn).toBeTruthy();
        expect(uploadBtn).toBeTruthy();
    });
    test('Only company given', () => {
        wrapper.setState({selectedTechnology:''});
        wrapper.setProps({userCompany:'Some Company'});
        const addBtn = wrapper.find('button#addBtn').prop('disabled');
        const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
        expect(addBtn).toBeTruthy();
        expect(uploadBtn).toBeTruthy();
    });
    test('Neither are given', () => {
        wrapper.setState({selectedTechnology:''});
        wrapper.setProps({userCompany:''});
        const addBtn = wrapper.find('button#addBtn').prop('disabled');
        const uploadBtn = wrapper.find('button#uploadBtn').prop('disabled');
        expect(addBtn).toBeTruthy();
        expect(uploadBtn).toBeTruthy();
        expect(wrapper.find('button.jd-button')).toHaveLength(2); //optional, test css class name
    });
});

【讨论】:

  • 好的,这样我们就可以在测试用例中使用状态值了。不仅非常感谢您的回答,而且您还教会了我一个新事物。所以,这样我可以测试使用本地状态的组件。现在,如果用户公司的价值来自我使用同一组件中的 mapStateToProps 的减速器,该怎么办
  • 我没有对 react + redux 一起做太多的测试,只是单独测试它们进行单元测试。但是这个博客可能会帮助你jsramblings.com/2018/01/15/…
  • 我总是收到这个错误。方法“props”意味着在 1 个节点上运行。找到了 0 个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多