Skip to content

Commit

Permalink
Get rid of a couple unsafe casts
Browse files Browse the repository at this point in the history
- use `assert`s in `setup-application-context` for router lookups
- bind an intermediate term in a conditional in `build-registry` to
  preserve the control-flow-based narrowing in the loop
  • Loading branch information
chriskrycho committed Jul 22, 2022
1 parent cc3844b commit 245d3d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Expand Up @@ -29,13 +29,12 @@ function exposeRegistryMethodsWithoutDeprecations(container: any) {
];

for (let i = 0, l = methods.length; i < l; i++) {
let method = methods[i];
let methodName = methods[i];

if (method && method in container) {
container[method] = function (...args: unknown[]) {
// SAFETY: `method` is defined because we *just* checked that it is in
// the conditional wrapping this.
return container._registry[method!](...args);
if (methodName && methodName in container) {
const knownMethod = methodName;
container[knownMethod] = function (...args: unknown[]) {
return container._registry[knownMethod](...args);
};
}
}
Expand Down
Expand Up @@ -12,6 +12,8 @@ import settled from './settled';
import getTestMetadata from './test-metadata';
import { runHooks } from './-internal/helper-hooks';
import { Router } from '@ember/routing';
import { RouterService } from '@ember/routing/router-service';
import { assert } from '@ember/debug';

export interface ApplicationTestContext extends TestContext {
element?: Element | null;
Expand Down Expand Up @@ -88,17 +90,24 @@ export function setupRouterSettlednessTracking() {
HAS_SETUP_ROUTER.set(context, true);

let { owner } = context;
let router: Router;
let router: Router | RouterService;
if (CAN_USE_ROUTER_EVENTS) {
router = owner.lookup('service:router') as Router;
let routerService = owner.lookup('service:router');
assert(
'router service is not available',
routerService instanceof RouterService
);
router = routerService;

// 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') as Router;
let mainRouter = owner.lookup('router:main');
assert('router:main is not available', mainRouter instanceof Router);
router = mainRouter;
ROUTER.set(context, router);
}

Expand Down

0 comments on commit 245d3d6

Please sign in to comment.