当前位置: 首页 > 面试题库 >

无法检查Expect(elm).not.toBeVisible()的语义UI反应组件

有翰海
2023-03-14
问题内容

我正在尝试测试React组件并使用expect(elm).not.toBeVisible()失败。

更新3

我将代码缩减为以下更简单的形式:

// ./TestItem.js
import React from 'react'
import './TestItem.css'

export default ({ hide }) => {
  return <div className={hide ? 'shouldHide' : ''}>Text</div>
}

// ./__tests__/TestItem.test.js
import React from 'react'
import { render } from 'react-testing-library'
import TestItem from '../TestItem'
import 'jest-dom/extend-expect'
import 'react-testing-library/cleanup-after-each'


test.only('TestItem should render correctly', async () => {
  const { getByText, debug } = render(<TestItem hide={true} />)
  const itemNode = getByText('Text')
  debug()
  expect(itemNode).not.toBeVisible()
})

// ./TestItem.css
.shouldHide {
  display: none;
}

测试结果:

 TestItem should render correctly

    expect(element).not.toBeVisible()

    Received element is visible:
      <div class="shouldHide" />

       7 |   const itemNode = getByText('Text')
       8 |   debug()
    >  9 |   expect(itemNode).not.toBeVisible()
         |                        ^
      10 | })
      11 |

debug() 日志:

console.log node_modules/react-testing-library/dist/index.js:58
    <body>
      <div>
        <div
          class="shouldHide"
        >
          Text
        </div>
      </div>
    </body>

更新2:

好的,这变得非常奇怪,因为我通过了测试以通过codesanbox,但是在本地计算机上仍然没有运气。

我原来的问题:

我使用React,语义UI反应和react-testing-library。

这是代码:

// ComboItem.test.js    
import React from 'react'
import ComboItem from '../ComboItem'
import { render } from 'react-testing-library'
import comboXoi from '../images/combo-xoi.jpg'
import 'path/to/semantic/semantic.min.css'

describe('ComboItem', () => {
  test('should render', async () => {
    const { getByText, debug } = render(
      <ComboItem image={comboXoi} outOfStock={false} />
    )
    const outOfStockNotice = getByText('Out of stock')
    debug()
    expect(outOfStockNotice).not.toBeVisible()
  })
})

// ComboItem.js
import React from 'react'
import { Card, Image } from 'semantic-ui-react'

export default ({ image, outOfStock = false }) => {
  return (
    <Card>
      <Image
        src={image}
        dimmer={{
          active: outOfStock,
          inverted: true,
          'data-testid': 'combo-item-dimmer',
          content: (
            <span style={{ marginTop: 'auto', color: 'black' }}>
               Out of stock
            </span>
          ),
        }}
      />
    </Card>
  )
}

我得到的是这里的结果:

ComboItem › should render

    expect(element).not.toBeVisible()

    Received element is visible:
      <span style="margin-top: auto; color: black;" />

      at Object.test (src/app/screens/App/screens/SaleEntries/screens/CreateSaleEntry/screens/StickyRiceComboSelect/__tests__/ComboItem.test.js:14:34)
      at process._tickCallback (internal/process/next_tick.js:68:7)

我试图查看组件在浏览器上的渲染结果,并且outOfStockNotice测试代码中的节点实际上是隐藏的,因为它的父级(dimmer具有类的div
具有style)display: none

根据jest-domdoc(由testing-react-library

可见

如果满足以下所有条件,则元素是可见的:

  • 它没有将其css属性显示设置为none
  • 它没有将其CSS属性可见性设置为隐藏或折叠
  • 它没有将其CSS属性不透明度设置为0
  • 其父元素也是可见的(依此类推直到DOM树的顶部)

请帮忙。我真的不知道这里可能出什么问题。

更新:

我将debug()这里的结果包括在内:

console.log node_modules/react-testing-library/dist/index.js:58
      <body>
        <div>
          <div
            class="ui card"
          >
            <div
              class="ui image"
            >
              <div
                class="ui inverted dimmer"
                data-testid="combo-item-dimmer"
              >
                <div
                  class="content"
                >
                  <span
                    style="margin-top: auto; color: black;"
                  >
                    Out of stock
                  </span>
                </div>
              </div>
              <img
                src="combo-xoi.jpg"
              />
            </div>
          </div>
        </div>
      </body>

问题答案:

根据他自己的作者,这是答案react- testing-library

可能是JSDOM限制(在codeandbox中,它在实际的浏览器中运行)。实际上,问题在于css并未真正加载到JSDOM中的文档中。如果是这样,那将起作用。如果您想出一个自定义的笑话转换,可以在测试过程中将css文件插入文档中,那么您将被设置。

因此,如果您使用CSS-in-JS,这将起作用。

因此,基本上该import './TestItem.css'测试中的部分将无法正常工作,因为JSDOM不会加载它,因此jest- dom无法理解类的shouldHide均值display: none

更新:

根据这个,您可以将CSS插入jsdom中:

import React from 'react'
import { render } from 'react-testing-library'
import TestItem from '../TestItem'

import fs from 'fs'
import path from 'path'

test.only('TestItem should render correctly', async () => {
  const cssFile = fs.readFileSync(
    path.resolve(__dirname, '../TestItem.css'),
    'utf8'
  )
  const { container, getByText, debug } = render(<TestItem hide={true} />)

  const style = document.createElement('style')
  style.type = 'text/css'
  style.innerHTML = cssFile
  container.append(style)

  const itemNode = getByText('Text')
  debug()
  expect(itemNode).not.toBeVisible()
})

然后测试应该通过。



 类似资料:
  • 问题内容: 我目前正在开发一个带有嵌入式文本编辑器的程序。用户应该在编辑器中键入Java代码。然后,将输入编辑器中的代码制成字符串。我只想要一些可以检查缺少括号的内容或不带钩子的尝试,等等。它不需要编译。我已经看了很多遍,但是我还是编程新手,无法实现一些较难的东西。 因此,使其更简短:我正在寻找一些Java软件包,该软件包将分析代码中的语法错误。 问题答案: 从Java 6开始,您可以使用Java

  • 在我的普通React类组件中,在返回条件html呈现之前,我在render()方法中做了一些检查。现在,我正在使用一个react函数组件,它显然没有render()方法。。。我如何在这里进行条件检查?就在普通函数中,然后从这些函数返回html代码? e、 g类组件: 在功能组件中?:

  • 例子 $ cat foo.c union u { char c; int i; } $ gcc -fsyntax-only foo.c foo.c:4:1: error: expected identifier or ‘(’ at end of input 技巧 如上所示,使用-fsyntax-only选项可以只做语法检查,不进行实际的编译输出。 详情参见 gcc手册

  • 我如何确认通过props(例如)接收的反应元素在我的渲染方法中属于给定类型? 假设我有一个元素和一个元素。在的render方法中,我想查找传递的所有子项,并对s中的任何子项执行一些特殊的操作。 我确实找到了一个可行的实现,但只是在反复试验之后。请参阅下面的代码。(第15.4.2节) 我ist.jsx 名单我tem.jsx 所以,最后一个实现似乎可以工作,但是为什么第一个实现不能工作呢?我在reac

  • 我见过多个使用Typescript的React组件示例: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++组件 当我们不使用道具或国家时,似乎没有一个明确的惯例。 人们将这些类型设置为,,,, ,等等。这是我到目前为止看到的:

  • 我正在检查一个屏幕上iOS应用程序,但它不提供该屏幕上的任何元素。 Appium inspector未在该屏幕上提供任何对象。请帮忙。我被困在这里了。如果有其他方法检查iOS屏幕,请建议。 我正在mac中使用Appium Desktop v1.9.0 https://i.stack.imgur.com/HZVkf.png