44 isTestContext ,
55 type TestContext ,
66} from './setup-context.ts' ;
7- import hasEmberVersion from './has-ember-version.ts' ;
87import settled from './settled.ts' ;
98import getTestMetadata from './test-metadata.ts' ;
109import { runHooks } from './helper-hooks.ts' ;
@@ -16,9 +15,7 @@ export interface ApplicationTestContext extends TestContext {
1615 element ?: Element | null ;
1716}
1817
19- const CAN_USE_ROUTER_EVENTS = hasEmberVersion ( 3 , 6 ) ;
2018let routerTransitionsPending : boolean | null = null ;
21- const ROUTER = new WeakMap ( ) ;
2219const HAS_SETUP_ROUTER = new WeakMap ( ) ;
2320
2421// eslint-disable-next-line require-jsdoc
@@ -35,33 +32,7 @@ export function isApplicationTestContext(
3532 @returns {(boolean|null) } if there are pending transitions
3633*/
3734export 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 ;
6536}
6637
6738/**
@@ -87,29 +58,19 @@ export function setupRouterSettlednessTracking() {
8758 HAS_SETUP_ROUTER . set ( context , true ) ;
8859
8960 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 ) ) ;
11374
11475 // hook into teardown to reset local settledness state
11576 const ORIGINAL_WILL_DESTROY = router . willDestroy ;
@@ -196,8 +157,6 @@ export function currentRouteName(): string {
196157 return currentRouteName ;
197158}
198159
199- const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion ( 2 , 13 ) ;
200-
201160/**
202161 @public
203162 @returns {string } the applications current url
@@ -211,30 +170,22 @@ export function currentURL(): string {
211170 }
212171
213172 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+ }
214182
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+ ) ;
230187
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 ;
238189}
239190
240191/**
0 commit comments