Skip to content

Commit fd8d733

Browse files
authored
Merge branch 'main' into perf-byrole
2 parents 77d3291 + fb6f2d5 commit fd8d733

File tree

9 files changed

+88
-12
lines changed

9 files changed

+88
-12
lines changed

.all-contributorsrc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,24 @@
14521452
"contributions": [
14531453
"code"
14541454
]
1455+
},
1456+
{
1457+
"login": "SantoJambit",
1458+
"name": "SantoJambit",
1459+
"avatar_url": "https://avatars.githubusercontent.com/u/53570854?v=4",
1460+
"profile": "https://github.com/Lusito",
1461+
"contributions": [
1462+
"code"
1463+
]
1464+
},
1465+
{
1466+
"login": "mittalyashu",
1467+
"name": "Yashu Mittal",
1468+
"avatar_url": "https://avatars.githubusercontent.com/u/29014463?v=4",
1469+
"profile": "https://github.com/mittalyashu",
1470+
"contributions": [
1471+
"doc"
1472+
]
14551473
}
14561474
],
14571475
"repoHost": "https://github.com"

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
registry=http://registry.npmjs.org/
1+
registry=https://registry.npmjs.org/
22
package-lock=false

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ Thanks goes to these people ([emoji key][emojis]):
322322
</tr>
323323
<tr>
324324
<td align="center"><a href="http://www.jacklaurence.net"><img src="https://avatars.githubusercontent.com/u/12763356?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jack Laurence</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=jacklaurencegaray" title="Code">💻</a></td>
325+
<td align="center"><a href="https://github.com/Lusito"><img src="https://avatars.githubusercontent.com/u/53570854?v=4?s=100" width="100px;" alt=""/><br /><sub><b>SantoJambit</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=SantoJambit" title="Code">💻</a></td>
326+
<td align="center"><a href="https://github.com/mittalyashu"><img src="https://avatars.githubusercontent.com/u/29014463?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yashu Mittal</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=mittalyashu" title="Documentation">📖</a></td>
325327
</tr>
326328
</table>
327329

src/__tests__/queries.find.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test('find asynchronously finds elements', async () => {
3131
<select><option>display value</option></select>
3232
<input placeholder="placeholder" />
3333
<img alt="test alt text" src="/lucy-ricardo.png" />
34+
<div alt="test alt text" />
3435
<span title="test title" />
3536
<div role="dialog"></div>
3637
<div role="meter progressbar"></div>

src/__tests__/text-matchers.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ cases(
3434
query: `Finding Nemo poster`,
3535
queryFn: `queryAllByAltText`,
3636
},
37+
'queryAllByAltText (for amp-img)': {
38+
dom: `
39+
<amp-img
40+
alt="Finding Nemo poster"
41+
src="/finding-nemo.png"
42+
/>`,
43+
query: `Finding Nemo poster`,
44+
queryFn: `queryAllByAltText`,
45+
},
3746
queryAllByPlaceholderText: {
3847
dom: `<input placeholder="Dwayne 'The Rock' Johnson" />`,
3948
query: `Dwayne 'The Rock' Johnson`,
@@ -93,6 +102,16 @@ cases(
93102
query: /^Finding Nemo poster$/,
94103
queryFn: `queryAllByAltText`,
95104
},
105+
'queryAllByAltText (for amp-img)': {
106+
dom: `
107+
<amp-img
108+
alt="
109+
Finding Nemo poster "
110+
src="/finding-nemo.png"
111+
/>`,
112+
query: /^Finding Nemo poster$/,
113+
queryFn: `queryAllByAltText`,
114+
},
96115
queryAllByPlaceholderText: {
97116
dom: `
98117
<input placeholder=" Dwayne 'The Rock' Johnson " />`,
@@ -198,6 +217,15 @@ cases(
198217
query: `Finding Nemo poster`,
199218
queryFn: `queryAllByAltText`,
200219
},
220+
'queryAllByAltText (for amp-img)': {
221+
dom: `
222+
<amp-img
223+
alt="Finding Nemo poster"
224+
src="/finding-nemo.png"
225+
/>`,
226+
query: `Finding Nemo poster`,
227+
queryFn: `queryAllByAltText`,
228+
},
201229
},
202230
)
203231

@@ -251,6 +279,10 @@ cases(
251279
dom: `<img alt="User ${LRM}name" src="username.jpg" />`,
252280
queryFn: 'queryAllByAltText',
253281
},
282+
'queryAllByAltText (for amp-img)': {
283+
dom: `<amp-img alt="User ${LRM}name" src="username.jpg" />`,
284+
queryFn: 'queryAllByAltText',
285+
},
254286
queryAllByTitle: {
255287
dom: `<div title="User ${LRM}name" />`,
256288
queryFn: 'queryAllByTitle',

src/queries/alt-text.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
1+
import {
2+
queryAllByAttribute,
3+
wrapAllByQueryWithSuggestion,
4+
} from '../query-helpers'
25
import {checkContainerType} from '../helpers'
3-
import {AllByBoundAttribute, GetErrorFunction} from '../../types'
4-
import {matches, fuzzyMatches, makeNormalizer, buildQueries} from './all-utils'
6+
import {
7+
AllByBoundAttribute,
8+
GetErrorFunction,
9+
MatcherOptions,
10+
} from '../../types'
11+
import {buildQueries} from './all-utils'
12+
13+
// Valid tags are img, input, area and custom elements
14+
const VALID_TAG_REGEXP = /^(img|input|area|.+-.+)$/i
515

616
const queryAllByAltText: AllByBoundAttribute = (
717
container,
818
alt,
9-
{exact = true, collapseWhitespace, trim, normalizer} = {},
19+
options: MatcherOptions = {},
1020
) => {
1121
checkContainerType(container)
12-
const matcher = exact ? matches : fuzzyMatches
13-
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
14-
return Array.from(
15-
container.querySelectorAll<HTMLElement>('img,input,area'),
16-
).filter(node =>
17-
matcher(node.getAttribute('alt'), node, alt, matchNormalizer),
22+
return queryAllByAttribute('alt', container, alt, options).filter(node =>
23+
VALID_TAG_REGEXP.test(node.tagName),
1824
)
1925
}
2026

types/__tests__/type-tests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ export async function testQueryHelpers() {
117117
await findByAutomationId(element, ['id', 'id'], {})
118118
await findAllByAutomationId(element, ['id', 'id'])
119119
await findByAutomationId(element, ['id', 'id'])
120+
121+
const screenWithCustomQueries = within(document.body, {
122+
...queries,
123+
queryByAutomationId,
124+
getAllByAutomationId,
125+
getByAutomationId,
126+
findAllByAutomationId,
127+
findByAutomationId,
128+
})
129+
130+
screenWithCustomQueries.queryByAutomationId('id')
131+
screenWithCustomQueries.getAllByAutomationId('id')
132+
screenWithCustomQueries.getByAutomationId(['id', 'automationId'])
133+
await screenWithCustomQueries.findAllByAutomationId('id', {}, {timeout: 1000})
134+
await screenWithCustomQueries.findByAutomationId('id', {}, {timeout: 1000})
120135
}
121136

122137
export function testBoundFunctions() {

types/get-queries-for-element.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ export type BoundFunctions<Q> = Q extends typeof queries
153153
findAllByTestId<T extends HTMLElement = HTMLElement>(
154154
...args: Parameters<BoundFunction<queries.FindAllByBoundAttribute<T>>>
155155
): ReturnType<queries.FindAllByBoundAttribute<T>>
156+
} & {
157+
[P in keyof Q]: BoundFunction<Q[P]>
156158
}
157159
: {
158160
[P in keyof Q]: BoundFunction<Q[P]>

types/queries.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export interface ByRoleOptions extends MatcherOptions {
109109
*/
110110
queryFallbacks?: boolean
111111
/**
112-
* Only considers elements with the specified accessible name.
112+
* Only considers elements with the specified accessible name.
113113
*/
114114
name?:
115115
| RegExp

0 commit comments

Comments
 (0)