@@ -4,21 +4,17 @@ import {
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' ;
11- import type Router from '@ember/routing/router' ;
1210import type RouterService from '@ember/routing/router-service' ;
1311import { assert } from '@ember/debug' ;
1412
1513export interface ApplicationTestContext extends TestContext {
1614 element ?: Element | null ;
1715}
1816
19- const CAN_USE_ROUTER_EVENTS = hasEmberVersion ( 3 , 6 ) ;
2017let routerTransitionsPending : boolean | null = null ;
21- const ROUTER = new WeakMap ( ) ;
2218const HAS_SETUP_ROUTER = new WeakMap ( ) ;
2319
2420// eslint-disable-next-line require-jsdoc
@@ -35,33 +31,7 @@ export function isApplicationTestContext(
3531 @returns {(boolean|null) } if there are pending transitions
3632*/
3733export 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 ;
6535}
6636
6737/**
@@ -87,29 +57,19 @@ export function setupRouterSettlednessTracking() {
8757 HAS_SETUP_ROUTER . set ( context , true ) ;
8858
8959 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 ) ) ;
11373
11474 // hook into teardown to reset local settledness state
11575 const ORIGINAL_WILL_DESTROY = router . willDestroy ;
@@ -196,8 +156,6 @@ export function currentRouteName(): string {
196156 return currentRouteName ;
197157}
198158
199- const HAS_CURRENT_URL_ON_ROUTER = hasEmberVersion ( 2 , 13 ) ;
200-
201159/**
202160 @public
203161 @returns {string } the applications current url
@@ -211,30 +169,22 @@ export function currentURL(): string {
211169 }
212170
213171 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+ }
214181
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+ ) ;
230186
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 ;
238188}
239189
240190/**
0 commit comments