@@ -4,21 +4,17 @@ import {
4
4
isTestContext ,
5
5
type TestContext ,
6
6
} from './setup-context.ts' ;
7
- import hasEmberVersion from './has-ember-version.ts' ;
8
7
import settled from './settled.ts' ;
9
8
import getTestMetadata from './test-metadata.ts' ;
10
9
import { runHooks } from './helper-hooks.ts' ;
11
- import type Router from '@ember/routing/router' ;
12
10
import type RouterService from '@ember/routing/router-service' ;
13
11
import { assert } from '@ember/debug' ;
14
12
15
13
export interface ApplicationTestContext extends TestContext {
16
14
element ?: Element | null ;
17
15
}
18
16
19
- const CAN_USE_ROUTER_EVENTS = hasEmberVersion ( 3 , 6 ) ;
20
17
let routerTransitionsPending : boolean | null = null ;
21
- const ROUTER = new WeakMap ( ) ;
22
18
const HAS_SETUP_ROUTER = new WeakMap ( ) ;
23
19
24
20
// eslint-disable-next-line require-jsdoc
@@ -35,33 +31,7 @@ export function isApplicationTestContext(
35
31
@returns {(boolean|null) } if there are pending transitions
36
32
*/
37
33
export function hasPendingTransitions ( ) : boolean | null {
38
- if ( CAN_USE_ROUTER_EVENTS ) {
39
- return routerTransitionsPending ;
40
- }
41
-
42
- const context = getContext ( ) ;
43
-
44
- // there is no current context, we cannot check
45
- if ( context === undefined ) {
46
- return null ;
47
- }
48
-
49
- const router = ROUTER . get ( context ) ;
50
-
51
- if ( router === undefined ) {
52
- // if there is no router (e.g. no `visit` calls made yet), we cannot
53
- // check for pending transitions but this is explicitly not an error
54
- // condition
55
- return null ;
56
- }
57
-
58
- const routerMicrolib = router . _routerMicrolib || router . router ;
59
-
60
- if ( routerMicrolib === undefined ) {
61
- return null ;
62
- }
63
-
64
- return ! ! routerMicrolib . activeTransition ;
34
+ return routerTransitionsPending ;
65
35
}
66
36
67
37
/**
@@ -87,29 +57,19 @@ export function setupRouterSettlednessTracking() {
87
57
HAS_SETUP_ROUTER . set ( context , true ) ;
88
58
89
59
const { owner } = context ;
90
- let router : Router | RouterService ;
91
- if ( CAN_USE_ROUTER_EVENTS ) {
92
- // SAFETY: unfortunately we cannot `assert` here at present because the
93
- // class is not exported, only the type, since it is not designed to be
94
- // sub-classed. The most we can do at present is assert that it at least
95
- // *exists* and assume that if it does exist, it is bound correctly.
96
- const routerService = owner . lookup ( 'service:router' ) ;
97
- assert ( 'router service is not set up correctly' , ! ! routerService ) ;
98
- router = routerService as RouterService ;
99
-
100
- // track pending transitions via the public routeWillChange / routeDidChange APIs
101
- // routeWillChange can fire many times and is only useful to know when we have _started_
102
- // transitioning, we can then use routeDidChange to signal that the transition has settled
103
- router . on ( 'routeWillChange' , ( ) => ( routerTransitionsPending = true ) ) ;
104
- router . on ( 'routeDidChange' , ( ) => ( routerTransitionsPending = false ) ) ;
105
- } else {
106
- // SAFETY: similarly, this cast cannot be made safer because on the versions
107
- // where we fall into this path, this is *also* not an exported class.
108
- const mainRouter = owner . lookup ( 'router:main' ) ;
109
- assert ( 'router:main is not available' , ! ! mainRouter ) ;
110
- router = mainRouter as Router ;
111
- ROUTER . set ( context , router ) ;
112
- }
60
+
61
+ // SAFETY: unfortunately we cannot `assert` here at present because the
62
+ // class is not exported, only the type, since it is not designed to be
63
+ // sub-classed. The most we can do at present is assert that it at least
64
+ // *exists* and assume that if it does exist, it is bound correctly.
65
+ const router = owner . lookup ( 'service:router' ) as RouterService ;
66
+ assert ( 'router service is not set up correctly' , ! ! router ) ;
67
+
68
+ // track pending transitions via the public routeWillChange / routeDidChange APIs
69
+ // routeWillChange can fire many times and is only useful to know when we have _started_
70
+ // transitioning, we can then use routeDidChange to signal that the transition has settled
71
+ router . on ( 'routeWillChange' , ( ) => ( routerTransitionsPending = true ) ) ;
72
+ router . on ( 'routeDidChange' , ( ) => ( routerTransitionsPending = false ) ) ;
113
73
114
74
// hook into teardown to reset local settledness state
115
75
const ORIGINAL_WILL_DESTROY = router . willDestroy ;
@@ -196,8 +156,6 @@ export function currentRouteName(): string {
196
156
return currentRouteName ;
197
157
}
198
158
199
- const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion ( 2 , 13 ) ;
200
-
201
159
/**
202
160
@public
203
161
@returns {string } the applications current url
@@ -211,30 +169,22 @@ export function currentURL(): string {
211
169
}
212
170
213
171
const router = context . owner . lookup ( 'router:main' ) as any ;
172
+ const routerCurrentURL = router . currentURL ;
173
+
174
+ // SAFETY: this path is a lie for the sake of the public-facing types. The
175
+ // framework itself sees this path, but users never do. A user who calls the
176
+ // API without calling `visit()` will see an `UnrecognizedURLError`, rather
177
+ // than getting back `null`.
178
+ if ( routerCurrentURL === null ) {
179
+ return routerCurrentURL as never as string ;
180
+ }
214
181
215
- if ( HAS_CURRENT_URL_ON_ROUTER ) {
216
- const routerCurrentURL = router . currentURL ;
217
-
218
- // SAFETY: this path is a lie for the sake of the public-facing types. The
219
- // framework itself sees this path, but users never do. A user who calls the
220
- // API without calling `visit()` will see an `UnrecognizedURLError`, rather
221
- // than getting back `null`.
222
- if ( routerCurrentURL === null ) {
223
- return routerCurrentURL as never as string ;
224
- }
225
-
226
- assert (
227
- `currentUrl should be a string, but was ${ typeof routerCurrentURL } ` ,
228
- typeof routerCurrentURL === 'string' ,
229
- ) ;
182
+ assert (
183
+ `currentUrl should be a string, but was ${ typeof routerCurrentURL } ` ,
184
+ typeof routerCurrentURL === 'string' ,
185
+ ) ;
230
186
231
- return routerCurrentURL ;
232
- } else {
233
- // SAFETY: this is *positively ancient* and should probably be removed at
234
- // some point; old routers which don't have `currentURL` *should* have a
235
- // `location` with `getURL()` per the docs for 2.12.
236
- return ( router . location as any ) . getURL ( ) ;
237
- }
187
+ return routerCurrentURL ;
238
188
}
239
189
240
190
/**
0 commit comments