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 d26a1bf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 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 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
3 changes: 3 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 @@ -414,6 +415,7 @@ class EmberRouter extends EmberObject {
if (initialTransition && initialTransition.error) {
throw initialTransition.error;
}
this._initialTransitionStarted = true;
}
}

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

0 comments on commit d26a1bf

Please sign in to comment.