Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

Passes original dom event object to beforeNavigate payload #179

Merged
merged 1 commit into from
Dec 15, 2016
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
6 changes: 4 additions & 2 deletions src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ class App extends EventEmitter {

var navigateFailed = false;
try {
this.navigate(utils.getUrlPath(href));
this.navigate(utils.getUrlPath(href), false, event);
} catch (err) {
// Do not prevent link navigation in case some synchronous error occurs
navigateFailed = true;
Expand Down Expand Up @@ -665,9 +665,10 @@ class App extends EventEmitter {
* Navigates to the specified path if there is a route handler that matches.
* @param {!string} path Path to navigate containing the base path.
* @param {boolean=} opt_replaceHistory Replaces browser history.
* @param {Event=} event Optional event object that triggered the navigation.
* @return {CancellablePromise} Returns a pending request cancellable promise.
*/
navigate(path, opt_replaceHistory) {
navigate(path, opt_replaceHistory, opt_event) {
if (!utils.isHtml5HistorySupported()) {
throw new Error('HTML5 History is not supported. Senna will not intercept navigation.');
}
Expand All @@ -679,6 +680,7 @@ class App extends EventEmitter {
}

this.emit('beforeNavigate', {
event: opt_event,
path: path,
replaceHistory: !!opt_replaceHistory
});
Expand Down
26 changes: 26 additions & 0 deletions test/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,32 @@ describe('App', function() {
exitDocumentLinkElement();
});

it('should pass original event object to "beforeNavigate" when a link is clicked', () => {
this.app = new App();
this.app.addRoutes(new Route('/path', Screen));
this.app.on('beforeNavigate', (data) => {
assert.ok(data.event);
assert.equal('click', data.event.type);
});
dom.triggerEvent(enterDocumentLinkElement('/path'), 'click');
exitDocumentLinkElement();

assert.notEqual('/path', window.location.pathname);
});

it('should prevent navigation on both senna and the browser via beforeNavigate', () => {
this.app = new App();
this.app.addRoutes(new Route('/preventedPath', Screen));
this.app.on('beforeNavigate', (data, event) => {
data.event.preventDefault();
event.preventDefault();
});
dom.triggerEvent(enterDocumentLinkElement('/preventedPath'), 'click');
exitDocumentLinkElement();

assert.notEqual('/preventedPath', window.location.pathname);
});

it('should not navigate when clicking on external links', () => {
var link = enterDocumentLinkElement('http://sennajs.com');
this.app = new App();
Expand Down