-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from testing-library/within
Add within() test example
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,34 @@ | ||
import { render, within } from '@testing-library/vue' | ||
|
||
test('within() returns an object with all queries bound to the DOM node', () => { | ||
const { getByTestId, getByText } = render({ | ||
template: ` | ||
<div> | ||
<p>repeated text</p> | ||
<div data-testid="div">repeated text</div> | ||
</div> | ||
` | ||
}) | ||
|
||
// getByText() provided by render() fails because it finds multiple elements | ||
// with the same text (as expected). | ||
expect(() => getByText('repeated text')).toThrow( | ||
'Found multiple elements with the text: repeated text' | ||
) | ||
|
||
const divNode = getByTestId('div') | ||
|
||
// within() returns queries bound to the provided DOM node, so the following | ||
// assertion passes. Notice how we are not using the getByText() function | ||
// provided by render(), but the one coming from within(). | ||
within(divNode).getByText('repeated text') | ||
|
||
// Here, proof that there's only one match for the specified text. | ||
expect(divNode).toMatchInlineSnapshot(` | ||
<div | ||
data-testid="div" | ||
> | ||
repeated text | ||
</div> | ||
`) | ||
}) |