|
| 1 | +import {elementRoles} from 'aria-query' |
| 2 | +import {checkHtmlElement, getMessage} from './utils' |
| 3 | + |
| 4 | +const elementRoleList = buildElementRoleList(elementRoles) |
| 5 | + |
| 6 | +export function toHaveRole(htmlElement, expectedRole) { |
| 7 | + checkHtmlElement(htmlElement, toHaveRole, this) |
| 8 | + |
| 9 | + const actualRoles = getExplicitOrImplicitRoles(htmlElement) |
| 10 | + const pass = actualRoles.some(el => el === expectedRole) |
| 11 | + |
| 12 | + return { |
| 13 | + pass, |
| 14 | + |
| 15 | + message: () => { |
| 16 | + const to = this.isNot ? 'not to' : 'to' |
| 17 | + return getMessage( |
| 18 | + this, |
| 19 | + this.utils.matcherHint( |
| 20 | + `${this.isNot ? '.not' : ''}.${toHaveRole.name}`, |
| 21 | + 'element', |
| 22 | + '', |
| 23 | + ), |
| 24 | + `Expected element ${to} have role`, |
| 25 | + expectedRole, |
| 26 | + 'Received', |
| 27 | + actualRoles.join(', '), |
| 28 | + ) |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function getExplicitOrImplicitRoles(htmlElement) { |
| 34 | + const hasExplicitRole = htmlElement.hasAttribute('role') |
| 35 | + |
| 36 | + if (hasExplicitRole) { |
| 37 | + const roleValue = htmlElement.getAttribute('role') |
| 38 | + |
| 39 | + // Handle fallback roles, such as role="switch button" |
| 40 | + // testing-library gates this behind the `queryFallbacks` flag; it is |
| 41 | + // unclear why, but it makes sense to support this pattern out of the box |
| 42 | + // https://testing-library.com/docs/queries/byrole/#queryfallbacks |
| 43 | + return roleValue.split(' ').filter(Boolean) |
| 44 | + } |
| 45 | + |
| 46 | + const implicitRoles = getImplicitAriaRoles(htmlElement) |
| 47 | + |
| 48 | + return implicitRoles |
| 49 | +} |
| 50 | + |
| 51 | +function getImplicitAriaRoles(currentNode) { |
| 52 | + for (const {match, roles} of elementRoleList) { |
| 53 | + if (match(currentNode)) { |
| 54 | + return [...roles] |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /* istanbul ignore next */ |
| 59 | + return [] // this does not get reached in practice, since elements have at least a 'generic' role |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Transform the roles map (with required attributes and constraints) to a list |
| 64 | + * of roles. Each item in the list has functions to match an element against it. |
| 65 | + * |
| 66 | + * Essentially copied over from [dom-testing-library's |
| 67 | + * helpers](https://github.com/testing-library/dom-testing-library/blob/bd04cf95a1ed85a2238f7dfc1a77d5d16b4f59dc/src/role-helpers.js#L80) |
| 68 | + * |
| 69 | + * TODO: If we are truly just copying over stuff, would it make sense to move |
| 70 | + * this to a separate package? |
| 71 | + * |
| 72 | + * TODO: This technique relies on CSS selectors; are those consistently |
| 73 | + * available in all jest-dom environments? Why do other matchers in this package |
| 74 | + * not use them like this? |
| 75 | + */ |
| 76 | +function buildElementRoleList(elementRolesMap) { |
| 77 | + function makeElementSelector({name, attributes}) { |
| 78 | + return `${name}${attributes |
| 79 | + .map(({name: attributeName, value, constraints = []}) => { |
| 80 | + const shouldNotExist = constraints.indexOf('undefined') !== -1 |
| 81 | + if (shouldNotExist) { |
| 82 | + return `:not([${attributeName}])` |
| 83 | + } else if (value) { |
| 84 | + return `[${attributeName}="${value}"]` |
| 85 | + } else { |
| 86 | + return `[${attributeName}]` |
| 87 | + } |
| 88 | + }) |
| 89 | + .join('')}` |
| 90 | + } |
| 91 | + |
| 92 | + function getSelectorSpecificity({attributes = []}) { |
| 93 | + return attributes.length |
| 94 | + } |
| 95 | + |
| 96 | + function bySelectorSpecificity( |
| 97 | + {specificity: leftSpecificity}, |
| 98 | + {specificity: rightSpecificity}, |
| 99 | + ) { |
| 100 | + return rightSpecificity - leftSpecificity |
| 101 | + } |
| 102 | + |
| 103 | + function match(element) { |
| 104 | + let {attributes = []} = element |
| 105 | + |
| 106 | + // https://github.com/testing-library/dom-testing-library/issues/814 |
| 107 | + const typeTextIndex = attributes.findIndex( |
| 108 | + attribute => |
| 109 | + attribute.value && |
| 110 | + attribute.name === 'type' && |
| 111 | + attribute.value === 'text', |
| 112 | + ) |
| 113 | + |
| 114 | + if (typeTextIndex >= 0) { |
| 115 | + // not using splice to not mutate the attributes array |
| 116 | + attributes = [ |
| 117 | + ...attributes.slice(0, typeTextIndex), |
| 118 | + ...attributes.slice(typeTextIndex + 1), |
| 119 | + ] |
| 120 | + } |
| 121 | + |
| 122 | + const selector = makeElementSelector({...element, attributes}) |
| 123 | + |
| 124 | + return node => { |
| 125 | + if (typeTextIndex >= 0 && node.type !== 'text') { |
| 126 | + return false |
| 127 | + } |
| 128 | + |
| 129 | + return node.matches(selector) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + let result = [] |
| 134 | + |
| 135 | + for (const [element, roles] of elementRolesMap.entries()) { |
| 136 | + result = [ |
| 137 | + ...result, |
| 138 | + { |
| 139 | + match: match(element), |
| 140 | + roles: Array.from(roles), |
| 141 | + specificity: getSelectorSpecificity(element), |
| 142 | + }, |
| 143 | + ] |
| 144 | + } |
| 145 | + |
| 146 | + return result.sort(bySelectorSpecificity) |
| 147 | +} |
0 commit comments