Skip to content

Commit

Permalink
Merge pull request #19395 from xg-wang/link-to-render-regression
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Feb 11, 2021
2 parents c180944 + af4a1b4 commit f5ffa06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ moduleFor(

this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
attrs: { href: null },
attrs: { href: '#/' },
content: 'Go to Index',
});
}
Expand Down Expand Up @@ -169,10 +169,24 @@ moduleFor(
return this.owner.resolveRegistration('router:main');
}

['@test should be able to be inserted in DOM when router is setup but not started']() {
['@test should be able to be inserted in DOM when initial transition not started']() {
this.render(`<LinkTo @route="dynamicWithChild.child">Link</LinkTo>`);
this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
attrs: {
href: null,
},
content: 'Link',
});
}

['@test should be able to be inserted in DOM with valid href when complete models are passed even if initial transition is not started']() {
this.render(`<LinkTo @route="dynamicWithChild.child" @model="1">Link</LinkTo>`);
this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
attrs: {
href: '/dynamic-with-child/1/child',
},
content: 'Link',
});
}
Expand Down
25 changes: 16 additions & 9 deletions packages/@ember/-internals/routing/lib/services/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,32 @@ export default class RoutingService extends Service {
this.router._prepareQueryParams(routeName, models, queryParams);
}

generateURL(routeName: string, models: {}[], queryParams: {}) {
let router = this.router;
// 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;
}

_generateURL(routeName: string, models: {}[], queryParams: {}) {
let visibleQueryParams = {};
if (queryParams) {
assign(visibleQueryParams, queryParams);
this.normalizeQueryParams(routeName, models, visibleQueryParams as QueryParam);
}

return router.generate(routeName, ...models, {
return this.router.generate(routeName, ...models, {
queryParams: visibleQueryParams,
});
}

generateURL(routeName: string, models: {}[], queryParams: {}) {
if (this.router._initialTransitionStarted) {
return this._generateURL(routeName, models, queryParams);
} else {
// Swallow error when transition has not started.
// When rendering in tests without visit(), we cannot infer the route context which <LinkTo/> needs be aware of
try {
return this._generateURL(routeName, models, queryParams);
} catch (_e) {
return;
}
}
}

isActiveForRoute(
contexts: {}[],
queryParams: QueryParam | undefined,
Expand Down

0 comments on commit f5ffa06

Please sign in to comment.