首页 > 解决方案 > 开玩笑地获取元素的子元素

问题描述

<div
    class="btnWrap"
    data-testid="footer-button"
>
    <button
    class="Polaris-Button Polaris-Button--primary"
    type="button"
    >
    <span
        class="Polaris-Button__Content"
    >
        <span
        class="Polaris-Button__Text"
        >
        Button
        </span>
    </span>
    </button>
</div>

我有这个组件并测试它我已经完成

 test("Test footer button disable false", () => {
    args.disabled = false;
    const { debug, getByTestId } = render(
      <AppProvider>
        <Footer {...args} />
      </AppProvider>
    );

    const element = getByTestId("footer-button");
    console.log(getByTestId("footer-button").closest("button"));
    expect(element.getAttribute("disabled")).toBe(null);
    debug();
  });

我想检查该按钮是否被禁用以获取我使用过的按钮

        console.log(getByTestId("footer-button").closest("button"));

但是这个的输出是空的。我怎样才能做到这一点?

在我的主要代码中,我无法将 testid 提供给按钮组件。

标签: reactjsjestjsreact-testing-library

解决方案


推荐阅读