Skip to content

Commit

Permalink
Improves Router's methods in case if locale is not passed
Browse files Browse the repository at this point in the history
  • Loading branch information
favorit committed Mar 14, 2018
1 parent 3b5cfb4 commit 3e4184d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
7 changes: 5 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,16 @@ var Routes = function () {

var wrap = function wrap(method) {
return function (route, params, locale, options) {
var _findAndGetUrls = _this4.findAndGetUrls(route, locale, params),
var locale2 = typeof locale === 'string' ? locale : _this4.locale;
var options2 = (typeof locale === 'undefined' ? 'undefined' : (0, _typeof3.default)(locale)) === 'object' ? locale : options;

var _findAndGetUrls = _this4.findAndGetUrls(route, locale2, params),
byName = _findAndGetUrls.byName,
_findAndGetUrls$urls = _findAndGetUrls.urls,
as = _findAndGetUrls$urls.as,
href = _findAndGetUrls$urls.href;

return Router[method](href, as, byName ? options : params);
return Router[method](href, as, byName ? options2 : params);
};
};

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"scripts": {
"lint": "standard 'src/*' 'test/*'",
"build": "del dist/* && babel src -d dist",
"prepublishOnly": "npm run test",
"prepublishOnly": "yarn test",
"pretest": "npm run lint && npm run build",
"test": "npm run testOnly",
"test": "yarn testOnly",
"testOnly": "jest \\.test.js --coverage",
"testCI": "npm run test && cat coverage/lcov.info | coveralls",
"dev": "concurrently -k 'npm run build -- -w' 'npm run testOnly -- --watch'"
"testCI": "yarn test && cat coverage/lcov.info | coveralls",
"testWatch": "yarn test --watch",
"dev": "concurrently -k 'npm run build -- -w' 'yarn testOnly -- --watch'"
},
"standard": {
"parser": "babel-eslint"
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ class Routes {

getRouter (Router) {
const wrap = method => (route, params, locale, options) => {
const {byName, urls: {as, href}} = this.findAndGetUrls(route, locale, params)
return Router[method](href, as, byName ? options : params)
const locale2 = typeof locale === 'string' ? locale : this.locale
const options2 = typeof locale === 'object' ? locale : options

const {byName, urls: {as, href}} = this.findAndGetUrls(route, locale2, params)
return Router[method](href, as, byName ? options2 : params)
}

Router.pushRoute = wrap('push')
Expand Down
6 changes: 6 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ describe(`Router ${routerMethods.join(', ')}`, () => {
testMethods(['a', {b: 'b'}, 'en', {}], [href, as, {}])
})

test('with options and without locale', () => {
const {route, testMethods} = setup('a', 'en', '/a/:b')
const {as, href} = route.getUrls({b: 'b'})
testMethods(['a', {b: 'b'}, { shallow: true }], [href, as, { shallow: true }])
})

test('with route not found', () => {
setup('a', 'en').testException(['/b', 'en', {}])
})
Expand Down

0 comments on commit 3e4184d

Please sign in to comment.