@@ -8,13 +8,14 @@ title: Using findByText
88// This is an example of how to use findByText to query for text that
99// is not visible right away
1010
11- import {getByRole , findByText , getByPlaceholderText } from ' @testing-library/dom'
11+ import {screen } from ' @testing-library/dom'
1212import userEvent from ' @testing-library/user-event'
1313// provides a set of custom jest matchers that you can use to extend jest
1414// i.e. `.toBeVisible`
1515import ' @testing-library/jest-dom'
1616
17- const renderContent = el => {
17+ function renderApp () {
18+ const el = document .body .appendChild (document .createElement (' div' ))
1819 el .innerHTML = `
1920 <form id =' login_form' method =' post' name =' login' >
2021 <label for =' username' >User Name:</label >
@@ -64,8 +65,8 @@ const renderContent = el => {
6465 const userInput = el .querySelector (' #username_input' )
6566 const passwordInput = el .querySelector (' #password_input' )
6667
67- var userName = userInput .value
68- var password = passwordInput .value
68+ const userName = userInput .value
69+ const password = passwordInput .value
6970 if (! userName) {
7071 el .querySelector (' #username_required_error' ).style .display = ' inline'
7172 }
@@ -88,51 +89,47 @@ const renderContent = el => {
8889 window .history .back ()
8990 })
9091
91- return el
92+ return {user : userEvent . setup ()}
9293}
9394
94- describe (' findByText Examples' , () => {
95- let div
96- let container
97-
98- beforeEach (() => {
99- div = document .createElement (' div' )
100- container = renderContent (div)
101- })
95+ afterEach (() => (document .body .innerHTML = ` ` ))
10296
97+ describe (' findByText Examples' , () => {
10398 it (' should show a required field warning for each empty input field' , async () => {
104- userEvent .click (
105- getByRole (container, ' button' , {
99+ const {user } = renderApp ()
100+ await user .click (
101+ screen .getByRole (' button' , {
106102 name: ' Login' ,
107103 }),
108104 )
109105
110- expect (await findByText (container, ' User Name Required' )).toBeVisible ()
106+ expect (await screen . findByText (' User Name Required' )).toBeVisible ()
111107
112- expect (await findByText (container, ' Password Required' )).toBeVisible ()
108+ expect (await screen . findByText (' Password Required' )).toBeVisible ()
113109 })
114110
115111 it (' should show invalid field errors for each invalid input field' , async () => {
116- const userNameField = getByPlaceholderText (container, ' Enter user name' )
117- const passwordField = getByPlaceholderText (container, ' Enter password' )
112+ const {user } = renderApp ()
113+ const userNameField = screen .getByPlaceholderText (' Enter user name' )
114+ const passwordField = screen .getByPlaceholderText (' Enter password' )
118115
119- expect (await findByText (container, ' Invalid Password' )).not .toBeVisible ()
120- expect (await findByText (container, ' Invalid User Name' )).not .toBeVisible ()
116+ expect (await screen . findByText (' Invalid Password' )).not .toBeVisible ()
117+ expect (await screen . findByText (' Invalid User Name' )).not .toBeVisible ()
121118
122- userEvent .type (userNameField, ' Philchard' )
123- userEvent .type (passwordField, ' theCat' )
119+ await user .type (userNameField, ' Philchard' )
120+ await user .type (passwordField, ' theCat' )
124121
125122 expect (userNameField).toHaveValue (' Philchard' )
126123 expect (passwordField).toHaveValue (' theCat' )
127124
128- userEvent .click (
129- getByRole (container, ' button' , {
125+ await user .click (
126+ screen . getByRole (' button' , {
130127 name: ' Login' ,
131128 }),
132129 )
133130
134- expect (await findByText (container, ' Invalid User Name' )).toBeVisible ()
135- expect (await findByText (container, ' Invalid Password' )).toBeVisible ()
131+ expect (await screen . findByText (' Invalid User Name' )).toBeVisible ()
132+ expect (await screen . findByText (' Invalid Password' )).toBeVisible ()
136133 })
137134})
138135```
0 commit comments