Skip to content

Add within() test example #88

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
merged 1 commit into from
Aug 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/__tests__/within.js
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>
`)
})