Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LTS fix] only return empty href when LinkTo href generation throws error #19395

Merged
merged 2 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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