Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
feat: support initialisation of params with an object of key value pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jan 19, 2016
1 parent 35677f9 commit c78355a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
15 changes: 8 additions & 7 deletions modules/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const persistentParamsPlugin = params => router => {
const persistentParamsPlugin = (params = {}) => router => {
// Persistent parameters
const persistentParams = params.reduce(
(acc, param) => ({ ...acc, [param]: undefined }),
{}
);
const persistentParams = Array.isArray(params)
? params.reduce((acc, param) => ({ ...acc, [param]: undefined }), {})
: params;

const paramNames = Object.keys(persistentParams);

// Root node path
const path = router.rootNode.path.split('?')[0] + params.length ? '?' + params.join('&') : '';
const path = router.rootNode.path.split('?')[0] + paramNames.length ? '?' + paramNames.join('&') : '';
router.rootNode.setPath(path);

const { buildPath, buildState } = router;
Expand All @@ -26,7 +27,7 @@ const persistentParamsPlugin = params => router => {
name: 'PERSISTENT_PARAMS',
onTransitionSuccess(toState) {
Object.keys(toState.params)
.filter(p => params.indexOf(p) !== -1)
.filter(p => paramNames.indexOf(p) !== -1)
.forEach(p => persistentParams[p] = toState.params[p]);
}
};
Expand Down
61 changes: 44 additions & 17 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,62 @@ import { expect } from 'chai';
import Router5 from 'router5';
import persistentParamsPlugin from '../modules';

const router = new Router5([
const createRouter = () => new Router5([
{ name: 'route1', path: '/route1/:id' },
{ name: 'route2', path: '/route2/:id' }
]);

describe('router5 persistent params plugin', () => {
it('should be registered with params', () => {
router.usePlugin(persistentParamsPlugin(['mode']));
});
let router;

it('should persist specified parameters', (done) => {
router.start();
router.navigate('route1', { id: '1', mode: 'dev' }, {}, (err, state) => {
expect(state.path).to.equal('/route1/1?mode=dev');
describe('with an array', () => {
before(() => {
router = createRouter();
});

router.navigate('route2', { id: '2' }, {}, (err, state) => {
expect(state.path).to.equal('/route2/2?mode=dev');
done();
it('should be registered with params', () => {
router.usePlugin(persistentParamsPlugin(['mode']));
});

it('should persist specified parameters', (done) => {
router.start();
router.navigate('route1', { id: '1', mode: 'dev' }, {}, (err, state) => {
expect(state.path).to.equal('/route1/1?mode=dev');

router.navigate('route2', { id: '2' }, {}, (err, state) => {
expect(state.path).to.equal('/route2/2?mode=dev');
done();
});
});
});

it('should save value on start', (done) => {
router.stop();
router.start('/route2/1?mode=dev', (err, state) => {
expect(state.params).to.eql({ mode: 'dev', id: '1' });

router.navigate('route2', { id: '2' }, {}, (err, state) => {
expect(state.path).to.equal('/route2/2?mode=dev');
done();
});
});
});
});

it('should save value on start', (done) => {
router.stop();
router.start('/route2/1?mode=dev', (err, state) => {
expect(state.params).to.eql({ mode: 'dev', id: '1' });
describe('with an object', () => {
before(() => {
router.stop();
router = createRouter();
});

it('should be registered with params', () => {
router.usePlugin(persistentParamsPlugin({'mode': 'dev'}));
});

router.navigate('route2', { id: '2' }, {}, (err, state) => {
expect(state.path).to.equal('/route2/2?mode=dev');
it('should persist specified parameters', (done) => {
router.start();
router.navigate('route1', { id: '1' }, {}, (err, state) => {
expect(state.path).to.equal('/route1/1?mode=dev');
done();
});
});
Expand Down

0 comments on commit c78355a

Please sign in to comment.