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 1 commit
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
32 changes: 18 additions & 14 deletions packages/@ember/-internals/routing/lib/services/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,25 @@ export default class RoutingService extends Service {

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;
try {
let visibleQueryParams = {};
if (queryParams) {
assign(visibleQueryParams, queryParams);
this.normalizeQueryParams(routeName, models, visibleQueryParams as QueryParam);
}

return router.generate(routeName, ...models, {
queryParams: visibleQueryParams,
});
} catch (e) {
// 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
if (!router._initialTransitionStarted) {
return;
} else {
throw e;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid the try/catch when we have started a transition, if we don't do that we break the ability to use the "break on uncaught exception" in the devtools.

I think a plausible solution here is something like:

_generateURL(routeName: string, models: {}[], queryParams: {}) {

}

generateURL(routeName: string, models: {}[], queryParams: {}) {
  let router = this.router;

  if (router._initialTransitionStarted) {
    try {
      return this._generateURL(routeName, models, queryParams);
    } catch (error) {
      return;
    }
  } else {
    return this._generateURL(routeName, models, queryParams);
  }
}

}

let visibleQueryParams = {};
if (queryParams) {
assign(visibleQueryParams, queryParams);
this.normalizeQueryParams(routeName, models, visibleQueryParams as QueryParam);
}

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

isActiveForRoute(
Expand Down