30db14b1 |
const mockPushState = window.history.pushState;
const {middleware, enhancer} = installBrowserRouter(routesConfig, window);
const reduce = jest.fn();
const store = createStore(reduce, compose(enhancer, applyMiddleware(middleware)));
function urlChanges() {
return mockPushState.mock.calls.map(item => item[2]);
}
function actionsDispatched() {
return reduce.mock.calls.map(item => item[1]).slice(1);
}
function fireUrlChange(path) {
window.dispatchEvent(new CustomEvent('urlchanged', {detail: createLocation(path)}));
}
return {store, reduce, window, urlChanges, actionsDispatched, fireUrlChange};
}
it("router handles exact match in preference to wildcard match", () => {
//given
const actionType = 'THE_ACTION';
const action = {type: actionType, id: 1};
const routesConfig = [
["/somewhere/:id", actionType, {}],
["/somewhere", actionType, {id: 1}],
];
const {urlChanges, store} = setupTest(routesConfig);
// when
store.dispatch(action);
// then
expect(urlChanges()).toEqual(['/somewhere']);
});
it("router handles wildcard with extra args correctly", () => {
//given
const actionType = 'THE_ACTION';
const action = {type: actionType, id: 1, view: "home"};
const routesConfig = [
["/somewhere/:id/:view", actionType, {}],
["/somewhere/:id/default", actionType, {view: "home"}],
];
const {urlChanges, store} = setupTest(routesConfig);
// when
store.dispatch(action);
// then
expect(urlChanges()).toEqual(['/somewhere/1/default']);
});
it("router handles wildcard with extraArgs correctly with reverse order", () => {
//given
const actionType = 'THE_ACTION';
const action = {type: actionType, id: 1, view: "home"};
const routesConfig = [
["/somewhere/:id/default", actionType, {view: "home"}],
["/somewhere/:id/:view", actionType, {}],
];
const {urlChanges, store} = setupTest(routesConfig);
// when
store.dispatch(action);
// then
expect(urlChanges()).toEqual(['/somewhere/1/default']);
});
it("router handles wildcard without extraArgs correctly", () => {
//given
const actionType = 'THE_ACTION';
const action = {type: actionType, id: 1};
const routesConfig = [
["/somewhere/:id/default", actionType, {}],
];
const {urlChanges, store} = setupTest(routesConfig);
// when
store.dispatch(action);
// then
expect(urlChanges()).toEqual(['/somewhere/1/default']);
});
it("router handles wildcard with no match correctly", () => {
//given
const actionType = 'THE_ACTION';
const action = {type: actionType, foo: 1};
const routesConfig = [
["/somewhere/:id/default", actionType, {}],
];
const {urlChanges, store} = setupTest(routesConfig);
// when
store.dispatch(action);
// then ( no url changes triggered)
expect(urlChanges()).toEqual([]);
});
it("router does not match when all args are not accounted for", () => {
//given
const actionType = 'THE_ACTION';
const action = {type: actionType, id: 1, view: "home"};
const routesConfig = [
["/somewhere/:id/default", actionType, {}],
];
const {urlChanges, store} = setupTest(routesConfig);
// when
store.dispatch(action);
// then ( no url changes triggered)
expect(urlChanges()).toEqual([]);
});
it("router should match non-wildcard route in preference to wildcard route", () => {
// given
const routesConfig = [
['/somewhere/:id', 'ACTION_NAME', {}],
["/somewhere/specific", 'ACTION_NAME', {id: 1}],
];
const {actionsDispatched, fireUrlChange} = setupTest(routesConfig);
// when
fireUrlChange('/somewhere/specific');
// then
expect(actionsDispatched()).toEqual([{type: 'ACTION_NAME', id: 1}]);
});
|
10f5ef83 |
it("router should throw on duplicate paths", () => {
// given
const routesConfig = [
['/somewhere/:id', 'ACTION_NAME', {}],
["/somewhere/:id", 'ACTION_NAME', {}],
];
expect( () => {
setupTest(routesConfig);
}).toThrow();
});
it("router should throw on equally specific routes", () => {
// given
const routesConfig = [
['/somewhere/:id', 'ACTION_NAME', {}],
["/somewhere/:specific", 'ACTION_NAME', {}],
];
expect( () => {
const {actionsDispatched, fireUrlChange} = setupTest(routesConfig);
fireUrlChange('/somewhere/specific');
}).toThrow();
});
|
d93719b8 |
});
it("router should propagate matches through non-matching cases", () => {
//given
const routesConfig = [
["/somewhere/specific/:view", "ACTION_NAME", {id: 1}],
["/somewhere/:id/:view", "ACTION_NAME", {}],
["/not/a/match", "ACTION_NAME", {}]
];
const {actionsDispatched, fireUrlChange} = setupTest(routesConfig);
// when
fireUrlChange('/somewhere/specific/etc');
// then
|
30db14b1 |
expect(actionsDispatched()).toEqual([{type:'ACTION_NAME', id: 1, view: "etc"}]);
});
it("router should give precedence to exact match first in equally-specific routes (/a/:b vs /:a/b)", () => {
// given
const routesConfig = [
["/something/:dynamic", "ACTION_NAME", {}],
["/:dyn/something", "ACTION_NAME", {}],
];
const {actionsDispatched, fireUrlChange} = setupTest(routesConfig);
// when
fireUrlChange("/something/something");
// then
expect(actionsDispatched()).toEqual([{type: 'ACTION_NAME', dynamic: 'something'}]);
});
|