Skip to content

Commit

Permalink
[BUGFIX lts] LinkTo with incomplete model failing in rendering tests
Browse files Browse the repository at this point in the history
(cherry picked from commit a8746b9)
  • Loading branch information
rwjblue committed Feb 10, 2021
1 parent a827b58 commit 444dedc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { moduleFor, ApplicationTestCase, RenderingTestCase, runTask } from 'internal-test-helpers';

import {
moduleFor,
ApplicationTestCase,
RenderingTestCase,
RouterNonApplicationTestCase,
runTask,
} from 'internal-test-helpers';
import { Router, Route } from '@ember/-internals/routing';
import Controller from '@ember/controller';
import { set } from '@ember/-internals/metal';
import { LinkComponent } from '@ember/-internals/glimmer';
Expand Down Expand Up @@ -92,6 +98,38 @@ moduleFor(
this.assertText('Hello');
});
}

['@test able to pupolate innermost dynamic segment when immediate parent route is active']() {
this.addTemplate('application', '{{outlet}}');
this.addTemplate('parents', '{{outlet}}');
this.addTemplate(
'parents.parent',
'<LinkTo @route="parents.parent.child" @model=1>Link To Child</LinkTo>'
);
this.addTemplate(
'parents.parent.child',
'<LinkTo @route="parents.parent">Link To Parent</LinkTo>'
);
this.add(
'route:parents.parent',
class extends Route {
async model({ id }) {
return { value: id };
}
}
);
this.router.map(function () {
this.route('parents', function () {
this.route('parent', { path: '/:parent_id' }, function () {
this.route('children');
this.route('child', { path: '/child/:child_id' });
});
});
});
return this.visit('/parents/1').then(() => {
this.assertText('Link To Child');
});
}
}
);

Expand All @@ -103,9 +141,40 @@ moduleFor(

this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
attrs: { href: '#/' },
attrs: { href: null },
content: 'Go to Index',
});
}
}
);

moduleFor(
'<LinkTo /> component (rendering tests, with router not started)',
class extends RouterNonApplicationTestCase {
constructor() {
super(...arguments);
this.resolver.add('router:main', Router.extend(this.routerOptions));
this.router.map(function () {
this.route('dynamicWithChild', { path: '/dynamic-with-child/:dynamic_id' }, function () {
this.route('child');
});
});
}
get routerOptions() {
return {
location: 'none',
};
}
get router() {
return this.owner.resolveRegistration('router:main');
}

['@test should be able to be inserted in DOM when router is setup but not started']() {
this.render(`<LinkTo @route="dynamicWithChild.child">Link</LinkTo>`);
this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
content: 'Link',
});
}
}
);
5 changes: 3 additions & 2 deletions packages/@ember/-internals/routing/lib/services/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ export default class RoutingService extends Service {

generateURL(routeName: string, models: {}[], queryParams: {}) {
let router = this.router;
// return early when the router microlib is not present, which is the case for {{link-to}} in integration tests
if (!router._routerMicrolib) {
// Return early when transition has not started, when rendering in tests without visit(),
// we cannot infer the route context which <LinkTo/> needs be aware of
if (!router._initialTransitionStarted) {
return;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class EmberRouter extends EmberObject {
rootURL!: string;
_routerMicrolib!: Router<Route>;
_didSetupRouter = false;
_initialTransitionStarted = false;

currentURL: string | null = null;
currentRouteName: string | null = null;
Expand Down Expand Up @@ -500,6 +501,7 @@ class EmberRouter extends EmberObject {
}

_doURLTransition(routerJsMethod: string, url: string) {
this._initialTransitionStarted = true;
let transition = this._routerMicrolib[routerJsMethod](url || '/');
didBeginTransition(transition, this);
return transition;
Expand Down Expand Up @@ -614,6 +616,7 @@ class EmberRouter extends EmberObject {
*/
reset() {
this._didSetupRouter = false;
this._initialTransitionStarted = false;
if (this._routerMicrolib) {
this._routerMicrolib.reset();
}
Expand Down Expand Up @@ -835,6 +838,8 @@ class EmberRouter extends EmberObject {
Boolean(targetRouteName) && this._routerMicrolib.hasRoute(targetRouteName)
);

this._initialTransitionStarted = true;

let queryParams = {};

this._processActiveTransitionQueryParams(targetRouteName, models, queryParams, _queryParams);
Expand Down

0 comments on commit 444dedc

Please sign in to comment.