Skip to content

Commit

Permalink
✨ router.absolute helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien JUIF committed Jan 5, 2018
1 parent 606b4be commit 5ccc051
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Home extends React.Component {
// ...

export default router('HOME', { absolute: true })(Home)
// or
export default router.absolute('HOME')(Home)
```
- `/` : Home Component is **printed**
Expand Down
10 changes: 8 additions & 2 deletions build/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

exports.default = function (title, options) {
var hoc = function hoc(title, options) {
var isRouteFound = function isRouteFound(result) {
return result && result.title === title;
};
Expand Down Expand Up @@ -58,4 +58,10 @@ exports.default = function (title, options) {
return _react2.default.createElement(Component, newProps);
});
};
};
};

hoc.absolute = function (title, options) {
return hoc(title, _extends({}, options, { absolute: true }));
};

exports.default = hoc;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hoc-little-router",
"version": "1.0.1",
"version": "1.0.2",
"description": "HOC to hide a component when the route didn't match. Edit",
"license": "MIT",
"main": "build/index.js",
Expand Down
8 changes: 8 additions & 0 deletions src/__snapshots__/router.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ exports[`Router -redux-little-router- HOC should not print Component 1`] = `null

exports[`Router -redux-little-router- HOC should not print Component on second level (absolute mode) 1`] = `null`;

exports[`Router -redux-little-router- HOC should not print Component on second level (absolute mode, with helper) 1`] = `null`;

exports[`Router -redux-little-router- HOC should print Component on first level (with helper) 1`] = `
<div>
{\"prop1\":\"prop1\",\"show\":true}
</div>
`;
exports[`Router -redux-little-router- HOC should print Component on first level 1`] = `
<div>
{\"prop1\":\"prop1\",\"show\":true}
Expand Down
6 changes: 5 additions & 1 deletion src/router.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { connect } from 'react-redux'

export default (title, options) => {
const hoc = (title, options) => {
const isRouteFound = result => result && result.title === title
// This is genrated prop name to avoid overlaping props given from parent
const propName = `show_${title}`
Expand Down Expand Up @@ -46,3 +46,7 @@ export default (title, options) => {
},
)
}

hoc.absolute = (title, options) => hoc(title, { ...options, absolute: true })

export default hoc
21 changes: 18 additions & 3 deletions src/router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const store = createStore(() => ({
},
}))

const snap = (title, options, st = store) => {
const Decorated = router(title, options)(Component)

const snapImpl = Decorated => (st = store) => {
const component = renderer.create(
<Provider store={st}>
<Decorated prop1="prop1" show />
Expand All @@ -37,12 +35,29 @@ const snap = (title, options, st = store) => {
expect(tree).toMatchSnapshot()
}

const snap = (title, options, ...args) => {
const Decorated = router(title, options)(Component)

return snapImpl(Decorated)(...args)
}

describe('Router -redux-little-router- HOC', () => {
it('should print Component on first level', () => snap('TITLE_1'))
it('should not print Component', () => snap('TITLE_42'))
it('should print Component on second level', () => snap('TITLE_2'))
it('should not print Component on second level (absolute mode)', () => snap('TITLE_2', { absolute: true }))

it('should not print Component on second level (absolute mode, with helper)', () => {
const Decorated = router.absolute('TITLE_2')(Component)
snapImpl(Decorated)()
})

it('should print Component on first level (with helper)', () => {
const Decorated = router.absolute('TITLE_1')(Component)
snapImpl(Decorated)()
})


describe('Errors', () => {
let spy
beforeEach(() => { spy = sinon.stub(console, 'error') })
Expand Down

0 comments on commit 5ccc051

Please sign in to comment.