forked from emberjs/ember-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-root-element.ts
48 lines (40 loc) · 1.37 KB
/
get-root-element.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { getContext, isTestContext } from '../setup-context';
import { isDocument, isElement } from './-target';
/**
Get the root element of the application under test (usually `#ember-testing`)
@public
@returns {Element} the root element
*/
export default function getRootElement(): Element | Document {
let context = getContext();
if (!context || !isTestContext(context) || !context.owner) {
throw new Error(
'Must setup rendering context before attempting to interact with elements.'
);
}
let owner = context.owner;
let rootElement;
// When the host app uses `setApplication` (instead of `setResolver`) the owner has
// a `rootElement` set on it with the element or id to be used
if (owner && owner._emberTestHelpersMockOwner === undefined) {
rootElement = owner.rootElement;
} else {
rootElement = '#ember-testing';
}
if (rootElement instanceof Window) {
rootElement = rootElement.document;
}
if (isElement(rootElement) || isDocument(rootElement)) {
return rootElement;
} else if (typeof rootElement === 'string') {
let _rootElement = document.querySelector(rootElement);
if (_rootElement) {
return _rootElement;
}
throw new Error(`Application.rootElement (${rootElement}) not found`);
} else {
throw new Error(
'Application.rootElement must be an element or a selector string'
);
}
}