Skip to content

Commit

Permalink
Lazily setup the router in non-application tests for <LinkTo> component
Browse files Browse the repository at this point in the history
PR emberjs#19080 fixed the issue where accessing public router service throwing
error, however, the PR did not support LinkTo component which uses the
internal -routing service. This PR adds similar lazy setup to
-routing.router.
  • Loading branch information
xg-wang committed Dec 29, 2020
1 parent 2bb6445 commit e751cb0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { moduleFor, ApplicationTestCase, RenderingTestCase, runTask } from 'internal-test-helpers';
import {
moduleFor,
ApplicationTestCase,
RenderingTestCase,
runTask,
} from 'internal-test-helpers';

import Controller from '@ember/controller';
import { set } from '@ember/-internals/metal';
Expand Down Expand Up @@ -101,7 +106,7 @@ moduleFor(
['@test should be able to be inserted in DOM when the router is not present - block']() {
this.render(`<LinkTo @route='index'>Go to Index</LinkTo>`);

this.assertText('Go to Index');
this.assertComponentElement(this.element.firstChild, { tagName: 'a', attrs: { href: '#/' }, content: 'Go to Index' });
}
}
);
16 changes: 15 additions & 1 deletion packages/@ember/-internals/routing/lib/services/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
@module ember
*/

import { getOwner, Owner } from '@ember/-internals/owner';
import { readOnly } from '@ember/object/computed';
import { assign } from '@ember/polyfills';
import Service from '@ember/service';
import { symbol } from '@ember/-internals/utils';
import EmberRouter, { QueryParam } from '../system/router';
import RouterState from '../system/router_state';

const ROUTER = symbol('ROUTER') as string;

/**
The Routing service is used by LinkComponent, and provides facilities for
the component/view layer to interact with the router.
Expand All @@ -19,7 +23,17 @@ import RouterState from '../system/router_state';
@class RoutingService
*/
export default class RoutingService extends Service {
router!: EmberRouter;
get router(): EmberRouter {
let router = this[ROUTER];
if (router !== undefined) {
return router;
}
const owner = getOwner(this) as Owner;
router = owner.lookup('router:main') as EmberRouter;
router.setupRouter();
return (this[ROUTER] = router);
}

hasRoute(routeName: string) {
return this.router.hasRoute(routeName);
}
Expand Down
2 changes: 0 additions & 2 deletions packages/@ember/engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,6 @@ function commonSetupRegistry(registry) {

// Register the routing service...
registry.register('service:-routing', RoutingService);
// Then inject the app router into it
registry.injection('service:-routing', 'router', 'router:main');

// DEBUGGING
registry.register('resolver-for-debugging:main', registry.resolver, {
Expand Down

0 comments on commit e751cb0

Please sign in to comment.