Skip to content

Commit

Permalink
fix: LinkTo with incomplete model failing in rendering tests
Browse files Browse the repository at this point in the history
LinkTo needs route context to allow omitting model from current active
route. Without the guard, tests where LinkTo rendered in tests without
routing transition started will break. See issues/19364
  • Loading branch information
xg-wang committed Feb 9, 2021
1 parent 6802d0d commit 441931a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ moduleFor(
});
});
});
debugger;
return this.visit('/parents/1').then(() => {
debugger;
this.assertText('Link To Child');
});
}
Expand All @@ -143,7 +141,7 @@ moduleFor(

this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
attrs: { href: '#/' },
attrs: { href: null },
content: 'Go to Index',
});
}
Expand Down Expand Up @@ -173,7 +171,6 @@ moduleFor(

['@test should be able to be inserted in DOM when router is setup but not started']() {
this.render(`<LinkTo @route="dynamicWithChild.child">Link</LinkTo>`);
debugger;
this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
content: 'Link',
Expand Down
6 changes: 4 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,10 @@ 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 the router microlib is not present, which is the case for <LinkTo/> in integration tests
// also 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._routerMicrolib || !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 @@ -130,6 +130,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 @@ -507,6 +508,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 @@ -621,6 +623,7 @@ class EmberRouter extends EmberObject {
*/
reset() {
this._didSetupRouter = false;
this._initialTransitionStarted = false;
if (this._routerMicrolib) {
this._routerMicrolib.reset();
}
Expand Down Expand Up @@ -842,6 +845,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 441931a

Please sign in to comment.