Skip to content

Commit 79d5c04

Browse files
authored
Merge branch 'main' into fix-get-element-error
2 parents 977d5ad + b7aaf18 commit 79d5c04

File tree

10 files changed

+102
-35
lines changed

10 files changed

+102
-35
lines changed

.all-contributorsrc

+27
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,33 @@
15351535
"contributions": [
15361536
"code"
15371537
]
1538+
},
1539+
{
1540+
"login": "dolevoper",
1541+
"name": "Omer Dolev",
1542+
"avatar_url": "https://avatars.githubusercontent.com/u/53278705?v=4",
1543+
"profile": "https://github.com/dolevoper",
1544+
"contributions": [
1545+
"code"
1546+
]
1547+
},
1548+
{
1549+
"login": "Lirlev48",
1550+
"name": "Lirlev48",
1551+
"avatar_url": "https://avatars.githubusercontent.com/u/58209233?v=4",
1552+
"profile": "https://github.com/Lirlev48",
1553+
"contributions": [
1554+
"code"
1555+
]
1556+
},
1557+
{
1558+
"login": "kalmi",
1559+
"name": "Tarnay Kálmán",
1560+
"avatar_url": "https://avatars.githubusercontent.com/u/54426?v=4",
1561+
"profile": "https://github.com/kalmi",
1562+
"contributions": [
1563+
"code"
1564+
]
15381565
}
15391566
],
15401567
"repoHost": "https://github.com"

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ Thanks goes to these people ([emoji key][emojis]):
333333
<td align="center"><a href="https://github.com/Dennis273"><img src="https://avatars.githubusercontent.com/u/19815164?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dennis273</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Dennis273" title="Code">💻</a></td>
334334
<td align="center"><a href="https://www.jacksonhardaker.dev"><img src="https://avatars.githubusercontent.com/u/7596320?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jackson Hardaker</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=jacksonhardaker" title="Tests">⚠️</a></td>
335335
<td align="center"><a href="https://www.robin-drexler.com/"><img src="https://avatars.githubusercontent.com/u/474248?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Robin Drexler</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=robin-drexler" title="Code">💻</a></td>
336+
<td align="center"><a href="https://github.com/dolevoper"><img src="https://avatars.githubusercontent.com/u/53278705?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Omer Dolev</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=dolevoper" title="Code">💻</a></td>
337+
<td align="center"><a href="https://github.com/Lirlev48"><img src="https://avatars.githubusercontent.com/u/58209233?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lirlev48</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=Lirlev48" title="Code">💻</a></td>
338+
<td align="center"><a href="https://github.com/kalmi"><img src="https://avatars.githubusercontent.com/u/54426?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tarnay Kálmán</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=kalmi" title="Code">💻</a></td>
336339
</tr>
337340
</table>
338341

src/__tests__/events.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ const eventTypes = [
7171
events: ['scroll'],
7272
elementType: 'div',
7373
},
74+
{
75+
type: '',
76+
events: ['load', 'error'],
77+
elementType: 'img',
78+
},
79+
{
80+
type: '',
81+
events: ['load', 'error'],
82+
elementType: 'script',
83+
},
7484
{
7585
type: 'Wheel',
7686
events: ['wheel'],
@@ -105,11 +115,6 @@ const eventTypes = [
105115
],
106116
elementType: 'video',
107117
},
108-
{
109-
type: 'Image',
110-
events: ['load', 'error'],
111-
elementType: 'img',
112-
},
113118
{
114119
type: 'Animation',
115120
events: ['animationStart', 'animationEnd', 'animationIteration'],

src/__tests__/role.js

+8
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ test('has no useful error message in findBy', async () => {
363363
)
364364
})
365365

366+
test('findBy error message for missing elements contains a name hint', async () => {
367+
const {findByRole} = render(`<button>Click me</button>`)
368+
369+
await expect(findByRole('button', {name: 'Submit'})).rejects.toThrow(
370+
'Unable to find role="button" and name "Submit"',
371+
)
372+
})
373+
366374
test('explicit role is most specific', () => {
367375
const {getByRole} = renderIntoDocument(
368376
`<button role="tab" aria-label="my-tab" />`,

src/__tests__/screen.js

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ test('logs Playground URL that are passed as element', () => {
6464
`)
6565
})
6666

67+
test('returns Playground URL that are passed as element', () => {
68+
const playGroundUrl = screen.logTestingPlaygroundURL(
69+
render(`<h1>Sign <em>up</em></h1>`).container,
70+
)
71+
expect(playGroundUrl).toMatchInlineSnapshot(
72+
'https://testing-playground.com/#markup=DwCwjAfAyglg5gOwATAKYFsIFcAOwD0GEB4EQA',
73+
)
74+
})
75+
6776
test('exposes debug method', () => {
6877
renderIntoDocument(
6978
`<button>test</button><span>multi-test</span><div>multi-test</div>`,

src/event-map.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,13 @@ export const eventMap = {
274274
EventType: 'Event',
275275
defaultInit: {bubbles: false, cancelable: false},
276276
},
277-
// Image Events
277+
// Events
278278
load: {
279-
EventType: 'UIEvent',
279+
// TODO: load events can be UIEvent or Event depending on what generated them
280+
// This is were this abstraction breaks down.
281+
// But the common targets are <img />, <script /> and window.
282+
// Neither of these targets receive a UIEvent
283+
EventType: 'Event',
280284
defaultInit: {bubbles: false, cancelable: false},
281285
},
282286
error: {

src/queries/role.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function makeRoleSelector(role, exact, customNormalizer) {
220220
.join(',')
221221
}
222222

223-
const getMultipleError = (c, role, {name} = {}) => {
223+
const getNameHint = name => {
224224
let nameHint = ''
225225
if (name === undefined) {
226226
nameHint = ''
@@ -230,7 +230,11 @@ const getMultipleError = (c, role, {name} = {}) => {
230230
nameHint = ` and name \`${name}\``
231231
}
232232

233-
return `Found multiple elements with the role "${role}"${nameHint}`
233+
return nameHint
234+
}
235+
236+
const getMultipleError = (c, role, {name} = {}) => {
237+
return `Found multiple elements with the role "${role}"${getNameHint(name)}`
234238
}
235239

236240
const getMissingError = (
@@ -239,7 +243,7 @@ const getMissingError = (
239243
{hidden = getConfig().defaultHidden, name, description} = {},
240244
) => {
241245
if (getConfig()._disableExpensiveErrorDiagnostics) {
242-
return `Unable to find role="${role}"`
246+
return `Unable to find role="${role}"${getNameHint(name)}`
243247
}
244248

245249
let roles = ''

src/role-helpers.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,32 @@ function buildElementRoleList(elementRolesMap) {
105105
}
106106

107107
function match(element) {
108+
let {attributes = []} = element
109+
110+
// https://github.com/testing-library/dom-testing-library/issues/814
111+
const typeTextIndex = attributes.findIndex(
112+
attribute =>
113+
attribute.value &&
114+
attribute.name === 'type' &&
115+
attribute.value === 'text',
116+
)
117+
118+
if (typeTextIndex >= 0) {
119+
// not using splice to not mutate the attributes array
120+
attributes = [
121+
...attributes.slice(0, typeTextIndex),
122+
...attributes.slice(typeTextIndex + 1),
123+
]
124+
}
125+
126+
const selector = makeElementSelector({...element, attributes})
127+
108128
return node => {
109-
let {attributes = []} = element
110-
// https://github.com/testing-library/dom-testing-library/issues/814
111-
const typeTextIndex = attributes.findIndex(
112-
attribute =>
113-
attribute.value &&
114-
attribute.name === 'type' &&
115-
attribute.value === 'text',
116-
)
117-
if (typeTextIndex >= 0) {
118-
// not using splice to not mutate the attributes array
119-
attributes = [
120-
...attributes.slice(0, typeTextIndex),
121-
...attributes.slice(typeTextIndex + 1),
122-
]
123-
if (node.type !== 'text') {
124-
return false
125-
}
129+
if (typeTextIndex >= 0 && node.type !== 'text') {
130+
return false
126131
}
127132

128-
return node.matches(makeElementSelector({...element, attributes}))
133+
return node.matches(selector)
129134
}
130135
}
131136

src/screen.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {compressToEncodedURIComponent} from 'lz-string'
1+
// WARNING: `lz-string` only has a default export but statically we assume named exports are allowd
2+
// TODO: Statically verify we don't rely on NodeJS implicit named imports.
3+
import lzString from 'lz-string'
24
import type {OptionsReceived} from 'pretty-format'
35
import {getQueriesForElement} from './get-queries-for-element'
46
import {getDocument} from './helpers'
@@ -12,7 +14,7 @@ function unindent(string: string) {
1214
}
1315

1416
function encode(value: string) {
15-
return compressToEncodedURIComponent(unindent(value))
17+
return lzString.compressToEncodedURIComponent(unindent(value))
1618
}
1719

1820
function getPlaygroundUrl(markup: string) {
@@ -39,9 +41,9 @@ const logTestingPlaygroundURL = (element = getDocument().body) => {
3941
console.log(`The provided element doesn't have any children.`)
4042
return
4143
}
42-
console.log(
43-
`Open this URL in your browser\n\n${getPlaygroundUrl(element.innerHTML)}`,
44-
)
44+
const playgroundUrl = getPlaygroundUrl(element.innerHTML)
45+
console.log(`Open this URL in your browser\n\n${playgroundUrl}`)
46+
return playgroundUrl
4547
}
4648

4749
const initialValue = {debug, logTestingPlaygroundURL}

types/screen.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export type Screen<Q extends Queries = typeof queries> = BoundFunctions<Q> & {
1313
options?: OptionsReceived,
1414
) => void
1515
/**
16-
* Convenience function for `Testing Playground` which logs URL that
16+
* Convenience function for `Testing Playground` which logs and returns the URL that
1717
* can be opened in a browser
1818
*/
19-
logTestingPlaygroundURL: (element?: Element | HTMLDocument) => void
19+
logTestingPlaygroundURL: (element?: Element | HTMLDocument) => string
2020
}
2121

2222
export const screen: Screen

0 commit comments

Comments
 (0)