Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error if an invalid rootElement is passed to assert.dom() #634

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
marcoow marked this conversation as resolved.
Show resolved Hide resolved
): 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/);
});
});