Skip to content

Commit

Permalink
fix: Ignore pseudo elements for accessible name by default (testing-l…
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Aug 10, 2020
1 parent 4e62a29 commit 01e0242
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@babel/runtime": "^7.10.3",
"@types/aria-query": "^4.2.0",
"aria-query": "^4.2.2",
"dom-accessibility-api": "^0.4.6",
"dom-accessibility-api": "^0.5.0",
"pretty-format": "^25.5.0"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,24 @@ describe('configuration', () => {
const {getByRole} = render('<div hidden><ul /></div>')
expect(getByRole('list')).not.toBeNull()
})

test('can be configured to consider ::before and ::after for accessible names which logs errors in jsdom', () => {
try {
jest.spyOn(console, 'error').mockImplementation(() => {})
configure({computedStyleSupportsPseudoElements: true})
const {queryByRole} = render('<button>Hello, Dave!</button>')

queryByRole('button', {name: 'Hello, Dave!'})

expect(console.error).toHaveBeenCalledTimes(2)
expect(console.error.mock.calls[0][0]).toMatch(
'Error: Not implemented: window.computedStyle(elt, pseudoElt)',
)
expect(console.error.mock.calls[1][0]).toMatch(
'Error: Not implemented: window.computedStyle(elt, pseudoElt)',
)
} finally {
jest.restoreAllMocks()
}
})
})
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ let config = {
return error
},
_disableExpensiveErrorDiagnostics: false,
computedStyleSupportsPseudoElements: false,
}

export const DEFAULT_IGNORE_TAGS = 'script, style'
Expand Down
5 changes: 4 additions & 1 deletion src/queries/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ function queryAllByRole(
}

return matches(
computeAccessibleName(element),
computeAccessibleName(element, {
computedStyleSupportsPseudoElements: getConfig()
.computedStyleSupportsPseudoElements,
}),
element,
name,
text => text,
Expand Down
6 changes: 5 additions & 1 deletion src/role-helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {elementRoles} from 'aria-query'
import {computeAccessibleName} from 'dom-accessibility-api'
import {prettyDOM} from './pretty-dom'
import {getConfig} from './config'

const elementRoleList = buildElementRoleList(elementRoles)

Expand Down Expand Up @@ -161,7 +162,10 @@ function prettyRoles(dom, {hidden}) {
const delimiterBar = '-'.repeat(50)
const elementsString = elements
.map(el => {
const nameString = `Name "${computeAccessibleName(el)}":\n`
const nameString = `Name "${computeAccessibleName(el, {
computedStyleSupportsPseudoElements: getConfig()
.computedStyleSupportsPseudoElements,
})}":\n`
const domString = prettyDOM(el.cloneNode(false))
return `${nameString}${domString}`
})
Expand Down
5 changes: 4 additions & 1 deletion src/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export function getSuggestedQuery(element, variant = 'get', method) {
if (role !== 'generic' && canSuggest('Role', method, role)) {
return makeSuggestion('Role', role, {
variant,
name: computeAccessibleName(element),
name: computeAccessibleName(element, {
computedStyleSupportsPseudoElements: getConfig()
.computedStyleSupportsPseudoElements,
}),
})
}

Expand Down
1 change: 1 addition & 0 deletions types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface Config {
asyncWrapper(cb: (...args: any[]) => any): Promise<any>
eventWrapper(cb: (...args: any[]) => any): void
asyncUtilTimeout: number
computedStyleSupportsPseudoElements: boolean
defaultHidden: boolean
showOriginalStrackTrace: boolean
throwSuggestions: boolean
Expand Down

0 comments on commit 01e0242

Please sign in to comment.