Skip to content
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>
`)
})