Skip to content

Commit

Permalink
Merge branch 'master' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
kelp404 committed Jun 27, 2021
2 parents ff50676 + 932ec44 commit fe14b19
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-and-upload-coveralls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push]

jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
node-version: [14.x, 16.x]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [pull_request]

jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
strategy:
matrix:
node-version: [14.x, 16.x]
Expand Down
6 changes: 6 additions & 0 deletions __tests__/__snapshots__/route.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`Generate a route with the parent. 1`] = `
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -29,6 +30,7 @@ exports[`Generate a route with the parent. 2`] = `
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -40,6 +42,7 @@ Route {
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand Down Expand Up @@ -81,6 +84,7 @@ exports[`Initial Route with a parent. 1`] = `
Route {
"component": undefined,
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -92,6 +96,7 @@ Route {
Route {
"component": undefined,
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -117,6 +122,7 @@ exports[`Initial Route without a parent. 1`] = `
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand Down
9 changes: 9 additions & 0 deletions __tests__/__snapshots__/router.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exports[`Find the route by the location. 1`] = `
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -59,6 +60,7 @@ exports[`Find the router belong a abstract router by the location. 1`] = `
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -70,6 +72,7 @@ Route {
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": true,
"loadComponent": undefined,
Expand All @@ -95,6 +98,7 @@ exports[`Get the current route. 1`] = `
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -116,6 +120,7 @@ Array [
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -133,6 +138,7 @@ Array [
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand Down Expand Up @@ -165,6 +171,7 @@ Array [
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand All @@ -182,6 +189,7 @@ Array [
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand Down Expand Up @@ -240,6 +248,7 @@ Array [
Route {
"component": [Function],
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand Down
1 change: 1 addition & 0 deletions __tests__/__snapshots__/utils.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exports[`Find the route by the name. 1`] = `
Route {
"component": undefined,
"dismissalDelay": undefined,
"generateLocation": [Function],
"generateUri": [Function],
"isAbstract": false,
"loadComponent": undefined,
Expand Down
6 changes: 2 additions & 4 deletions __tests__/components/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,14 @@ test('Link component calls router.go() when it was clicked.', () => {
router.go = jest.fn(() => {});
const component = renderer.create(<Link to="https://github.com">GitHub</Link>);
const tree = component.toJSON();
tree.props.onClick(new Event('click'));
tree.props.onClick({preventDefault: () => {}});
expect(router.go).toBeCalledWith('https://github.com');
});

test('Link component does not call router.go() when it was clicked with the meta key.', () => {
router.go = jest.fn(() => {});
const component = renderer.create(<Link to="https://github.com">GitHub</Link>);
const tree = component.toJSON();
const event = new Event('click');
event.metaKey = true;
tree.props.onClick(event);
tree.props.onClick({metaKey: true, preventDefault: () => {}});
expect(router.go).not.toBeCalled();
});
19 changes: 15 additions & 4 deletions __tests__/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ const historyActions = require('../lib/constants/history-actions');

let router;
beforeEach(() => {
global.location = {
hash: '',
host: 'localhost:8000',
hostname: 'localhost',
href: 'http://localhost:8000',
origin: 'http://localhost:8000',
pathname: '/',
port: '8000',
protocol: 'http:',
search: ''
};
router = new Router({
history: history.createMemoryHistory({initialEntries: ['/']}),
routes: [
Expand Down Expand Up @@ -50,13 +61,13 @@ afterEach(() => jest.restoreAllMocks());
test('Going to a page with the URI will push the history state.', () => {
router.history.push = jest.fn(() => {});
router.go('/login');
expect(router.history.push).toBeCalledWith('/login');
expect(router.history.push).toBeCalledWith({pathname: '/login', search: ''}, undefined);
});

test('Replace a page with the URI.', () => {
router.history.replace = jest.fn(() => {});
router.go('/login', {replace: true});
expect(router.history.replace).toBeCalledWith('/login');
expect(router.history.replace).toBeCalledWith({pathname: '/login', search: ''}, undefined);
});

test('Reload a page with the URI.', () => {
Expand All @@ -72,7 +83,7 @@ test('Going to a page with a route name will push the history state.', () => {
params: {userId: 'AWgrmJp1SjjuUM2bzZXM', index: 0}
});
expect(router.history.push).toBeCalledWith(
'/users/AWgrmJp1SjjuUM2bzZXM/projects?index=0',
{pathname: '/users/AWgrmJp1SjjuUM2bzZXM/projects', search: '?index=0'},
{
name: 'projects',
params: {index: 0, userId: 'AWgrmJp1SjjuUM2bzZXM'}
Expand All @@ -87,7 +98,7 @@ test('Replace a page with a route name.', () => {
{replace: true}
);
expect(router.history.replace).toBeCalledWith(
'/users/AWgrmJp1SjjuUM2bzZXM/projects?index=0',
{pathname: '/users/AWgrmJp1SjjuUM2bzZXM/projects', search: '?index=0'},
{
name: 'projects',
params: {index: 0, userId: 'AWgrmJp1SjjuUM2bzZXM'}
Expand Down
16 changes: 8 additions & 8 deletions example/web.js

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module.exports = class Route {
this.matchReg = new RegExp(`^${this.matchPattern}$`);
this.uriTemplate += uriTemplate;

this.generateLocation = this.generateLocation.bind(this);
this.generateUri = this.generateUri.bind(this);
}

Expand All @@ -92,11 +93,10 @@ module.exports = class Route {
}

/**
* Generate the URI of the route with params.
* @param {Object} params - Params of the route.
* @returns {string} - The URI.
* @returns {{pathname: string, search: (string|null)}} - The location.
*/
generateUri(params) {
generateLocation(params) {
let uri = this.uriTemplate;
const query = {};

Expand All @@ -110,9 +110,20 @@ module.exports = class Route {
});

if (Object.keys(query).length) {
return `${uri}?${queryString.stringify(query)}`;
return {pathname: uri, search: `?${queryString.stringify(query)}`};
}

return uri;
return {pathname: uri, search: null};
}

/**
* Generate the URI of the route with params.
* @param {Object} params - Params of the route.
* @returns {string} - The URI.
*/
generateUri(params) {
const {pathname, search} = this.generateLocation(params);

return `${pathname}${search || ''}`;
}
};
31 changes: 22 additions & 9 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ module.exports = class Router {
* @returns {undefined}
*/
go(target, options = {}) {
let historyDestination;
let historyState;
let currentUri;

this.isReloadNextHistoryChange = Boolean(options.reload);
Expand All @@ -382,22 +384,33 @@ module.exports = class Router {
if (typeof target === 'string') {
if (currentUri === target) {
this.reload();
} else if (options.replace) {
this.history.replace(target);
} else {
this.history.push(target);
return;
}

const url = new URL(target, location.origin);

historyDestination = {
pathname: url.pathname,
search: url.search || ''
};
} else {
const route = utils.findRouteByNameInRoutes(target.name, this.routes);
const uri = route.generateUri(target.params || {});
const {pathname, search} = route.generateLocation(target.params || {});
const uri = `${pathname}${search || ''}`;

if (currentUri === uri) {
this.reload();
} else if (options.replace) {
this.history.replace(uri, {name: target.name, params: target.params || {}});
} else {
this.history.push(uri, {name: target.name, params: target.params || {}});
return;
}

historyDestination = {pathname, search: search || ''};
historyState = {name: target.name, params: target.params || {}};
}

if (options.replace) {
this.history.replace(historyDestination, historyState);
} else {
this.history.push(historyDestination, historyState);
}
}

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "capybara-router",
"version": "2.3.0",
"version": "2.4.0",
"description": "Unfancy react router without flux and redux.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -58,12 +58,12 @@
"eslint-plugin-react": "7.23.2",
"eslint-plugin-react-hooks": "4.2.0",
"express": "4.17.1",
"jest": "26.6.3",
"jest": "27.0.5",
"nodemon": "2.0.7",
"npm-check-updates": "11.5.13",
"nprogress": "0.2.0",
"react-dom": "16.13.1",
"react-test-renderer": "16.13.1",
"react-dom": "16.14.0",
"react-test-renderer": "16.14.0",
"webpack": "4.44.2",
"webpack-cli": "3.3.12",
"webpack-dev-server": "3.11.2"
Expand Down

0 comments on commit fe14b19

Please sign in to comment.