-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
setup-application-context.ts
204 lines (167 loc) · 5.97 KB
/
setup-application-context.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { get } from '@ember/object';
import { Promise } from './-utils';
import { BaseContext, getContext, isTestContext, TestContext } from './setup-context';
import global from './global';
import hasEmberVersion from './has-ember-version';
import settled from './settled';
import getTestMetadata, { ITestMetadata } from './test-metadata';
import { runHooks } from './-internal/helper-hooks';
export interface ApplicationTestContext extends TestContext {
element?: Element | null;
}
const CAN_USE_ROUTER_EVENTS = hasEmberVersion(3, 6);
let routerTransitionsPending: boolean | null = null;
const ROUTER = new WeakMap();
const HAS_SETUP_ROUTER = new WeakMap();
// eslint-disable-next-line require-jsdoc
export function isApplicationTestContext(context: BaseContext): context is ApplicationTestContext {
return isTestContext(context);
}
/**
Determines if we have any pending router transtions (used to determine `settled` state)
@public
@returns {(boolean|null)} if there are pending transitions
*/
export function hasPendingTransitions(): boolean | null {
if (CAN_USE_ROUTER_EVENTS) {
return routerTransitionsPending;
}
let context = getContext();
// there is no current context, we cannot check
if (context === undefined) {
return null;
}
let router = ROUTER.get(context);
if (router === undefined) {
// if there is no router (e.g. no `visit` calls made yet), we cannot
// check for pending transitions but this is explicitly not an error
// condition
return null;
}
let routerMicrolib = router._routerMicrolib || router.router;
if (routerMicrolib === undefined) {
return null;
}
return !!routerMicrolib.activeTransition;
}
/**
Setup the current router instance with settledness tracking. Generally speaking this
is done automatically (during a `visit('/some-url')` invocation), but under some
circumstances (e.g. a non-application test where you manually call `this.owner.setupRouter()`)
you may want to call it yourself.
@public
*/
export function setupRouterSettlednessTracking() {
const context = getContext();
if (context === undefined) {
throw new Error('Cannot setupRouterSettlednessTracking outside of a test context');
}
// avoid setting up many times for the same context
if (HAS_SETUP_ROUTER.get(context)) {
return;
}
HAS_SETUP_ROUTER.set(context, true);
let { owner } = context;
let router;
if (CAN_USE_ROUTER_EVENTS) {
router = owner.lookup('service:router');
// track pending transitions via the public routeWillChange / routeDidChange APIs
// routeWillChange can fire many times and is only useful to know when we have _started_
// transitioning, we can then use routeDidChange to signal that the transition has settled
router.on('routeWillChange', () => (routerTransitionsPending = true));
router.on('routeDidChange', () => (routerTransitionsPending = false));
} else {
router = owner.lookup('router:main');
ROUTER.set(context, router);
}
// hook into teardown to reset local settledness state
let ORIGINAL_WILL_DESTROY = router.willDestroy;
router.willDestroy = function () {
routerTransitionsPending = null;
return ORIGINAL_WILL_DESTROY.call(this);
};
}
/**
Navigate the application to the provided URL.
@public
@param {string} url The URL to visit (e.g. `/posts`)
@param {object} options app boot options
@returns {Promise<void>} resolves when settled
*/
export function visit(url: string, options?: { [key: string]: any }): Promise<void> {
const context = getContext();
if (!context || !isApplicationTestContext(context)) {
throw new Error('Cannot call `visit` without having first called `setupApplicationContext`.');
}
let { owner } = context;
let testMetadata = getTestMetadata(context);
testMetadata.usedHelpers.push('visit');
return Promise.resolve()
.then(() => {
return runHooks('visit', 'start', url, options);
})
.then(() => {
let visitResult = owner.visit(url, options);
setupRouterSettlednessTracking();
return visitResult;
})
.then(() => {
if (global.EmberENV._APPLICATION_TEMPLATE_WRAPPER !== false) {
context.element = document.querySelector('#ember-testing > .ember-view');
} else {
context.element = document.querySelector('#ember-testing');
}
})
.then(settled)
.then(() => {
return runHooks('visit', 'end', url, options);
});
}
/**
@public
@returns {string} the currently active route name
*/
export function currentRouteName(): string {
const context = getContext();
if (!context || !isApplicationTestContext(context)) {
throw new Error(
'Cannot call `currentRouteName` without having first called `setupApplicationContext`.'
);
}
let router = context.owner.lookup('router:main');
return get(router, 'currentRouteName');
}
const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion(2, 13);
/**
@public
@returns {string} the applications current url
*/
export function currentURL(): string {
const context = getContext();
if (!context || !isApplicationTestContext(context)) {
throw new Error(
'Cannot call `currentURL` without having first called `setupApplicationContext`.'
);
}
let router = context.owner.lookup('router:main');
if (HAS_CURRENT_URL_ON_ROUTER) {
return get(router, 'currentURL');
} else {
return get(router, 'location').getURL();
}
}
/**
Used by test framework addons to setup the provided context for working with
an application (e.g. routing).
`setupContext` must have been run on the provided context prior to calling
`setupApplicationContext`.
Sets up the basic framework used by application tests.
@public
@param {Object} context the context to setup
@returns {Promise<Object>} resolves with the context that was setup
*/
export default function setupApplicationContext(context: TestContext): Promise<void> {
let testMetadata: ITestMetadata = getTestMetadata(context);
testMetadata.setupTypes.push('setupApplicationContext');
return Promise.resolve();
}