Skip to content

Commit e4fbcb7

Browse files
committed
added tests
1 parent b05fc69 commit e4fbcb7

File tree

6 files changed

+141
-26
lines changed

6 files changed

+141
-26
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ dist
77
# when working with contributors
88
package-lock.json
99
yarn.lock
10+
11+
.idea

src/__tests__/element-queries.js

Lines changed: 135 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ test('query can return null', () => {
2929
})
3030

3131
describe('get throws a useful error message', () => {
32-
let renderResult
32+
let missingRenderResult;
3333

3434
beforeEach(() => {
35-
renderResult = render(
35+
missingRenderResult = render(
3636
`<div></div><!-- Ignored comment --><style type="text/css">body {} </style><script type="text/javascript></script>`,
3737
)
3838
})
3939

4040
test('ByLabelText', () => {
41-
const {getByLabelText} = renderResult
41+
const {getByLabelText} = missingRenderResult
4242

4343
expect(() => getByLabelText('LucyRicardo'))
4444
.toThrowErrorMatchingInlineSnapshot(`
@@ -52,12 +52,22 @@ describe('get throws a useful error message', () => {
5252
})
5353

5454
test('ByPlaceholderText', () => {
55-
const {getByPlaceholderText} = renderResult
55+
const {getByPlaceholderText} = missingRenderResult
5656

5757
expect(() => getByPlaceholderText('LucyRicardo'))
5858
.toThrowErrorMatchingInlineSnapshot(`
5959
Unable to find an element with the placeholder text of: LucyRicardo
6060
61+
Ignored nodes: comments, script, style
62+
<div>
63+
<div />
64+
</div>
65+
`)
66+
67+
expect(() => getByPlaceholderText(' LucyRicardo '))
68+
.toThrowErrorMatchingInlineSnapshot(`
69+
Unable to find an element with the placeholder text of: LucyRicardo (normalized from ' LucyRicardo ')
70+
6171
Ignored nodes: comments, script, style
6272
<div>
6373
<div />
@@ -100,10 +110,11 @@ describe('get throws a useful error message', () => {
100110
`)
101111
})
102112

103-
test('ByText', () => {
104-
const {getByText} = renderResult
113+
describe('ByText', () => {
114+
test('Missing element', () => {
115+
const {getByText} = missingRenderResult
105116

106-
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
117+
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
107118
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.
108119
109120
Ignored nodes: comments, script, style
@@ -112,8 +123,8 @@ describe('get throws a useful error message', () => {
112123
</div>
113124
`)
114125

115-
expect(() => getByText('LucyRicardo', {selector: 'span'}))
116-
.toThrowErrorMatchingInlineSnapshot(`
126+
expect(() => getByText('LucyRicardo', {selector: 'span'}))
127+
.toThrowErrorMatchingInlineSnapshot(`
117128
Unable to find an element with the text: LucyRicardo, which matches selector 'span'. 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.
118129
119130
Ignored nodes: comments, script, style
@@ -122,7 +133,16 @@ describe('get throws a useful error message', () => {
122133
</div>
123134
`)
124135

125-
expect(() => getByText(/LucyRicardo/)).toThrowErrorMatchingInlineSnapshot(`
136+
expect(() => getByText(' LucyRicardo ', {})).toThrowErrorMatchingInlineSnapshot(`
137+
Unable to find an element with the text: LucyRicardo (normalized from ' 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.
138+
139+
Ignored nodes: comments, script, style
140+
<div>
141+
<div />
142+
</div>
143+
`)
144+
145+
expect(() => getByText(/LucyRicardo/)).toThrowErrorMatchingInlineSnapshot(`
126146
Unable to find an element that its text match the regex: /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.
127147
128148
Ignored nodes: comments, script, style
@@ -131,7 +151,7 @@ describe('get throws a useful error message', () => {
131151
</div>
132152
`)
133153

134-
expect(() => getByText(() => false)).toThrowErrorMatchingInlineSnapshot(`
154+
expect(() => getByText(() => false)).toThrowErrorMatchingInlineSnapshot(`
135155
Unable to find an element that match the custom matcher: [anonymous function]. 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.
136156
137157
Ignored nodes: comments, script, style
@@ -140,23 +160,87 @@ describe('get throws a useful error message', () => {
140160
</div>
141161
`)
142162

143-
function something() {
144-
return false
145-
}
146-
something.customMatcherText = 'Lucy and Ricardo'
163+
function something() {
164+
return false
165+
}
166+
something.customMatcherText = 'Lucy and Ricardo'
147167

148-
expect(() => getByText(something)).toThrowErrorMatchingInlineSnapshot(`
168+
expect(() => getByText(something)).toThrowErrorMatchingInlineSnapshot(`
149169
Unable to find an element that match the custom matcher: Lucy and Ricardo. 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.
150170
151171
Ignored nodes: comments, script, style
152172
<div>
153173
<div />
154174
</div>
155175
`)
156-
})
176+
});
177+
178+
test('Multiple elements', () => {
179+
const {getByText} = render(
180+
`<div><span>LucyRicardo</span><span>LucyRicardo</span></div><!-- Ignored comment --><style type="text/css">body {} </style><script type="text/javascript></script>`,
181+
);
182+
183+
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`Found multiple elements with the text: LucyRicardo
184+
185+
Here are the matching elements:
186+
187+
Ignored nodes: comments, script, style
188+
<span>
189+
LucyRicardo
190+
</span>
191+
192+
Ignored nodes: comments, script, style
193+
<span>
194+
LucyRicardo
195+
</span>
196+
197+
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
198+
199+
Ignored nodes: comments, script, style
200+
<div>
201+
<div>
202+
<span>
203+
LucyRicardo
204+
</span>
205+
<span>
206+
LucyRicardo
207+
</span>
208+
</div>
209+
</div>`);
210+
211+
expect(() => getByText('LucyRicardo', {selector: 'span'}))
212+
.toThrowErrorMatchingInlineSnapshot(`Found multiple elements with the text: LucyRicardo, which matches selector 'span'
213+
214+
Here are the matching elements:
215+
216+
Ignored nodes: comments, script, style
217+
<span>
218+
LucyRicardo
219+
</span>
220+
221+
Ignored nodes: comments, script, style
222+
<span>
223+
LucyRicardo
224+
</span>
225+
226+
(If this is intentional, then use the \`*AllBy*\` variant of the query (like \`queryAllByText\`, \`getAllByText\`, or \`findAllByText\`)).
227+
228+
Ignored nodes: comments, script, style
229+
<div>
230+
<div>
231+
<span>
232+
LucyRicardo
233+
</span>
234+
<span>
235+
LucyRicardo
236+
</span>
237+
</div>
238+
</div>`)
239+
});
240+
});
157241

158242
test('ByTestId', () => {
159-
const {getByTestId} = renderResult
243+
const {getByTestId} = missingRenderResult
160244

161245
expect(() => getByTestId('LucyRicardo'))
162246
.toThrowErrorMatchingInlineSnapshot(`
@@ -170,12 +254,22 @@ describe('get throws a useful error message', () => {
170254
})
171255

172256
test('ByAltText', () => {
173-
const {getByAltText} = renderResult
257+
const {getByAltText} = missingRenderResult
174258

175259
expect(() => getByAltText('LucyRicardo'))
176260
.toThrowErrorMatchingInlineSnapshot(`
177261
Unable to find an element with the alt text: LucyRicardo
178262
263+
Ignored nodes: comments, script, style
264+
<div>
265+
<div />
266+
</div>
267+
`)
268+
269+
expect(() => getByAltText(' LucyRicardo '))
270+
.toThrowErrorMatchingInlineSnapshot(`
271+
Unable to find an element with the alt text: LucyRicardo (normalized from ' LucyRicardo ')
272+
179273
Ignored nodes: comments, script, style
180274
<div>
181275
<div />
@@ -217,11 +311,20 @@ describe('get throws a useful error message', () => {
217311
})
218312

219313
test('ByTitle', () => {
220-
const {getByTitle} = renderResult
314+
const {getByTitle} = missingRenderResult
221315

222316
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
223317
Unable to find an element with the title: LucyRicardo.
224318
319+
Ignored nodes: comments, script, style
320+
<div>
321+
<div />
322+
</div>
323+
`)
324+
325+
expect(() => getByTitle(' LucyRicardo ')).toThrowErrorMatchingInlineSnapshot(`
326+
Unable to find an element with the title: LucyRicardo (normalized from ' LucyRicardo ').
327+
225328
Ignored nodes: comments, script, style
226329
<div>
227330
<div />
@@ -262,12 +365,22 @@ describe('get throws a useful error message', () => {
262365
})
263366

264367
test('ByDisplayValue', () => {
265-
const {getByDisplayValue} = renderResult
368+
const {getByDisplayValue} = missingRenderResult
266369

267370
expect(() => getByDisplayValue('LucyRicardo'))
268371
.toThrowErrorMatchingInlineSnapshot(`
269372
Unable to find an element with the display value: LucyRicardo.
270373
374+
Ignored nodes: comments, script, style
375+
<div>
376+
<div />
377+
</div>
378+
`)
379+
380+
expect(() => getByDisplayValue(' LucyRicardo '))
381+
.toThrowErrorMatchingInlineSnapshot(`
382+
Unable to find an element with the display value: LucyRicardo (normalized from ' LucyRicardo ').
383+
271384
Ignored nodes: comments, script, style
272385
<div>
273386
<div />
@@ -311,7 +424,7 @@ describe('get throws a useful error message', () => {
311424
})
312425

313426
test('ByRole', () => {
314-
const {getByRole} = renderResult
427+
const {getByRole} = missingRenderResult
315428

316429
expect(() => getByRole('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
317430
Unable to find an accessible element with the role "LucyRicardo"

src/queries/display-value.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const getMissingError: GetErrorFunction<[Matcher, MatcherOptions]> = (
5757

5858
function getMatcherHintOrDefault(
5959
matcher: Matcher,
60-
options: MatcherOptions = {},
60+
options: MatcherOptions,
6161
) {
6262
const matcherHint = getMatcherHint(matcher, 'that its display value')
6363

src/queries/placeholder-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const getMissingError: GetErrorFunction<[Matcher, MatcherOptions]> = (
2626

2727
function getMatcherHintOrDefault(
2828
matcher: Matcher,
29-
options: MatcherOptions = {},
29+
options: MatcherOptions,
3030
) {
3131
const matcherHint = getMatcherHint(matcher, 'that its placeholder text')
3232

src/queries/text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const getMissingError: GetErrorFunction<[Matcher, SelectorMatcherOptions]> = (
7373

7474
function getMatcherHintOrDefault(
7575
matcher: Matcher,
76-
options: SelectorMatcherOptions = {},
76+
options: SelectorMatcherOptions,
7777
) {
7878
const matcherHint = getMatcherHint(matcher, 'that its text')
7979

src/queries/title.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const getMissingError: GetErrorFunction<[Matcher, MatcherOptions]> = (
5050

5151
function getMatcherHintOrDefault(
5252
matcher: Matcher,
53-
options: MatcherOptions = {},
53+
options: MatcherOptions,
5454
) {
5555
const matcherHint = getMatcherHint(matcher, 'that its title')
5656

0 commit comments

Comments
 (0)