読み方のヒント
矢印ボタン(または ← → キー)でページを移動できます。
画面の中の要素は、なるべく「役割」と「名前」で探します。
const increase = screen.getByRole("button", { name: "+" });
const decrease = screen.getByRole("button", { name: "-" });
getByRole("button", { name: "+" }) は、「『+』という名前のボタン」を探します。読み上げの道具も、同じように「+、ボタン」と読みます。
| クエリ | 探し方 | 使う順 |
|---|---|---|
getByRole | 役割(ボタン、見出し、入力欄など)と名前 | 1 |
getByLabelText | ラベルの文字 | 2(フォームの入力欄) |
getByText | 表示されている文字 | 3 |
getByTestId | data-testid の値 | 最後 |
役割と名前で探せない要素は、利用者にとっても分かりにくい要素です。たとえば、<div onClick> で作ったボタンは、getByRole("button") で見つかりません。テストを書くと、アクセシビリティの問題にも気づけます。役割と名前の考え方は、カリキュラム「品質を保つ:アクセシビリティ・性能・セキュリティ」で詳しく扱います。
screen.getByRole("button", { name: "予約する" })screen.getByLabelText("お名前")(または getByRole("textbox", { name: "お名前" }))screen.getByRole("heading", { name: "商品一覧" })4 / 17