Skip to content

Commit

Permalink
Resolves #19004: RouterService#isActive() now consumes currentURL and…
Browse files Browse the repository at this point in the history
… currentRouteName
  • Loading branch information
NullVoxPopuli committed Aug 14, 2020
1 parent a497a8e commit 37b2a77
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assign } from '@ember/polyfills';
import Service from '@ember/service';
import { DEBUG } from '@glimmer/env';
import { Transition } from 'router_js';
import { consumeTag, tagFor } from '@glimmer/validator';
import EmberRouter, { QueryParam } from '../system/router';
import { extractRouteArgs, resemblesURL, shallowEqual } from '../utils';

Expand Down Expand Up @@ -309,6 +310,19 @@ 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, 'currentURL'));
consumeTag(tagFor(this, 'currentRouteName'));

// 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

0 comments on commit 37b2a77

Please sign in to comment.