|
| 1 | +import { render, within } from '@testing-library/vue' |
| 2 | + |
| 3 | +test('within() returns an object with all queries bound to the DOM node', () => { |
| 4 | + const { getByTestId, getByText } = render({ |
| 5 | + template: ` |
| 6 | + <div> |
| 7 | + <p>repeated text</p> |
| 8 | + <div data-testid="div">repeated text</div> |
| 9 | + </div> |
| 10 | + ` |
| 11 | + }) |
| 12 | + |
| 13 | + // getByText() provided by render() fails because it finds multiple elements |
| 14 | + // with the same text (as expected). |
| 15 | + expect(() => getByText('repeated text')).toThrow( |
| 16 | + 'Found multiple elements with the text: repeated text' |
| 17 | + ) |
| 18 | + |
| 19 | + const divNode = getByTestId('div') |
| 20 | + |
| 21 | + // within() returns queries bound to the provided DOM node, so the following |
| 22 | + // assertion passes. Notice how we are not using the getByText() function |
| 23 | + // provided by render(), but the one coming from within(). |
| 24 | + within(divNode).getByText('repeated text') |
| 25 | + |
| 26 | + // Here, proof that there's only one match for the specified text. |
| 27 | + expect(divNode).toMatchInlineSnapshot(` |
| 28 | + <div |
| 29 | + data-testid="div" |
| 30 | + > |
| 31 | + repeated text |
| 32 | + </div> |
| 33 | + `) |
| 34 | +}) |
0 commit comments