-
Notifications
You must be signed in to change notification settings - Fork 728
docs: add findByText example #616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexkrolick
merged 4 commits into
testing-library:master
from
shermanhui:add-vanilla-js-findByText-example
Oct 7, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
--- | ||
id: example-findByText | ||
title: example `findByText` | ||
sidebar_label: findByText | ||
--- | ||
|
||
```javascript | ||
// src/__tests__/example.test.js | ||
// This is an example of how to use findByText to query for text that | ||
// is not visible right away | ||
|
||
import { | ||
getByRole, | ||
findByText, | ||
getByPlaceholderText, | ||
} from '@testing-library/dom' | ||
import userEvent from '@testing-library/user-event' | ||
// provides a set of custom jest matchers that you can use to extend jest | ||
// i.e. `.toBeVisible` | ||
import '@testing-library/jest-dom' | ||
|
||
const renderContent = el => { | ||
el.innerHTML = ` | ||
<form id='login_form' method='post' name='login'> | ||
<label for='username'>User Name:</label> | ||
|
||
<input | ||
type='text' | ||
name='username' | ||
id='username_input' | ||
placeholder='Enter user name' | ||
/> | ||
|
||
<span id='username_required_error' style='color: red; display: none;'> | ||
User Name Required | ||
</span> | ||
|
||
<span id='invalid_username_error' style='color: red; display: none;'> | ||
Invalid User Name | ||
</span> | ||
|
||
<label for='password'>Password:</label> | ||
|
||
<input | ||
type='password' | ||
name='password' | ||
id='password_input' | ||
placeholder='Enter password' | ||
/> | ||
|
||
<span id='invalid_password_error' style='color: red; display: none;'> | ||
Invalid Password | ||
</span> | ||
|
||
<span id='password_required_error' style='color: red; display: none;'> | ||
Password Required | ||
</span> | ||
|
||
<button id='submit' type='submit'> | ||
Login | ||
</button> | ||
</form> | ||
` | ||
|
||
const submitButton = el.querySelector('#submit') | ||
const formEl = el.querySelector('#login_form') | ||
|
||
submitButton.addEventListener('click', () => { | ||
const userInput = el.querySelector('#username_input') | ||
const passwordInput = el.querySelector('#password_input') | ||
|
||
var userName = userInput.value | ||
var password = passwordInput.value | ||
if (!userName) { | ||
el.querySelector('#username_required_error').style.display = 'inline' | ||
} | ||
|
||
if (!password) { | ||
el.querySelector('#password_required_error').style.display = 'inline' | ||
} | ||
|
||
if (userName && userName !== 'Bob') { | ||
el.querySelector('#invalid_username_error').style.display = 'inline' | ||
} | ||
|
||
if (password && password !== 'theBuilder') { | ||
el.querySelector('#invalid_password_error').style.display = 'inline' | ||
} | ||
}) | ||
|
||
formEl.addEventListener('submit', function(evt) { | ||
evt.preventDefault() | ||
window.history.back() | ||
}) | ||
|
||
return el | ||
} | ||
|
||
describe('findByText Examples', () => { | ||
let div | ||
let container | ||
|
||
beforeEach(() => { | ||
div = document.createElement('div') | ||
container = renderContent(div) | ||
}) | ||
|
||
it('should show a required field warning for each empty input field', async () => { | ||
userEvent.click( | ||
getByRole(container, 'button', { | ||
name: 'Login', | ||
}) | ||
) | ||
|
||
expect( | ||
await findByText(container, "User Name Required") | ||
).toBeVisible(); | ||
|
||
expect( | ||
await findByText(container, "Password Required") | ||
).toBeVisible(); | ||
}) | ||
|
||
it('should show invalid field errors for each invalid input field', async () => { | ||
const userNameField = getByPlaceholderText(container, 'Enter user name') | ||
const passwordField = getByPlaceholderText(container, 'Enter password') | ||
|
||
expect(await findByText(container, 'Invalid Password')).not.toBeVisible() | ||
expect(await findByText(container, 'Invalid User Name')).not.toBeVisible() | ||
|
||
userEvent.type(userNameField, 'Philchard') | ||
userEvent.type(passwordField, 'theCat') | ||
|
||
expect(userNameField).toHaveValue('Philchard') | ||
expect(passwordField).toHaveValue('theCat') | ||
|
||
userEvent.click( | ||
getByRole(container, 'button', { | ||
name: 'Login', | ||
}) | ||
) | ||
|
||
expect(await findByText(container, 'Invalid User Name')).toBeVisible() | ||
expect(await findByText(container, 'Invalid Password')).toBeVisible() | ||
}) | ||
}) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,8 +128,9 @@ | |
], | ||
"Examples": [ | ||
"example-codesandbox", | ||
"example-external", | ||
"example-findByText", | ||
"example-input-event", | ||
"example-update-props", | ||
"example-react-context", | ||
"example-react-hooks-useReducer", | ||
"example-react-intl", | ||
|
@@ -138,7 +139,7 @@ | |
"example-reach-router", | ||
"example-react-transition-group", | ||
"example-react-modal", | ||
"example-external" | ||
"example-update-props" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reorganized these examples to be in alphabetical order |
||
], | ||
"Help": ["faq", "learning", "contributing"] | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the website README to make sure future contributors don't use non-url safe characters.