Skip to content

Commit 2293b4c

Browse files
sheerungnapse
authored andcommitted
fix: flawed logic in checking if elements are indeed html elements (#67)
* Fix checkHtmlElement helper * Remove checkHtmlText
1 parent 84a68c1 commit 2293b4c

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

src/__tests__/utils.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {deprecate} from '../utils'
1+
import {deprecate, checkHtmlElement, HtmlElementTypeError} from '../utils'
2+
import document from './helpers/document'
23

34
test('deprecate', () => {
45
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
@@ -14,3 +15,46 @@ test('deprecate', () => {
1415

1516
spy.mockRestore()
1617
})
18+
19+
describe('checkHtmlElement', () => {
20+
it('does not throw an error for correct html element', () => {
21+
expect(() => {
22+
const element = document.createElement('p')
23+
checkHtmlElement(element, () => {}, {})
24+
}).not.toThrow()
25+
})
26+
27+
it('does not throw an error for correct svg element', () => {
28+
expect(() => {
29+
const element = document.createElementNS(
30+
'http://www.w3.org/2000/svg',
31+
'rect',
32+
)
33+
checkHtmlElement(element, () => {}, {})
34+
}).not.toThrow()
35+
})
36+
37+
it('does not throw for body', () => {
38+
expect(() => {
39+
checkHtmlElement(document.body, () => {}, {})
40+
}).not.toThrow()
41+
})
42+
43+
it('throws for undefined', () => {
44+
expect(() => {
45+
checkHtmlElement(undefined, () => {}, {})
46+
}).toThrow(HtmlElementTypeError)
47+
})
48+
49+
it('throws for document', () => {
50+
expect(() => {
51+
checkHtmlElement(document, () => {}, {})
52+
}).toThrow(HtmlElementTypeError)
53+
})
54+
55+
it('throws for function', () => {
56+
expect(() => {
57+
checkHtmlElement(() => {}, () => {}, {})
58+
}).toThrow(HtmlElementTypeError)
59+
})
60+
})

src/to-contain-html.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import {matcherHint, printReceived} from 'jest-matcher-utils'
22
import {checkHtmlElement} from './utils'
33

4-
function checkHtmlText(element, htmlText, ...args) {
5-
const DOMParser = element.ownerDocument.defaultView.DOMParser
6-
const htmlElement =
7-
typeof htmlText === 'string'
8-
? new DOMParser().parseFromString(htmlText, 'text/html').body.firstChild
9-
: null
10-
checkHtmlElement(htmlElement, ...args)
11-
}
12-
134
export function toContainHTML(container, htmlText) {
145
checkHtmlElement(container, toContainHTML, this)
15-
checkHtmlText(container, htmlText, toContainHTML, this)
166

177
return {
188
pass: container.outerHTML.includes(htmlText),

src/utils.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class HtmlElementTypeError extends Error {
1717
if (Error.captureStackTrace) {
1818
Error.captureStackTrace(this, matcherFn)
1919
}
20+
let withType = ''
21+
try {
22+
withType = printWithType('Received', received, printReceived)
23+
} catch (e) {
24+
// Can throw for Document:
25+
// https://github.com/jsdom/jsdom/issues/2304
26+
}
2027
this.message = [
2128
matcherHint(
2229
`${context.isNot ? '.not' : ''}.${matcherFn.name}`,
@@ -27,16 +34,28 @@ class HtmlElementTypeError extends Error {
2734
`${receivedColor(
2835
'received',
2936
)} value must be an HTMLElement or an SVGElement.`,
30-
printWithType('Received', received, printReceived),
37+
withType,
3138
].join('\n')
3239
}
3340
}
3441

42+
function checkHasWindow(htmlElement, ...args) {
43+
if (
44+
!htmlElement ||
45+
!htmlElement.ownerDocument ||
46+
!htmlElement.ownerDocument.defaultView
47+
) {
48+
throw new HtmlElementTypeError(htmlElement, ...args)
49+
}
50+
}
51+
3552
function checkHtmlElement(htmlElement, ...args) {
53+
checkHasWindow(htmlElement, ...args)
54+
const window = htmlElement.ownerDocument.defaultView
55+
3656
if (
37-
!htmlElement.ownerDocument &&
38-
!(htmlElement instanceof htmlElement.ownerDocument.HTMLElement) &&
39-
!(htmlElement instanceof htmlElement.ownerDocument.SVGElement)
57+
!(htmlElement instanceof window.HTMLElement) &&
58+
!(htmlElement instanceof window.SVGElement)
4059
) {
4160
throw new HtmlElementTypeError(htmlElement, ...args)
4261
}
@@ -115,6 +134,7 @@ function normalize(text) {
115134
}
116135

117136
export {
137+
HtmlElementTypeError,
118138
checkHtmlElement,
119139
checkValidCSS,
120140
deprecate,

0 commit comments

Comments
 (0)