Skip to content
This repository was archived by the owner on Dec 15, 2018. It is now read-only.
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
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ import routerMiddleware from './middleware';
import routerReducer from './reducer';
import Link from './link';
import createMatcher from './create-matcher';
import { LOCATION_CHANGED } from './action-types';
import {
LOCATION_CHANGED,
PUSH,
REPLACE,
GO,
GO_FORWARD,
GO_BACK
} from './action-types';

export {
createStoreWithRouter,
routerMiddleware,
routerReducer,
Link,
createMatcher,
LOCATION_CHANGED
LOCATION_CHANGED,
PUSH,
REPLACE,
GO,
GO_FORWARD,
GO_BACK
};
13 changes: 8 additions & 5 deletions src/link.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import React, { Component, PropTypes } from 'react';
import { PUSH } from './action-types';
import { PUSH, REPLACE } from './action-types';

export default class Link extends Component {
onClick(event) {
event.preventDefault();
this.props.dispatch({
type: PUSH,
const { replaceState, dispatch } = this.props;

dispatch({
type: replaceState ? REPLACE : PUSH,
payload: {
pathname: this.props.href
}
});
}

render() {
const { href, history, children} = this.props;
const { href, history, children } = this.props;
return (
<a
{...this.props}
Expand All @@ -30,5 +32,6 @@ Link.propTypes = {
href: PropTypes.string,
children: PropTypes.node,
dispatch: PropTypes.func,
history: PropTypes.object
history: PropTypes.object,
replaceState: PropTypes.bool
};
9 changes: 5 additions & 4 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
} from './action-types';

const locationDidChange = location => {
const { basename, pathname, action } = location;
const { basename, pathname, action, state } = location;
return {
type: LOCATION_CHANGED,
payload: {
action,
state,
url: `${basename || ''}${pathname}`
.replace(/\/$/, '') // remove trailing slash
}
Expand All @@ -28,13 +29,13 @@ export default history => {

switch (action.type) {
case PUSH:
history.push(action.payload.pathname);
history.push(action.payload);
break;
case REPLACE:
history.replace(action.payload.pathname);
history.replace(action.payload);
break;
case GO:
history.go(action.payload.index);
history.go(action.payload);
break;
case GO_BACK:
history.goBack();
Expand Down
5 changes: 2 additions & 3 deletions src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ export default (routes, createMatcher = matcherFactory) => {
return {
current: {
...matchRoute(action.payload.url),
url: action.payload.url
...action.payload
},
previous: state.current,
historyAction: action.payload.action
previous: state.current
};
}
return state;
Expand Down
18 changes: 17 additions & 1 deletion test/spec/link.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { mount } from 'enzyme';

import { PUSH } from 'src/action-types';
import { PUSH, REPLACE } from 'src/action-types';
import Link from 'src/link';

describe('Router link component', () => {
Expand All @@ -20,4 +20,20 @@ describe('Router link component', () => {
);
wrapper.find('a').simulate('click');
});

it('dispatches a REPLACE action with the correct href when clicked', done => {
const dispatch = action => {
expect(action).to.deep.equal({
type: REPLACE,
payload: {
pathname: '/yo'
}
});
done();
};
const wrapper = mount(
<Link replaceState dispatch={dispatch} href='/yo' />
);
wrapper.find('a').simulate('click');
});
});
12 changes: 9 additions & 3 deletions test/spec/middleware.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ describe('Router middleware', () => {
Object.keys(actions).forEach(actionType => {
const expected = actions[actionType];

it(`dispatches location changes with ${actionType}`, done => {
it(`dispatches location changes with ${actionType} and associated state`, done => {
const action = {
type: actionType,
payload: {
pathname: expected.url
pathname: expected.url,
state: {
bork: 'bork'
}
}
};
testRouterMiddleware(action, done, resultAction => {
expect(resultAction).to.deep.equal({
type: LOCATION_CHANGED,
payload: {
url: expected.url,
action: expected.action
action: expected.action,
state: {
bork: 'bork'
}
}
});
});
Expand Down
25 changes: 20 additions & 5 deletions test/spec/mocks/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,50 @@ export default class MockHistory {
push() {
this.callback({
pathname: '/push',
action: 'PUSH'
action: 'PUSH',
state: {
bork: 'bork'
}
});
}

replace() {
this.callback({
pathname: '/replace',
action: 'REPLACE'
action: 'REPLACE',
state: {
bork: 'bork'
}
});
}

go() {
this.callback({
pathname: '/go',
action: 'REPLACE'
action: 'REPLACE',
state: {
bork: 'bork'
}
});
}

goBack() {
this.callback({
pathname: '/goBack',
action: 'POP'
action: 'POP',
state: {
bork: 'bork'
}
});
}

goForward() {
this.callback({
pathname: '/goForward',
action: 'PUSH'
action: 'PUSH',
state: {
bork: 'bork'
}
});
}
}
14 changes: 10 additions & 4 deletions test/spec/reducer.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ describe('Router reducer', () => {
type: LOCATION_CHANGED,
payload: {
url: '/rofl',
action: 'PUSH'
action: 'PUSH',
state: {
bork: 'bork'
}
}
};
const result = routerReducer({}, mockCreateMatcher)({}, action);
Expand All @@ -23,10 +26,13 @@ describe('Router reducer', () => {
current: {
params: {},
result: 'rofl',
url: '/rofl'
url: '/rofl',
action: 'PUSH',
state: {
bork: 'bork'
}
},
previous: undefined,
historyAction: 'PUSH'
previous: undefined
});
});

Expand Down