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

[BUGFIX lts] LinkTo with incomplete model failing in rendering tests #19387

Merged
merged 3 commits into from
Feb 10, 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
@@ -1,5 +1,11 @@
import { moduleFor, ApplicationTestCase, RenderingTestCase, runTask } from 'internal-test-helpers';

import {
moduleFor,
ApplicationTestCase,
RenderingTestCase,
RouterNonApplicationTestCase,
runTask,
} from 'internal-test-helpers';
import { Router, Route } from '@ember/-internals/routing';
import Controller from '@ember/controller';
import { set } from '@ember/-internals/metal';
import { LinkComponent } from '@ember/-internals/glimmer';
Expand Down Expand Up @@ -92,6 +98,38 @@ moduleFor(
this.assertText('Hello');
});
}

['@test able to pupolate innermost dynamic segment when immediate parent route is active']() {
this.addTemplate('application', '{{outlet}}');
this.addTemplate('parents', '{{outlet}}');
this.addTemplate(
'parents.parent',
'<LinkTo @route="parents.parent.child" @model=1>Link To Child</LinkTo>'
);
this.addTemplate(
'parents.parent.child',
'<LinkTo @route="parents.parent">Link To Parent</LinkTo>'
);
this.add(
'route:parents.parent',
class extends Route {
async model({ id }) {
return { value: id };
}
}
);
this.router.map(function () {
this.route('parents', function () {
this.route('parent', { path: '/:parent_id' }, function () {
this.route('children');
this.route('child', { path: '/child/:child_id' });
});
});
});
return this.visit('/parents/1').then(() => {
this.assertText('Link To Child');
});
}
}
);

Expand All @@ -103,9 +141,40 @@ moduleFor(

this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
attrs: { href: '#/' },
attrs: { href: null },
content: 'Go to Index',
});
}
}
);

moduleFor(
'<LinkTo /> component (rendering tests, with router not started)',
class extends RouterNonApplicationTestCase {
constructor() {
super(...arguments);
this.resolver.add('router:main', Router.extend(this.routerOptions));
this.router.map(function () {
this.route('dynamicWithChild', { path: '/dynamic-with-child/:dynamic_id' }, function () {
this.route('child');
});
});
}
get routerOptions() {
return {
location: 'none',
};
}
get router() {
return this.owner.resolveRegistration('router:main');
}

['@test should be able to be inserted in DOM when router is setup but not started']() {
this.render(`<LinkTo @route="dynamicWithChild.child">Link</LinkTo>`);
this.assertComponentElement(this.element.firstChild, {
tagName: 'a',
content: 'Link',
});
}
}
);
5 changes: 3 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,9 @@ 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 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;
}

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