Skip to content

Commit 74f2739

Browse files
authored
Merge branch 'main' into main
2 parents f6d0604 + 4f965e9 commit 74f2739

File tree

16 files changed

+102
-26
lines changed

16 files changed

+102
-26
lines changed

.all-contributorsrc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,33 @@
15261526
"contributions": [
15271527
"test"
15281528
]
1529+
},
1530+
{
1531+
"login": "robin-drexler",
1532+
"name": "Robin Drexler",
1533+
"avatar_url": "https://avatars.githubusercontent.com/u/474248?v=4",
1534+
"profile": "https://www.robin-drexler.com/",
1535+
"contributions": [
1536+
"code"
1537+
]
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+
]
15291556
}
15301557
],
15311558
"repoHost": "https://github.com"

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Thanks goes to these people ([emoji key][emojis]):
154154
<td align="center"><a href="https://github.com/alecook"><img src="https://avatars3.githubusercontent.com/u/725236?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alex Cook</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=alecook" title="Documentation">📖</a> <a href="#example-alecook" title="Examples">💡</a> <a href="https://github.com/testing-library/dom-testing-library/pulls?q=is%3Apr+reviewed-by%3Aalecook" title="Reviewed Pull Requests">👀</a></td>
155155
<td align="center"><a href="https://github.com/dfcook"><img src="https://avatars3.githubusercontent.com/u/10348212?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Daniel Cook</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=dfcook" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=dfcook" title="Documentation">📖</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=dfcook" title="Tests">⚠️</a></td>
156156
<td align="center"><a href="https://github.com/thchia"><img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4?s=100" width="100px;" alt=""/><br /><sub><b>Thomas Chia</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/issues?q=author%3Athchia" title="Bug reports">🐛</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=thchia" title="Code">💻</a></td>
157-
<td align="center"><a href="http://timdeschryver.dev"><img src="https://avatars.githubusercontent.com/u/28659384?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tim Deschryver</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=tdeschryver" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=tdeschryver" title="Tests">⚠️</a><a href="https://github.com/testing-library/dom-testing-library/pulls?q=is%3Apr+reviewed-by%3Atimdeschryver" title="Reviewed Pull Requests">👀</a></td>
157+
<td align="center"><a href="http://timdeschryver.dev"><img src="https://avatars.githubusercontent.com/u/28659384?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tim Deschryver</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=timdeschryver" title="Code">💻</a> <a href="https://github.com/testing-library/dom-testing-library/commits?author=timdeschryver" title="Tests">⚠️</a> <a href="https://github.com/testing-library/dom-testing-library/pulls?q=is%3Apr+reviewed-by%3Atimdeschryver" title="Reviewed Pull Requests">👀</a></td>
158158
<td align="center"><a href="https://alexkrolick.com"><img src="https://avatars3.githubusercontent.com/u/1571667?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alex Krolick</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=alexkrolick" title="Code">💻</a></td>
159159
<td align="center"><a href="http://www.maddijoyce.com"><img src="https://avatars2.githubusercontent.com/u/2224291?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Maddi Joyce</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=maddijoyce" title="Code">💻</a></td>
160160
</tr>
@@ -332,6 +332,9 @@ Thanks goes to these people ([emoji key][emojis]):
332332
<tr>
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>
335+
<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>
335338
</tr>
336339
</table>
337340

src/__tests__/element-queries.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,18 @@ test('getByText ignores script tags by default', () => {
10801080
expect(getAllByText(/hello/i, {ignore: false})).toHaveLength(3)
10811081
})
10821082

1083+
test('the default value for `ignore` can be configured', () => {
1084+
configure({defaultIgnore: 'style'})
1085+
1086+
const {getAllByText} = render(
1087+
'<script>Hello</script><div>Hello</div><style>.Hello{}</style>',
1088+
)
1089+
const noStyle = getAllByText(/hello/i)
1090+
expect(noStyle).toHaveLength(2)
1091+
expect(noStyle[0].tagName).toBe('SCRIPT')
1092+
expect(noStyle[1].tagName).toBe('DIV')
1093+
})
1094+
10831095
test('get/query input element by current value', () => {
10841096
const {getByDisplayValue, queryByDisplayValue, getByTestId} =
10851097
renderIntoDocument(`

src/__tests__/events.js

Lines changed: 10 additions & 5 deletions
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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ test('has no useful error message in findBy', async () => {
364364
)
365365
})
366366

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

src/__tests__/screen.js

Lines changed: 9 additions & 0 deletions
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/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ let config: InternalConfig = {
2424
eventWrapper: cb => cb(),
2525
// default value for the `hidden` option in `ByRole` queries
2626
defaultHidden: false,
27+
// default value for the `ignore` option in `ByText` queries
28+
defaultIgnore: 'script, style',
2729
// showOriginalStackTrace flag to show the full error stack traces for async errors
2830
showOriginalStackTrace: false,
2931

src/event-map.js

Lines changed: 6 additions & 2 deletions
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/pretty-dom.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as prettyFormat from 'pretty-format'
22
import createDOMElementFilter from './DOMElementFilter'
33
import {getUserCodeFrame} from './get-user-code-frame'
44
import {getDocument} from './helpers'
5-
import {DEFAULT_IGNORE_TAGS} from './shared'
5+
import {getConfig} from './config'
66

77
const inNode = () =>
88
typeof process !== 'undefined' &&
@@ -19,8 +19,8 @@ const COMMENT_NODE = 8
1919
function filterCommentsAndDefaultIgnoreTagsTags(value) {
2020
return (
2121
value.nodeType !== COMMENT_NODE &&
22-
// value.nodeType === ELEMENT_NODE => !value.matches(DEFAULT_IGNORE_TAGS)
23-
(value.nodeType !== ELEMENT_NODE || !value.matches(DEFAULT_IGNORE_TAGS))
22+
(value.nodeType !== ELEMENT_NODE ||
23+
!value.matches(getConfig().defaultIgnore))
2424
)
2525
}
2626

src/queries/role.js

Lines changed: 7 additions & 3 deletions
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/queries/text.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {wrapAllByQueryWithSuggestion} from '../query-helpers'
22
import {checkContainerType} from '../helpers'
3-
import {DEFAULT_IGNORE_TAGS} from '../shared'
43
import {
54
AllByText,
65
GetErrorFunction,
@@ -13,6 +12,7 @@ import {
1312
makeNormalizer,
1413
getNodeText,
1514
buildQueries,
15+
getConfig,
1616
} from './all-utils'
1717

1818
const queryAllByText: AllByText = (
@@ -23,7 +23,7 @@ const queryAllByText: AllByText = (
2323
exact = true,
2424
collapseWhitespace,
2525
trim,
26-
ignore = DEFAULT_IGNORE_TAGS,
26+
ignore = getConfig().defaultIgnore,
2727
normalizer,
2828
} = {},
2929
) => {

src/screen.ts

Lines changed: 7 additions & 5 deletions
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}

src/shared.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/suggestions.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {getNodeText} from './get-node-text'
44
import {getConfig} from './config'
55
import {getImplicitAriaRoles, isInaccessible} from './role-helpers'
66
import {getLabels} from './label-helpers'
7-
import {DEFAULT_IGNORE_TAGS} from './shared'
87

98
const normalize = getDefaultNormalizer()
109

@@ -76,7 +75,7 @@ function canSuggest(currentMethod, requestedMethod, data) {
7675

7776
export function getSuggestedQuery(element, variant = 'get', method) {
7877
// don't create suggestions for script and style elements
79-
if (element.matches(DEFAULT_IGNORE_TAGS)) {
78+
if (element.matches(getConfig().defaultIgnore)) {
8079
return undefined
8180
}
8281

types/config.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export interface Config {
1212
asyncUtilTimeout: number
1313
computedStyleSupportsPseudoElements: boolean
1414
defaultHidden: boolean
15+
/** default value for the `ignore` option in `ByText` queries */
16+
defaultIgnore: string
1517
showOriginalStackTrace: boolean
1618
throwSuggestions: boolean
1719
getElementError: (message: string | null, container: Element) => Error

types/screen.d.ts

Lines changed: 2 additions & 2 deletions
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)