(
expectedTagName: string | undefined = undefined,
expectedAttrs: {[key: string]: string | null} = {},
)
| 148 | * @param expectedAttributes optional DOM element properties. |
| 149 | */ |
| 150 | export function matchDomElement( |
| 151 | expectedTagName: string | undefined = undefined, |
| 152 | expectedAttrs: {[key: string]: string | null} = {}, |
| 153 | ): jasmine.AsymmetricMatcher<Element> { |
| 154 | const matcher = function () {}; |
| 155 | let _actual: any = null; |
| 156 | |
| 157 | matcher.asymmetricMatch = function (actual: any) { |
| 158 | _actual = actual; |
| 159 | if (!isDOMElement(actual)) return false; |
| 160 | if (expectedTagName && expectedTagName.toUpperCase() !== actual.tagName.toUpperCase()) { |
| 161 | return false; |
| 162 | } |
| 163 | if (expectedAttrs) { |
| 164 | for (const attrName in expectedAttrs) { |
| 165 | if (expectedAttrs.hasOwnProperty(attrName)) { |
| 166 | const expectedAttrValue = expectedAttrs[attrName]; |
| 167 | const actualAttrValue = actual.getAttribute(attrName); |
| 168 | if (expectedAttrValue !== actualAttrValue) { |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return true; |
| 175 | }; |
| 176 | matcher.jasmineToString = function () { |
| 177 | let actualStr = isDOMElement(_actual) |
| 178 | ? `<${_actual.tagName}${toString(_actual.attributes)}>` |
| 179 | : JSON.stringify(_actual); |
| 180 | let expectedStr = `<${expectedTagName || '*'}${Object.entries(expectedAttrs).map( |
| 181 | ([key, value]) => ` ${key}=${JSON.stringify(value)}`, |
| 182 | )}>`; |
| 183 | return `[${actualStr} != ${expectedStr}]`; |
| 184 | }; |
| 185 | |
| 186 | function toString(attrs: NamedNodeMap) { |
| 187 | let text = ''; |
| 188 | for (let i = 0; i < attrs.length; i++) { |
| 189 | const attr = attrs[i]; |
| 190 | text += ` ${attr.name}=${JSON.stringify(attr.value)}`; |
| 191 | } |
| 192 | return text; |
| 193 | } |
| 194 | |
| 195 | return matcher; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Asymmetric matcher which matches DOM text node. |
no test coverage detected
searching dependent graphs…