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

fix(link-manager): Use private router API to query currentURL in a side-effect-free way #518

Merged
merged 1 commit into from
Jan 29, 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
11 changes: 10 additions & 1 deletion addon/services/link-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
import { addListener, removeListener } from '@ember/object/events';
import RouteInfo from '@ember/routing/-private/route-info';
Expand Down Expand Up @@ -30,7 +31,15 @@ export default class LinkManagerService extends Service {
* @see https://github.com/buschtoens/ember-link/issues/126
*/
get isRouterInitialized() {
return this.router.currentURL !== null;
// Ideally we would use the public `router` service here, but accessing
// the `currentURL` on that service automatically starts the routing layer
// as a side-effect, which is not our intention here. Once or if Ember.js
// provides a flag on the `router` service to figure out if routing was
// already initialized we should switch back to the public service instead.
//
// Inspiration for this workaround was taken from the `currentURL()` test
// helper (see https://github.com/emberjs/ember-test-helpers/blob/v2.1.4/addon-test-support/@ember/test-helpers/setup-application-context.ts#L180)
return Boolean(getOwner(this).lookup('router:main').currentURL);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/components/link-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ module('Integration | Component | link', function (hooks) {
}
assert.strictEqual(currentURL(), null);
});

test('it does not break any following LinkTo components', async function (assert) {
await render(hbs`
<Link @route="parent.second-child" as |l|>
<a
data-test-link
href={{l.href}}
class={{if l.isActive "is-active"}}
{{on "click" l.transitionTo}}
>
Link
</a>
</Link>

<LinkTo @route="parent.second-child" data-test-link-to>
Link
</LinkTo>
`);

assert
.dom('[data-test-link]')
.hasAttribute('href', withSetupLink ? /ember\d+/ : '');
assert.dom('[data-test-link]').hasNoClass('is-active');

assert.dom('[data-test-link-to]').hasNoAttribute('href');
assert.dom('[data-test-link-to]').hasNoClass('is-active');
});
});
});
}
Expand Down