Skip to content

Commit

Permalink
Merge pull request #88 from testing-library/within
Browse files Browse the repository at this point in the history
Add within() test example
  • Loading branch information
dfcook authored Aug 18, 2019
2 parents 08500e4 + 1eda1ee commit 40ff06e
Showing 1 changed file with 34 additions and 0 deletions.
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>
`)
})

0 comments on commit 40ff06e

Please sign in to comment.