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' ;
@@ -16,9 +15,7 @@ export interface ApplicationTestContext extends TestContext {
16
15
element ?: Element | null ;
17
16
}
18
17
19
- const CAN_USE_ROUTER_EVENTS = hasEmberVersion ( 3 , 6 ) ;
20
18
let routerTransitionsPending : boolean | null = null ;
21
- const ROUTER = new WeakMap ( ) ;
22
19
const HAS_SETUP_ROUTER = new WeakMap ( ) ;
23
20
24
21
// eslint-disable-next-line require-jsdoc
@@ -35,33 +32,7 @@ export function isApplicationTestContext(
35
32
@returns {(boolean|null) } if there are pending transitions
36
33
*/
37
34
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 ;
35
+ return routerTransitionsPending ;
65
36
}
66
37
67
38
/**
@@ -87,29 +58,19 @@ export function setupRouterSettlednessTracking() {
87
58
HAS_SETUP_ROUTER . set ( context , true ) ;
88
59
89
60
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
- }
61
+
62
+ // SAFETY: unfortunately we cannot `assert` here at present because the
63
+ // class is not exported, only the type, since it is not designed to be
64
+ // sub-classed. The most we can do at present is assert that it at least
65
+ // *exists* and assume that if it does exist, it is bound correctly.
66
+ const router = owner . lookup ( 'service:router' ) as RouterService ;
67
+ assert ( 'router service is not set up correctly' , ! ! routerService ) ;
68
+
69
+ // track pending transitions via the public routeWillChange / routeDidChange APIs
70
+ // routeWillChange can fire many times and is only useful to know when we have _started_
71
+ // transitioning, we can then use routeDidChange to signal that the transition has settled
72
+ router . on ( 'routeWillChange' , ( ) => ( routerTransitionsPending = true ) ) ;
73
+ router . on ( 'routeDidChange' , ( ) => ( routerTransitionsPending = false ) ) ;
113
74
114
75
// hook into teardown to reset local settledness state
115
76
const ORIGINAL_WILL_DESTROY = router . willDestroy ;
@@ -196,8 +157,6 @@ export function currentRouteName(): string {
196
157
return currentRouteName ;
197
158
}
198
159
199
- const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion ( 2 , 13 ) ;
200
-
201
160
/**
202
161
@public
203
162
@returns {string } the applications current url
@@ -211,30 +170,22 @@ export function currentURL(): string {
211
170
}
212
171
213
172
const router = context . owner . lookup ( 'router:main' ) as any ;
173
+ const routerCurrentURL = router . currentURL ;
174
+
175
+ // SAFETY: this path is a lie for the sake of the public-facing types. The
176
+ // framework itself sees this path, but users never do. A user who calls the
177
+ // API without calling `visit()` will see an `UnrecognizedURLError`, rather
178
+ // than getting back `null`.
179
+ if ( routerCurrentURL === null ) {
180
+ return routerCurrentURL as never as string ;
181
+ }
214
182
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
- ) ;
183
+ assert (
184
+ `currentUrl should be a string, but was ${ typeof routerCurrentURL } ` ,
185
+ typeof routerCurrentURL === 'string' ,
186
+ ) ;
230
187
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
- }
188
+ return routerCurrentURL ;
238
189
}
239
190
240
191
/**
0 commit comments