feat(router): implement Router.isRouteActive

This commit is contained in:
Brian Ford
2015-08-30 21:25:46 -07:00
parent e1a7e0329c
commit de37729823
5 changed files with 241 additions and 192 deletions
@@ -105,6 +105,7 @@ function makeDummyLocation() {
function makeDummyRouter() {
var dr = new SpyRouter();
dr.spy('generate').andCallFake((routeParams) => dummyInstruction);
dr.spy('isRouteActive').andCallFake((_) => false);
dr.spy('navigateInstruction');
return dr;
}
+32 -12
View File
@@ -53,9 +53,9 @@ export function main() {
var outlet = makeDummyOutlet();
router.config([new Route({path: '/', component: DummyComponent})])
.then((_) => router.registerOutlet(outlet))
.then((_) => router.registerPrimaryOutlet(outlet))
.then((_) => {
expect(outlet.spy('commit')).toHaveBeenCalled();
expect(outlet.spy('activate')).toHaveBeenCalled();
expect(location.urlChanges).toEqual([]);
async.done();
});
@@ -66,11 +66,11 @@ export function main() {
inject([AsyncTestCompleter], (async) => {
var outlet = makeDummyOutlet();
router.registerOutlet(outlet)
router.registerPrimaryOutlet(outlet)
.then((_) => router.config([new Route({path: '/a', component: DummyComponent})]))
.then((_) => router.navigate('/a'))
.then((_) => {
expect(outlet.spy('commit')).toHaveBeenCalled();
expect(outlet.spy('activate')).toHaveBeenCalled();
expect(location.urlChanges).toEqual(['/a']);
async.done();
});
@@ -80,11 +80,11 @@ export function main() {
inject([AsyncTestCompleter], (async) => {
var outlet = makeDummyOutlet();
router.registerOutlet(outlet)
router.registerPrimaryOutlet(outlet)
.then((_) => router.config([new Route({path: '/b', component: DummyComponent})]))
.then((_) => router.navigate('/b', true))
.then((_) => {
expect(outlet.spy('commit')).toHaveBeenCalled();
expect(outlet.spy('activate')).toHaveBeenCalled();
expect(location.urlChanges).toEqual([]);
async.done();
});
@@ -94,14 +94,14 @@ export function main() {
it('should navigate after being configured', inject([AsyncTestCompleter], (async) => {
var outlet = makeDummyOutlet();
router.registerOutlet(outlet)
router.registerPrimaryOutlet(outlet)
.then((_) => router.navigate('/a'))
.then((_) => {
expect(outlet.spy('commit')).not.toHaveBeenCalled();
expect(outlet.spy('activate')).not.toHaveBeenCalled();
return router.config([new Route({path: '/a', component: DummyComponent})]);
})
.then((_) => {
expect(outlet.spy('commit')).toHaveBeenCalled();
expect(outlet.spy('activate')).toHaveBeenCalled();
async.done();
});
}));
@@ -142,13 +142,33 @@ export function main() {
inject([AsyncTestCompleter], (async) => {
var outlet = makeDummyOutlet();
router.registerOutlet(outlet);
router.registerPrimaryOutlet(outlet);
router.config([new AsyncRoute({path: '/first', loader: loader, as: 'FirstCmp'})]);
var instruction = router.generate(['/FirstCmp']);
router.navigateInstruction(instruction)
.then((_) => {
expect(outlet.spy('commit')).toHaveBeenCalled();
expect(outlet.spy('activate')).toHaveBeenCalled();
async.done();
});
}));
it('should return whether a given instruction is active with isRouteActive',
inject([AsyncTestCompleter], (async) => {
var outlet = makeDummyOutlet();
router.registerPrimaryOutlet(outlet)
.then((_) => router.config([
new Route({path: '/a', component: DummyComponent, as: 'A'}),
new Route({path: '/b', component: DummyComponent, as: 'B'})
]))
.then((_) => router.navigate('/a'))
.then((_) => {
var instruction = router.generate(['/A']);
var otherInstruction = router.generate(['/B']);
expect(router.isRouteActive(instruction)).toEqual(true);
expect(router.isRouteActive(otherInstruction)).toEqual(false);
async.done();
});
}));
@@ -213,7 +233,7 @@ function makeDummyOutlet() {
ref.spy('canActivate').andCallFake((_) => PromiseWrapper.resolve(true));
ref.spy('canReuse').andCallFake((_) => PromiseWrapper.resolve(false));
ref.spy('canDeactivate').andCallFake((_) => PromiseWrapper.resolve(true));
ref.spy('commit').andCallFake((_) => PromiseWrapper.resolve(true));
ref.spy('activate').andCallFake((_) => PromiseWrapper.resolve(true));
return ref;
}