如何正确使用react-testing-library中的getByTestId查询?

huangapple go评论53阅读模式
英文:

Ho do I use a query getByTestId from react-testing-library properly?

问题

I need my SideBar component to pass the test with getByTestId query.

我的SideBar组件需要通过getByTestId查询来通过测试。

You can try adding the data-testid attribute to a different element within your SideBar component, such as the button element, to ensure that there is an element with the specified data-testid attribute to be found by the test. Here's an updated version of your SideBar component with the data-testid attribute added to the button:

你可以尝试将data-testid属性添加到SideBar组件中的不同元素,例如按钮元素,以确保存在具有指定data-testid属性的元素可以被测试找到。以下是更新后的SideBar组件版本,其中将data-testid属性添加到按钮上:

export const SideBar: FC<SideBarPropsInterface> = ({ className }) => {
    const [collapsed, setCollapsed] = useState(false)

    const handleToggle = (): void => {
        setCollapsed((prev) => !prev)
    }

    const classNameChecked = className ?? ''

    return (
        <div
            className={classNames(
                styles.sideBar,
                { [styles.collapsed]: collapsed },
                [classNameChecked]
            )}
        >
            <button data-testid="sidebar-toggle" onClick={handleToggle}>toggle</button>
            <div className={styles.switchers}>
                <ThemeSwitcher />
                <LangSwitcher />
            </div>
        </div>
    )
}

And in your test, update the getByTestId query to match the new data-testid attribute value:

在你的测试中,更新getByTestId查询以匹配新的data-testid属性值:

describe('SideBar', () => {
    test('render', () => {
        render(<SideBar/>)
        screen.debug()
        expect(screen.getByTestId('sidebar-toggle')).toBeInTheDocument()
    })
})

By adding the data-testid attribute to the button element, you ensure that there is an element with the specified data-testid attribute that can be found during testing. This should help your SideBar component pass the test with getByTestId query.

英文:

I'm trying to test my components with react-testing library.

Some of my components are good with getByTestId query but others aren't.

Examlpe:
SideBar component code

export const SideBar: FC&lt;SideBarPropsInterface&gt; = ({ className }) =&gt; {
    const [collapsed, setCollapsed] = useState(false)

    const handleToggle = (): void =&gt; {
        setCollapsed((prev) =&gt; !prev)
    }

    const classNameChecked = className ?? &#39;&#39;

    return (
        &lt;div
            data-testid=&quot;sidebar&quot;
            className={classNames(
                styles.sideBar,
                { [styles.collapsed]: collapsed },
                [classNameChecked]
            )}
        &gt;
            &lt;button onClick={handleToggle}&gt;toggle&lt;/button&gt;
            &lt;div className={styles.switchers}&gt;
                &lt;ThemeSwitcher /&gt;
                &lt;LangSwitcher /&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    )
}

Test for SideBar

describe(&#39;SideBar&#39;, () =&gt; {
    test(&#39;render&#39;, () =&gt; {
        render(&lt;SideBar/&gt;)
        screen.debug()
        expect(screen.getByTestId(&#39;sidebar&#39;)).toBeInTheDocument()
    })
})

Error

TestingLibraryElementError: Unable to find an element by: [data-testid=&quot;sidebar&quot;]

Ignored nodes: comments, script, style
&lt;body&gt;
  &lt;div /&gt;
&lt;/body&gt;

As you can see test renders just div without attributes.

But my other components are good with it. Like Button component

Button component code

export const Button: FC&lt;ButtonPropsInterface&gt; = ({
    children,
    className,
    theme,
    ...otherProps
}) =&gt; {
    
    const classNameChecked = className ?? &#39;&#39;

    return (
        &lt;button
            data-testid=&quot;special&quot;
            className={classNames(styles.button, {}, [
                classNameChecked,
                styles[theme as ThemeButton]
            ])}
            {...otherProps}
        &gt;
            {children}
        &lt;/button&gt;
    )
}

Test for Button

describe(&#39;Button&#39;, () =&gt; {
    test(&#39;render&#39;, () =&gt; {
        render(&lt;Button&gt;test&lt;/Button&gt;)
        screen.debug()
        expect(screen.getByTestId(&#39;special&#39;)).toBeInTheDocument()
    })
})

Passed(screen.debug)

  console.log
    &lt;body&gt;
      &lt;div&gt;
        &lt;button
          class=&quot;button undefined&quot;
          data-testid=&quot;special&quot;
        &gt;
          test
        &lt;/button&gt;
      &lt;/div&gt;
    &lt;/body&gt;

Question: what am I doing wrong? I need my SideBar component to pass the test with getByTestId query

答案1

得分: 0

Ok. 我找到了一个解决方案。如果您在父组件内有子组件,您需要手动模拟它们。

在我的情况下,我在<SideBar/>内有<LangSwitcher/><ThemeSwitchers/>

RTL 在我模拟之前看不到它们:

jest.mock("widgets/LangSwitcher")
jest.mock("widgets/ThemeSwitcher")

describe('SideBar', () => {
    test('render', () => {
        render(<SideBar/>)
        screen.debug()
        expect(screen.getByTestId('sidebar')).toBeInTheDocument()
    })
})
英文:

Ok. I found a solution. If you have child components inside your parent component, you need to mock them manually.

In my case I have &lt;LangSwitcher/&gt; and &lt;ThemeSwitchers/&gt; inside &lt;SideBar/&gt;

RTL doesn't see them until I mock

jest.mock(&quot;widgets/LangSwitcher&quot;)
jest.mock(&quot;widgets/ThemeSwitcher&quot;)

describe(&#39;SideBar&#39;, () =&gt; {
    test(&#39;render&#39;, () =&gt; {
        render(&lt;SideBar/&gt;)
        screen.debug()
        expect(screen.getByTestId(&#39;sidebar&#39;)).toBeInTheDocument()
    })
})

huangapple
  • 本文由 发表于 2023年6月22日 16:08:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定