Skip to content

Commit d8f09b5

Browse files
eps1lonMatthiasKainer
authored andcommitted
fix(ByRole): Ensure valid query selectors in all transpilation targets (testing-library#1055)
1 parent 673c5e9 commit d8f09b5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/queries/role.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,17 @@ function makeRoleSelector(role, exact, customNormalizer) {
188188
const explicitRoleSelector =
189189
exact && !customNormalizer ? `*[role~="${role}"]` : '*[role]'
190190

191-
const roleRelations = roleElements.get(role)
192-
const implicitRoleSelectors =
193-
roleRelations && new Set(Array.from(roleRelations).map(({name}) => name))
191+
const roleRelations = roleElements.get(role) ?? new Set()
192+
const implicitRoleSelectors = new Set(
193+
Array.from(roleRelations).map(({name}) => name),
194+
)
194195

195-
return [explicitRoleSelector, ...(implicitRoleSelectors ?? [])].join(',')
196+
// Current transpilation config sometimes assumes `...` is always applied to arrays.
197+
// `...` is equivalent to `Array.prototype.concat` for arrays.
198+
// If you replace this code with `[explicitRoleSelector, ...implicitRoleSelectors]`, make sure every transpilation target retains the `...` in favor of `Array.prototype.concat`.
199+
return [explicitRoleSelector]
200+
.concat(Array.from(implicitRoleSelectors))
201+
.join(',')
196202
}
197203

198204
const getMultipleError = (c, role, {name} = {}) => {

0 commit comments

Comments
 (0)