This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
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
12 changed files
with
255 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import {createAction} from 'redux-actions' | ||
import {INIT_ROUTER, URL_CHANGED} from './index' | ||
|
||
export const initRouter = createAction(INIT_ROUTER) | ||
import {URL_CHANGED, NAVIGATE} from './index' | ||
|
||
export const urlChanged = createAction(URL_CHANGED) | ||
export const navigate = createAction(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const INIT_ROUTER = 'INIT_ROUTER' | ||
export const URL_CHANGED = 'URL_CHANGED' | ||
export const NAVIGATE = '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
import * as actions from './actions' | ||
import {configureRouter} from './router' | ||
import {getUrl} from './utils' | ||
import {urlChanged} from './actions' | ||
|
||
export function getUrl(universal = false, store) { | ||
if (universal) { | ||
return store.getState().router.url | ||
} | ||
return window.location.pathname + window.location.search | ||
} | ||
|
||
export function handlePopState(store) { | ||
export const handlePopState = store => () => { | ||
const url = getUrl() | ||
store.dispatch(actions.urlChanged({url, source: 'popState'})) | ||
store.dispatch(urlChanged({url, source: 'popState'})) | ||
} | ||
|
||
export default function init(store, routes, aliases) { | ||
const url = getUrl() | ||
store.dispatch(actions.initRouter({url, routes, aliases})) | ||
window.onpopstate = handlePopState | ||
|
||
configureRouter(routes, aliases) | ||
|
||
store.dispatch(urlChanged({url, source: 'init'})) | ||
window.onpopstate = handlePopState(store) | ||
} |
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 |
---|---|---|
@@ -1,9 +1,26 @@ | ||
import {URL_CHANGED} from '../src/index' | ||
import handleActions from 'redux-actions' | ||
import uniloc from 'uniloc' | ||
import {getUrl} from './utils' | ||
import {handleActions} from 'redux-actions' | ||
import {URL_CHANGED, NAVIGATE} from '../src/index' | ||
import router from '../src/router' | ||
|
||
export function routeState(url) { | ||
return {route: router.lookup(url), url} | ||
} | ||
|
||
export default handleActions({ | ||
[URL_CHANGED]: (state, action) => { | ||
|
||
return Object.assign({}, state, routeState(action.payload.url)) | ||
}, | ||
[NAVIGATE]: (state, action) => { | ||
if (getUrl() !== action.payload.url) { | ||
if (action.payload.silent) { | ||
history.replaceState({}, null, action.payload.url) | ||
} else { | ||
history.pushState({}, null, action.payload.url) | ||
} | ||
|
||
return Object.assign({}, state, routeState(action.payload.url)) | ||
} | ||
return state | ||
}, | ||
}) |
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,9 @@ | ||
import uniloc from 'uniloc' | ||
|
||
let router = uniloc() | ||
|
||
export function configureRouter(routes, aliases) { | ||
router = uniloc(routes, aliases) | ||
} | ||
|
||
export default router |
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,6 @@ | ||
export function getUrl(universal = false, store) { | ||
if (universal) { | ||
return store.getState().router.url | ||
} | ||
return window.location.pathname + window.location.search | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,114 @@ | ||
import {expect} from 'chai' | ||
import * as actions from '../src/actions' | ||
import chai, {expect} from 'chai' | ||
import reducer, {routeState} from '../src/reducer' | ||
import sinon from 'sinon' | ||
import sinonChai from 'sinon-chai' | ||
|
||
chai.use(sinonChai) | ||
|
||
describe('reducer', () => { | ||
|
||
|
||
before(() => { | ||
global.window = { | ||
location: { | ||
pathname: '/space/unicorn', | ||
search: '?lasers=marshmallow', | ||
}, | ||
} | ||
}) | ||
|
||
after(() => { | ||
delete global.window | ||
}) | ||
|
||
describe('routeState()', () => { | ||
|
||
it('packages the route details up for the state', () => { | ||
const expected = { | ||
route: { | ||
name: undefined, | ||
options: {'bigger-on-the-inside': 'true'}, | ||
}, | ||
url: '/the/tardis?bigger-on-the-inside=true', | ||
} | ||
|
||
const result = routeState('/the/tardis?bigger-on-the-inside=true') | ||
|
||
expect(result).to.deep.equal(expected) | ||
}) | ||
|
||
}) | ||
|
||
describe('URL_CHANGED', () => { | ||
|
||
it('handles url changed events', () => { | ||
const action = actions.urlChanged({ | ||
url: '/my/awesome/url?awesome=true', | ||
source: 'test', | ||
}) | ||
const expected = routeState('/my/awesome/url?awesome=true') | ||
|
||
const result = reducer({}, action) | ||
|
||
expect(result).to.deep.equal(expected) | ||
}) | ||
|
||
}) | ||
|
||
describe('NAVIGATE', () => { | ||
|
||
before(() => { | ||
global.history = { | ||
pushState: sinon.spy(), | ||
replaceState: sinon.spy(), | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
global.history.pushState.reset() | ||
global.history.replaceState.reset() | ||
}) | ||
|
||
after(() => { | ||
delete global.history | ||
}) | ||
|
||
it('handles navigate events', () => { | ||
const action = actions.navigate({url: '/my/awesome/url?awesome=true'}) | ||
const expected = routeState('/my/awesome/url?awesome=true') | ||
|
||
const result = reducer({}, action) | ||
|
||
expect(result).to.deep.equal(expected) | ||
}) | ||
|
||
it('creates a new history entry with pushState', () => { | ||
const action = actions.navigate({url: '/into/the/tardis'}) | ||
|
||
reducer({}, action) | ||
|
||
expect(global.history.pushState).to.have.been.calledWith({}, null, '/into/the/tardis') | ||
expect(global.history.replaceState).to.not.have.been.called | ||
}) | ||
|
||
it('uses replaceState if silent is true', () => { | ||
const action = actions.navigate({url: '/into/the/tardis', silent: true}) | ||
|
||
reducer({}, action) | ||
|
||
expect(global.history.pushState).to.not.have.been.called | ||
expect(global.history.replaceState).to.have.been.calledWith({}, null, '/into/the/tardis') | ||
}) | ||
|
||
it('doesn\'t create a new history entry if the user is already on that location', () => { | ||
const action = actions.navigate({url: '/space/unicorn?lasers=marshmallow'}) | ||
|
||
reducer({}, action) | ||
|
||
expect(global.history.pushState).to.not.have.been.called | ||
expect(global.history.replaceState).to.not.have.been.called | ||
}) | ||
|
||
}) | ||
|
||
}) |
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,33 @@ | ||
import chai, {expect} from 'chai' | ||
import sinon from 'sinon' | ||
import sinonChai from 'sinon-chai' | ||
import proxyquire from 'proxyquire' | ||
|
||
chai.use(sinonChai) | ||
|
||
const unilocSpy = sinon.spy() | ||
|
||
const router = proxyquire('../src/router', { | ||
'uniloc': unilocSpy, | ||
}) | ||
|
||
describe('router', () => { | ||
|
||
afterEach(() => { | ||
unilocSpy.reset() | ||
}) | ||
|
||
describe('configureRouter()', () => { | ||
|
||
it('refreshes the router instance with new routes and aliases', () => { | ||
const routes = [] | ||
const aliases = {} | ||
|
||
router.configureRouter(routes, aliases) | ||
|
||
expect(unilocSpy).to.have.been.calledWith(routes, aliases) | ||
}) | ||
|
||
}) | ||
|
||
}) |
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,39 @@ | ||
import {getUrl} from '../src/utils' | ||
import {expect} from 'chai' | ||
|
||
describe('utils', () => { | ||
|
||
describe('getUrl()', () => { | ||
|
||
before(() => { | ||
global.window = { | ||
location: { | ||
pathname: '/space/unicorn', | ||
search: '?lasers=marshmallow', | ||
}, | ||
} | ||
}) | ||
|
||
after(() => { | ||
delete global.window | ||
}) | ||
|
||
it('pulls the url from window.location', () => { | ||
const result = getUrl() | ||
|
||
expect(result).to.equal('/space/unicorn?lasers=marshmallow') | ||
}) | ||
|
||
it('pulls the url from the state if this is a universal app', () => { | ||
const store = { | ||
getState: () => ({router: {url: '/space/unicorn?rainbows=delivered'}}), | ||
} | ||
|
||
const result = getUrl(true, store) | ||
|
||
expect(result).to.equal('/space/unicorn?rainbows=delivered') | ||
}) | ||
|
||
}) | ||
|
||
}) |