Skip to content

Commit 393377a

Browse files
author
Kent C. Dodds
committed
chore(tests): improve tests a bit
1 parent 30ac68a commit 393377a

8 files changed

+231
-350
lines changed

src/__tests__/__snapshots__/element-queries.js.snap

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/__tests__/__snapshots__/example.js.snap

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/__tests__/__snapshots__/pretty-dom.js.snap

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/__tests__/__snapshots__/wait-for-dom-change.js.snap

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/__tests__/element-queries.js

Lines changed: 110 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,72 @@ test('get throws a useful error message', () => {
3434
getByValue,
3535
getByRole,
3636
} = render('<div />')
37-
expect(() => getByLabelText('LucyRicardo')).toThrowErrorMatchingSnapshot()
38-
expect(() => getBySelectText('LucyRicardo')).toThrowErrorMatchingSnapshot()
39-
expect(() =>
40-
getByPlaceholderText('LucyRicardo'),
41-
).toThrowErrorMatchingSnapshot()
42-
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingSnapshot()
43-
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingSnapshot()
44-
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingSnapshot()
45-
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingSnapshot()
46-
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingSnapshot()
47-
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingSnapshot()
37+
expect(() => getByLabelText('LucyRicardo'))
38+
.toThrowErrorMatchingInlineSnapshot(`
39+
"Unable to find a label with the text of: LucyRicardo
40+
41+
<div>
42+
<div />
43+
</div>"
44+
`)
45+
expect(() => getBySelectText('LucyRicardo'))
46+
.toThrowErrorMatchingInlineSnapshot(`
47+
"Unable to find a <select> element with the selected option's text: LucyRicardo
48+
49+
<div>
50+
<div />
51+
</div>"
52+
`)
53+
expect(() => getByPlaceholderText('LucyRicardo'))
54+
.toThrowErrorMatchingInlineSnapshot(`
55+
"Unable to find an element with the placeholder text of: LucyRicardo
56+
57+
<div>
58+
<div />
59+
</div>"
60+
`)
61+
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
62+
"Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
63+
64+
<div>
65+
<div />
66+
</div>"
67+
`)
68+
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
69+
"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]
70+
71+
<div>
72+
<div />
73+
</div>"
74+
`)
75+
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
76+
"Unable to find an element with the alt text: LucyRicardo
77+
78+
<div>
79+
<div />
80+
</div>"
81+
`)
82+
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
83+
"Unable to find an element with the title: LucyRicardo.
84+
85+
<div>
86+
<div />
87+
</div>"
88+
`)
89+
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
90+
"Unable to find an element with the value: LucyRicardo.
91+
92+
<div>
93+
<div />
94+
</div>"
95+
`)
96+
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
97+
"Unable to find an element by role=LucyRicardo
98+
99+
<div>
100+
<div />
101+
</div>"
102+
`)
48103
})
49104

50105
test('can get elements by matching their text content', () => {
@@ -160,13 +215,27 @@ test('get can get form controls by placeholder', () => {
160215
test('label with no form control', () => {
161216
const {getByLabelText, queryByLabelText} = render(`<label>All alone</label>`)
162217
expect(queryByLabelText(/alone/)).toBeNull()
163-
expect(() => getByLabelText(/alone/)).toThrowErrorMatchingSnapshot()
218+
expect(() => getByLabelText(/alone/)).toThrowErrorMatchingInlineSnapshot(`
219+
"Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
220+
221+
<div>
222+
<label>
223+
All alone
224+
</label>
225+
</div>"
226+
`)
164227
})
165228

166229
test('totally empty label', () => {
167230
const {getByLabelText, queryByLabelText} = render(`<label />`)
168231
expect(queryByLabelText('')).toBeNull()
169-
expect(() => getByLabelText('')).toThrowErrorMatchingSnapshot()
232+
expect(() => getByLabelText('')).toThrowErrorMatchingInlineSnapshot(`
233+
"Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
234+
235+
<div>
236+
<label />
237+
</div>"
238+
`)
170239
})
171240

172241
test('getByLabelText with aria-label', () => {
@@ -568,16 +637,36 @@ test('get throws a useful error message without DOM in Cypress', () => {
568637
getByTitle,
569638
getByValue,
570639
} = render('<div />')
571-
expect(() => getByLabelText('LucyRicardo')).toThrowErrorMatchingSnapshot()
572-
expect(() => getBySelectText('LucyRicardo')).toThrowErrorMatchingSnapshot()
640+
expect(() =>
641+
getByLabelText('LucyRicardo'),
642+
).toThrowErrorMatchingInlineSnapshot(
643+
`"Unable to find a label with the text of: LucyRicardo"`,
644+
)
645+
expect(() =>
646+
getBySelectText('LucyRicardo'),
647+
).toThrowErrorMatchingInlineSnapshot(
648+
`"Unable to find a <select> element with the selected option's text: LucyRicardo"`,
649+
)
573650
expect(() =>
574651
getByPlaceholderText('LucyRicardo'),
575-
).toThrowErrorMatchingSnapshot()
576-
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingSnapshot()
577-
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingSnapshot()
578-
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingSnapshot()
579-
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingSnapshot()
580-
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingSnapshot()
652+
).toThrowErrorMatchingInlineSnapshot(
653+
`"Unable to find an element with the placeholder text of: LucyRicardo"`,
654+
)
655+
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
656+
`"Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible."`,
657+
)
658+
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
659+
`"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]"`,
660+
)
661+
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
662+
`"Unable to find an element with the alt text: LucyRicardo"`,
663+
)
664+
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
665+
`"Unable to find an element with the title: LucyRicardo."`,
666+
)
667+
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
668+
`"Unable to find an element with the value: LucyRicardo."`,
669+
)
581670
})
582671

583672
test('getByText ignores script tags by default', () => {

0 commit comments

Comments
 (0)