-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
276 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2019 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import switchPath from 'switch-path'; | ||
|
||
export class Router { | ||
constructor(routeDefinitions) { | ||
const defaultRoute = { | ||
// do nothing if url was not matched | ||
'*': null, | ||
}; | ||
const routes = Object.assign({}, defaultRoute, routeDefinitions); | ||
|
||
const parseRoute = () => { | ||
const currentPath = window.location.hash.replace('#', '') || '/'; | ||
|
||
const {value} = switchPath(currentPath, routes); | ||
return typeof value === 'function' ? value() : value; | ||
}; | ||
|
||
/** | ||
* We need to proxy the replaceState method of history in order to trigger an event and warn the app that something happens. | ||
* This is needed because the replaceState method can be called from outside of the app (eg. in the desktop app) | ||
* @returns {void} | ||
*/ | ||
const originalReplaceState = window.history.replaceState.bind(window.history); | ||
|
||
window.history.replaceState = (...args) => { | ||
originalReplaceState(...args); | ||
parseRoute(); | ||
}; | ||
window.addEventListener('hashchange', parseRoute); | ||
|
||
// tigger an initial parsing of the current url | ||
parseRoute(); | ||
} | ||
|
||
navigate(path) { | ||
window.history.replaceState(null, null, `#${path}`); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2018 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import ko from 'knockout'; | ||
|
||
export function initRouterBindings(routerInstance) { | ||
ko.bindingHandlers.link_to = { | ||
init(element, valueAccessor) { | ||
const navigate = event => { | ||
routerInstance.navigate(valueAccessor()); | ||
event.preventDefault(); | ||
}; | ||
element.addEventListener('click', navigate); | ||
|
||
ko.utils.domNodeDisposal.addDisposeCallback(element, () => element.removeEventListener('click', navigate)); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2019 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {Router} from 'src/script/router/Router'; | ||
|
||
describe('Router', () => { | ||
let originalReplaceStateFn; | ||
|
||
beforeEach(() => { | ||
originalReplaceStateFn = window.history.replaceState; | ||
}); | ||
|
||
afterEach(() => { | ||
window.location.hash = '#'; | ||
window.history.replaceState = originalReplaceStateFn; | ||
}); | ||
|
||
describe('constructor', () => { | ||
it("overwrites browser's replaceState method", () => { | ||
const originalReplaceState = window.history.replaceState; | ||
new Router(); | ||
|
||
expect(originalReplaceState).not.toBe(window.history.replaceState); | ||
}); | ||
|
||
it('parse the current URL when instantiated', () => { | ||
const routes = {'/conv': () => {}}; | ||
spyOn(routes, '/conv'); | ||
|
||
window.location.hash = '#/conv'; | ||
new Router(routes); | ||
|
||
expect(routes['/conv']).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('navigate', () => { | ||
it('allows to navigate to specific url and call the associated handler', () => { | ||
const handlers = {conversation: () => {}, user: () => {}}; | ||
|
||
spyOn(handlers, 'conversation'); | ||
spyOn(handlers, 'user'); | ||
|
||
const router = new Router({ | ||
'/conversation/:id': handlers.conversation, | ||
'/user/:id': handlers.user, | ||
}); | ||
|
||
router.navigate('/nomatch'); | ||
|
||
expect(handlers.conversation).not.toHaveBeenCalled(); | ||
expect(handlers.user).not.toHaveBeenCalled(); | ||
|
||
router.navigate('/conversation/uuid'); | ||
|
||
expect(handlers.conversation).toHaveBeenCalled(); | ||
expect(handlers.user).not.toHaveBeenCalled(); | ||
|
||
router.navigate('/user/uuid'); | ||
|
||
expect(handlers.user).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('hash change event listener', () => { | ||
it('triggers routing when a hashchange event is triggered', done => { | ||
const handlers = {conversation: () => {}}; | ||
spyOn(handlers, 'conversation'); | ||
|
||
new Router({'/conversation/:id': handlers.conversation}); | ||
|
||
expect(handlers.conversation).not.toHaveBeenCalled(); | ||
|
||
window.location.hash = '#/conversation/uuid'; | ||
|
||
setTimeout(() => { | ||
expect(handlers.conversation).toHaveBeenCalled(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('history.replaceState proxy', () => { | ||
it('calls the matching handler when a new state is replaced', () => { | ||
const routes = { | ||
'/conversation/:id': () => {}, | ||
'/user/:id': () => {}, | ||
}; | ||
|
||
spyOn(routes, '/conversation/:id'); | ||
spyOn(routes, '/user/:id'); | ||
|
||
new Router(routes); | ||
|
||
window.history.replaceState('', '', '#/nomatch'); | ||
|
||
expect(routes['/conversation/:id']).not.toHaveBeenCalled(); | ||
expect(routes['/user/:id']).not.toHaveBeenCalled(); | ||
|
||
window.history.replaceState('', '', '#/conversation/uuid'); | ||
|
||
expect(routes['/conversation/:id']).toHaveBeenCalled(); | ||
expect(routes['/user/:id']).not.toHaveBeenCalled(); | ||
|
||
window.history.replaceState('', '', '#/user/uuid'); | ||
|
||
expect(routes['/user/:id']).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2019 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import {initRouterBindings} from 'src/script/router/routerBindings'; | ||
import {bindHtml} from '../../api/knockoutHelpers'; | ||
|
||
describe('routerBindings', () => { | ||
let mockRouter; | ||
beforeEach(() => { | ||
mockRouter = {navigate: () => {}}; | ||
initRouterBindings(mockRouter); | ||
spyOn(mockRouter, 'navigate'); | ||
}); | ||
|
||
it('handles click and triggers router navigation', async () => { | ||
const url = '/conversation/uuid'; | ||
const domElement = await bindHtml(`<a data-bind="link_to: '${url}'">click me</a>`); | ||
domElement.querySelector('a').click(); | ||
|
||
expect(mockRouter.navigate).toHaveBeenCalledWith(url); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters