跳至主要內容

ByDisplayValue

getByDisplayValue、queryByDisplayValue、getAllByDisplayValue、queryAllByDisplayValue、findByDisplayValue、findAllByDisplayValue

API

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

傳回具有相符顯示值的 inputtextareaselect 元素。

input 標籤

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

const lastNameInput = screen.getByDisplayValue('Norris')

textarea 標籤

<textarea id="messageTextArea" />
document.getElementById('messageTextArea').value = 'Hello World'
import {screen} from '@testing-library/dom'

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

select 標籤

針對 select,這將會搜尋所選的 <option> 符合給定的 TextMatch<select>

<select>
<option value="">State</option>
<option value="AL">Alabama</option>
<option selected value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>
import {screen} from '@testing-library/dom'

const selectElement = screen.getByDisplayValue('Alaska')

選項

TextMatch 選項