【问题标题】:Test case to check if Button onClick opens a new tab测试用例检查 Button onClick 是否打开新选项卡
【发布时间】:2018-04-19 07:46:14
【问题描述】:

我是 UI 开发的新手,最近我开始使用 Typescript 在 React 中编写代码。 我创建了一个按钮,单击该按钮后,它会打开一个菜单,其中包含一些可供选择的选项。如果我单击其中一个选项,将打开一个带有一些目标 URL 的新选项卡。我在用 window.open(target_URL, "_blank"); 实现这一目标。

现在我正在为我的代码编写测试用例,我想编写一个测试用例来检查我是否单击该选项,它会打开一个带有 target_URL 的新选项卡。

const myButton: HTMLElement = TestUtils.findRenderedDOMComponentWithClass(renderedComponent, "my-button") as HTMLElement;
    expect(myButton).to.not.be.undefined;

    // Click the Button and check number of options displayed
    TestUtils.Simulate.click(myButton);
    const menuItems: HTMLElement[] = TestUtils.scryRenderedDOMComponentsWithClass(renderedComponent, "ms-ContextualMenu-item") as HTMLElement[];
    expect(menuItems).to.not.be.undefined;
    expect(menuItems.length).to.be.equal(4, "4 options dispalyed");

    const menuItem1: HTMLElement = TestUtils.findRenderedDOMComponentWithClass(renderedComponent, "menu-item-1") as HTMLElement;
    TestUtils.Simulate.click(menuItem1);

上面的代码我已经写好了,但是我不知道如何完成这个来检查在浏览器中打开一个新标签的情况。

【问题讨论】:

    标签: reactjs typescript sinon chai karma-mocha


    【解决方案1】:

    window.open() 方法将返回窗口对象,你可以检查那个寡妇: 例如,只需尝试关闭窗口。
    一般只能关闭自己创建的窗口,否则会报错。

    【讨论】:

    • 不鼓励测试实施细节吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 2023-03-30
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多