跳至主要內容

ByPlaceholderText

getByPlaceholderText, queryByPlaceholderText, getAllByPlaceholderText, queryAllByPlaceholderText, findByPlaceholderText, findAllByPlaceholderText

API

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

這會搜尋所有具有 placeholder 屬性的元素,並找到一個符合給定的 TextMatch 的元素。

<input placeholder="Username" />
import {screen} from '@testing-library/dom'

const inputNode = screen.getByPlaceholderText('Username')

注意

placeholder 並不是 label 的好替代品,所以你通常應該改用 getByLabelText

選項

TextMatch 選項