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

Backport fix for #19004: RouterService#isActive() now consumes currentURL to 3.20 #19158

Merged
merged 1 commit into from
Sep 28, 2020
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
13 changes: 13 additions & 0 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { assert } from '@ember/debug';
import { readOnly } from '@ember/object/computed';
import Service from '@ember/service';
import { DEBUG } from '@glimmer/env';
import { consumeTag, tagFor } from '@glimmer/validator';
import { Transition } from 'router_js';
import EmberRouter, { QueryParam } from '../system/router';
import { extractRouteArgs, resemblesURL, shallowEqual } from '../utils';
Expand Down Expand Up @@ -308,6 +309,18 @@ export default class RouterService extends Service {
let { routeName, models, queryParams } = extractRouteArgs(args);
let routerMicrolib = this._router._routerMicrolib;

// When using isActive() in a getter, we want to entagle with the auto-tracking system
// for example,
// in
// get isBarActive() {
// return isActive('foo.bar');
// }
//
// you'd expect isBarActive to be dirtied when the route changes.
//
// https://github.com/emberjs/ember.js/issues/19004
consumeTag(tagFor(this._router, 'currentURL'));

// UNSAFE: casting `routeName as string` here encodes the existing
// assumption but may be wrong: `extractRouteArgs` correctly returns it as
// `string | undefined`. There may be bugs if `isActiveIntent` does
Expand Down
37 changes: 37 additions & 0 deletions packages/ember/tests/routing/router_service_test/isActive_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Controller from '@ember/controller';
import { RouterTestCase, moduleFor } from 'internal-test-helpers';
import Service, { inject as service } from '@ember/service';

moduleFor(
'Router Service - isActive',
Expand Down Expand Up @@ -33,6 +34,42 @@ moduleFor(
});
}

async ['@test RouterService#isActive entangles with route transitions'](assert) {
assert.expect(6);

this.add(
`service:foo`,
class extends Service {
@service router;

get isChildActive() {
return this.router.isActive('parent.child');
}

get isSisterActive() {
return this.router.isActive('parent.sister');
}
}
);

await this.visit('/');

let fooService = this.applicationInstance.lookup('service:foo');

assert.equal(fooService.isChildActive, false);
assert.equal(fooService.isSisterActive, false);

await this.routerService.transitionTo('parent.child');

assert.equal(fooService.isChildActive, true);
assert.equal(fooService.isSisterActive, false);

await this.routerService.transitionTo('parent.sister');

assert.equal(fooService.isChildActive, false);
assert.equal(fooService.isSisterActive, true);
}

['@test RouterService#isActive does not eagerly instantiate controller for query params'](
assert
) {
Expand Down