ByDisplayValue

getByDisplayValue, queryByDisplayValue, getAllByDisplayValue, queryAllByDisplayValue, findByDisplayValue, findAllByDisplayValue

API

javascript 复制代码
getByDisplayValue(
  // If you're using `screen`, then skip the container argument:
  container: HTMLElement,
  value: TextMatch,
  options?: {
    exact?: boolean = true,
    normalizer?: NormalizerFn,
  }): HTMLElement

返回具有匹配显示值的 input、textarea 或 select 元素。

input tags

html 复制代码
<input type="text" id="lastName" />
javascript 复制代码
document.getElementById('lastName').value = 'Norris'
javascript 复制代码
// HTML
import {screen} from '@testing-library/dom'

const lastNameInput = screen.getByDisplayValue('Norris')


// React
import {render, screen} from '@testing-library/react'

render(<MyComponent />)
const lastNameInput = screen.getByDisplayValue('Norris')

textarea tags

javascript 复制代码
<textarea id="messageTextArea" />

document.getElementById('messageTextArea').value = 'Hello World'
javascript 复制代码
// HTML
import {screen} from '@testing-library/dom'

const messageTextArea = screen.getByDisplayValue('Hello World')


// React
import {render, screen} from '@testing-library/react'

render(<MyComponent />)
const messageTextArea = screen.getByDisplayValue('Hello World')

select tags

如果是select,这将搜索一个匹配给定 TextMatch 的选定<option>的<select>。

html 复制代码
<select>
  <option value="">State</option>
  <option value="AL">Alabama</option>
  <option selected value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
</select>
javascript 复制代码
// HTML
import {screen} from '@testing-library/dom'

const selectElement = screen.getByDisplayValue('Alaska')



// React
import {render, screen} from '@testing-library/react'

render(<MyComponent />)
const selectElement = screen.getByDisplayValue('Alaska')
相关推荐
lyj1689979 分钟前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽2 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头2 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
墨菲安全3 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing3 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆3 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
我在北京coding4 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js
Eiceblue4 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
前端开发与ui设计的老司机4 小时前
UI前端与数字孪生结合实践探索:智慧物流的货物追踪与配送优化
前端·ui