Skip to content

Commit

Permalink
Tests for canceling navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerpadilla committed May 27, 2015
1 parent c58bbc4 commit b3630e3
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 9 deletions.
8 changes: 8 additions & 0 deletions dist/prouter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/prouter.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/prouter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/prouter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/prouter.js.map

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/prouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,13 @@ export class Router {
static history: History;
// The previous route data.
private _oldRouteData: NavigationData;
// Copy event bus functionality.
/* tslint:disable:no-unused-variable */
private _eventHandlers: EventHandler = {};
/* tslint:enable:no-unused-variable */
trigger = History.prototype.trigger;
on = History.prototype.on;
off = History.prototype.off;

/**
* Constructor for the router.
Expand Down Expand Up @@ -752,6 +759,12 @@ export class Router {
return;
}

next = this.trigger('route:before', newRouteData, this._oldRouteData);

if (next === false) {
return;
}

if (this._oldRouteData && this._oldRouteData.handler.deactivate) {
next = this._oldRouteData.handler.deactivate.call(this._oldRouteData.handler, newRouteData, this._oldRouteData);
if (next === false) {
Expand Down
28 changes: 22 additions & 6 deletions test/router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,23 +263,39 @@
});
});

test('routes cancel navigation', 2, function() {
test('cancel navigation', 2, function() {
var router = new Router([{
route: 'something',
activate: function(newRouteData) {
activate: function() {
ok(false);
}
}, {
route: 'other',
activate: function() {
ok(false);
}
}]);
Router.history.on('route:before', function(router, newNavigationData, oldNavigationData) {
strictEqual(router, router);
equal(newNavigationData.path, 'something');
return false;
if (newNavigationData.path === 'something') {
strictEqual(router, router);
return false;
}
});
Router.history.on('route:after', function(router, newNavigationData, oldNavigationData) {
ok(false);
notEqual(newNavigationData.path, 'something');
});
router.on('route:before', function(newNavigationData, oldNavigationData) {
notEqual(newNavigationData.path, 'something');
if (newNavigationData.path === 'other') {
return false;
}
});
router.on('route:after', function(newNavigationData, oldNavigationData) {
notEqual(newNavigationData.path, 'other');
});
Router.history.start();
Router.history.navigate('something');
Router.history.navigate('other');
});

test('activate / deactivate callbacks', 9, function() {
Expand Down

0 comments on commit b3630e3

Please sign in to comment.