React测试库:测试布尔状态

发布于 2025-02-13 09:03:45 字数 1846 浏览 0 评论 0原文

我目前正在使用Testing-library/React软件包编写测试。

在我的测试的第二次点击中,文本不应在文档中,而应在文档中。

这是我正在测试的组件:

import { useState } from 'react'
import { InfoType } from '../../constants'

type Props = {
    type: InfoType
}

const HelpWidget = ({ type }: Props) => {
    const [isHover, setHover] = useState<boolean>(false)

    return (
        <div className="help-widget">
            <img
                src={require('../../../public/images/info.svg')}
                onMouseEnter={() => setHover(true)}
                onMouseOut={() => setHover(false)}
                onClick={() => setHover(!isHover)}
            />
            {isHover ? <div className="info">{type}</div> : <div></div>}
        </div>
    )
}

export default HelpWidget

这是我写的测试:

import React from 'react'
import { screen, render, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import HelpWidget from '../components/helpWidget/helpWidget'


// Testing if the HelpWidget conditionally renders based on the isHover boolean state.
test('helpWidget conditionally renders to the page', () => {
    const epk4Post = 'Select a persona to post this'
    render(<HelpWidget type={epk4Post} />)
    const imageElement = screen.getByRole("img")
    userEvent.click(imageElement)
    screen.debug()
    expect(screen.getByText(/select a persona to post this/i)).toBeInTheDocument()
    userEvent.click(imageElement)
    screen.debug()
});

这是我运行screen.debug()查看DOM输出时得到的。在这里,您可以清楚地看到,在第二次单击时,当文档应 不是 BE时,文本仍在文档中。

请分享您的想法!谢谢。

I am currently writing a test using the testing-library/react package.

On the second click of my test, the text should not be in the document, but it is.

Here is the component I am testing:

import { useState } from 'react'
import { InfoType } from '../../constants'

type Props = {
    type: InfoType
}

const HelpWidget = ({ type }: Props) => {
    const [isHover, setHover] = useState<boolean>(false)

    return (
        <div className="help-widget">
            <img
                src={require('../../../public/images/info.svg')}
                onMouseEnter={() => setHover(true)}
                onMouseOut={() => setHover(false)}
                onClick={() => setHover(!isHover)}
            />
            {isHover ? <div className="info">{type}</div> : <div></div>}
        </div>
    )
}

export default HelpWidget

Here is the test I wrote:

import React from 'react'
import { screen, render, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import HelpWidget from '../components/helpWidget/helpWidget'


// Testing if the HelpWidget conditionally renders based on the isHover boolean state.
test('helpWidget conditionally renders to the page', () => {
    const epk4Post = 'Select a persona to post this'
    render(<HelpWidget type={epk4Post} />)
    const imageElement = screen.getByRole("img")
    userEvent.click(imageElement)
    screen.debug()
    expect(screen.getByText(/select a persona to post this/i)).toBeInTheDocument()
    userEvent.click(imageElement)
    screen.debug()
});

And here is what I get when I run screen.debug() to see the DOM output. Here you can clearly see that on the second click, the text is still within the document when it should not be:
enter image description here

Please share your thoughts! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文