Skip to content

Commit

Permalink
fix: flawed logic in checking if elements are indeed html elements (#67)
Browse files Browse the repository at this point in the history
* Fix checkHtmlElement helper

* Remove checkHtmlText
  • Loading branch information
sheerun authored and gnapse committed Nov 6, 2018
1 parent 84a68c1 commit 2293b4c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
46 changes: 45 additions & 1 deletion src/__tests__/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {deprecate} from '../utils'
import {deprecate, checkHtmlElement, HtmlElementTypeError} from '../utils'
import document from './helpers/document'

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

spy.mockRestore()
})

describe('checkHtmlElement', () => {
it('does not throw an error for correct html element', () => {
expect(() => {
const element = document.createElement('p')
checkHtmlElement(element, () => {}, {})
}).not.toThrow()
})

it('does not throw an error for correct svg element', () => {
expect(() => {
const element = document.createElementNS(
'http://www.w3.org/2000/svg',
'rect',
)
checkHtmlElement(element, () => {}, {})
}).not.toThrow()
})

it('does not throw for body', () => {
expect(() => {
checkHtmlElement(document.body, () => {}, {})
}).not.toThrow()
})

it('throws for undefined', () => {
expect(() => {
checkHtmlElement(undefined, () => {}, {})
}).toThrow(HtmlElementTypeError)
})

it('throws for document', () => {
expect(() => {
checkHtmlElement(document, () => {}, {})
}).toThrow(HtmlElementTypeError)
})

it('throws for function', () => {
expect(() => {
checkHtmlElement(() => {}, () => {}, {})
}).toThrow(HtmlElementTypeError)
})
})
10 changes: 0 additions & 10 deletions src/to-contain-html.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import {matcherHint, printReceived} from 'jest-matcher-utils'
import {checkHtmlElement} from './utils'

function checkHtmlText(element, htmlText, ...args) {
const DOMParser = element.ownerDocument.defaultView.DOMParser
const htmlElement =
typeof htmlText === 'string'
? new DOMParser().parseFromString(htmlText, 'text/html').body.firstChild
: null
checkHtmlElement(htmlElement, ...args)
}

export function toContainHTML(container, htmlText) {
checkHtmlElement(container, toContainHTML, this)
checkHtmlText(container, htmlText, toContainHTML, this)

return {
pass: container.outerHTML.includes(htmlText),
Expand Down
28 changes: 24 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class HtmlElementTypeError extends Error {
if (Error.captureStackTrace) {
Error.captureStackTrace(this, matcherFn)
}
let withType = ''
try {
withType = printWithType('Received', received, printReceived)
} catch (e) {
// Can throw for Document:
// https://github.com/jsdom/jsdom/issues/2304
}
this.message = [
matcherHint(
`${context.isNot ? '.not' : ''}.${matcherFn.name}`,
Expand All @@ -27,16 +34,28 @@ class HtmlElementTypeError extends Error {
`${receivedColor(
'received',
)} value must be an HTMLElement or an SVGElement.`,
printWithType('Received', received, printReceived),
withType,
].join('\n')
}
}

function checkHasWindow(htmlElement, ...args) {
if (
!htmlElement ||
!htmlElement.ownerDocument ||
!htmlElement.ownerDocument.defaultView
) {
throw new HtmlElementTypeError(htmlElement, ...args)
}
}

function checkHtmlElement(htmlElement, ...args) {
checkHasWindow(htmlElement, ...args)
const window = htmlElement.ownerDocument.defaultView

if (
!htmlElement.ownerDocument &&
!(htmlElement instanceof htmlElement.ownerDocument.HTMLElement) &&
!(htmlElement instanceof htmlElement.ownerDocument.SVGElement)
!(htmlElement instanceof window.HTMLElement) &&
!(htmlElement instanceof window.SVGElement)
) {
throw new HtmlElementTypeError(htmlElement, ...args)
}
Expand Down Expand Up @@ -115,6 +134,7 @@ function normalize(text) {
}

export {
HtmlElementTypeError,
checkHtmlElement,
checkValidCSS,
deprecate,
Expand Down

0 comments on commit 2293b4c

Please sign in to comment.