Skip to content

Commit

Permalink
Merge pull request #634 from Turbo87/root-element-assertion
Browse files Browse the repository at this point in the history
Throw an error if an invalid `rootElement` is passed to `assert.dom()`
  • Loading branch information
Turbo87 authored Mar 4, 2020
2 parents 402c6d6 + e6763f9 commit 60f3ff6
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 60f3ff6

Please sign in to comment.