首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jest office-ui-fabric-react组件进行测试?

如何使用jest office-ui-fabric-react组件进行测试?
EN

Stack Overflow用户
提问于 2019-03-01 09:07:51
回答 1查看 912关注 0票数 0

我一直试图在我的react项目中测试呼号组件。

为了简化,以下是React呈现组件:

代码语言:javascript
复制
    <div className="UserInfoDiv">
        <div ref={this.menuButtonElement}>
            <ActionButton id="toggleCallout"
                onClick={changeIsCallOutVisibleProptoTrue}
                text="Show Callout" />
        </div>
        <Callout
            className="calloutClass1"
            target={this.menuButtonElement.current}
            hidden={!this.props.isCalloutVisible}>
            <div id="callOutContainer">
                <span>Need to test items here.<span>
                <button className="clickForSomeAction">Simulate Click on this</button>
            </div>
        </Callout>
    </div>

这在UI中非常好。为了在开玩笑中进行测试,我尝试了以下几点:

代码语言:javascript
复制
    userMenu = mount(<UserInfoDivComponent {...props} />);
    UserInfoDiv.find("button#toggleCallout").simulate('click');
    expect(changeIsCallOutVisibleProptoTrue.mock.calls.length).toBe(1); 
    userMenu.setProps({isCalloutVisible: true });

    // Following only gives html(included UserInfoDiv,toggleCallout)  `without html from callout`:
    console.log(userMenu.html());

我需要帮助,如何测试以下场景?

  1. 呼号是可见的?
  2. .clickForSomeAction中查找Callout.calloutClass1按钮并模拟单击

office-fabric-ui中有类似的组件(ex: DropDown,上下文菜单),它在文档中呈现HTML,而不是在当前组件HTML中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-07 06:42:55

最后,我使用ReactTestUtils测试了示例中给出的标注。

代码语言:javascript
复制
    let component:any;
    const container = document.createElement('div');
    document.body.appendChild(container);
    let threwException = false;
    try {
        component = ReactTestUtils.renderIntoDocument(<UserInfoDivComponent {...itemProps} />);
    } catch (e) {
        threwException = true;
    } 
    expect(threwException).toEqual(false); 
    const UserInfoDiv= ReactTestUtils.findRenderedDOMComponentWithClass(component, "UserInfoDiv");


    const clickForSomeAction = ReactTestUtils.findRenderedDOMComponentWithClass(component, "clickForSomeAction");
    ReactTestUtils.Simulate.click(clickForSomeAction);

由于我们不能通过ReactTestUtils直接查询querySelectors,所以很少会出现故障。

编辑1

我们可以使用XML选择器进行查询。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54941248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档