Skip to content

Commit

Permalink
Throw error if an invalid rootElement is passed to assert.dom()
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Mar 4, 2020
1 parent e5cfe37 commit e6763f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/qunit-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ QUnit.assert.dom = function(
target?: string | Element | null,
rootElement?: Element
): DOMAssertions {
if (!isValidRootElement(rootElement)) {
throw new Error(`${rootElement} is not a valid root element`);
}

rootElement = rootElement || this.dom.rootElement || document;
return new DOMAssertions(target || rootElement, rootElement, this);
};

function isValidRootElement(element: any): element is Element {
return (
!element ||
(typeof element === 'object' &&
typeof element.querySelector === 'function' &&
typeof element.querySelectorAll === 'function')
);
}
2 changes: 2 additions & 0 deletions tests/acceptance/qunit-dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ module('Acceptance | qunit-dom', function(hooks) {
* See this: https://github.com/jsdom/jsdom/issues/1928
*/
assert.dom('#with-pseudo-element').hasPseudoElementStyle(':after', { content: '";"' });

assert.throws(() => assert.dom('foo', 'bar'), /bar is not a valid root element/);
});
});

0 comments on commit e6763f9

Please sign in to comment.