Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ hoc / not found routes #34

Merged
merged 1 commit into from
Oct 12, 2018
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
2 changes: 1 addition & 1 deletion components/react/k-ramel/dist/index.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/react/k-ramel/dist/index.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions components/react/k-ramel/src/__snapshots__/hoc.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ exports[`hoc array of codes should print relative route -strict equality- 1`] =
</div>
`;

exports[`hoc not found should print the failover component when route is not found 1`] = `
<div>
<div>
Not found component
</div>
</div>
`;

exports[`hoc uniq code (string) should not print absolute route -child- 1`] = `<div />`;

exports[`hoc uniq code (string) should print absolute route -strict equality- 1`] = `
Expand Down
10 changes: 10 additions & 0 deletions components/react/k-ramel/src/hoc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ const hoc = (code, options) => Component => class extends React.Component {
return
}

// The route is not found
// we can show the wrapped component if `notFound` is given into options
if (!router.isFound()) {
const show = (options && options.notFound)
this.setState(innerState => ({ ...innerState, show }))

return
}

// Absolute mode, we are looking in top level only
const codes = [].concat(code)
let currentRoute = router.getCurrentRoute()
Expand Down Expand Up @@ -81,5 +90,6 @@ const hoc = (code, options) => Component => class extends React.Component {
}

hoc.absolute = (code, options) => hoc(code, { ...options, absolute: true })
hoc.notFound = options => hoc(undefined, { ...options, notFound: true })

export default hoc
9 changes: 8 additions & 1 deletion components/react/k-ramel/src/hoc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createStore } from 'k-ramel'
import { provider } from '@k-ramel/react'
import hoc from './hoc'

const matchApp = (code, multiple = false) => () => {
const matchApp = (code, multiple = false, notFound) => () => {
const store = createStore(
{
dummy: (state = 'nothing') => state, // dummy reducer to get rid of error from redux
Expand All @@ -17,6 +17,7 @@ const matchApp = (code, multiple = false) => () => {
drivers: {
router: {
getDriver: () => ({
isFound: () => !notFound,
getState: () => 'state',
getResult: () => 'result',
getRoute: () => ({
Expand All @@ -34,10 +35,12 @@ const matchApp = (code, multiple = false) => () => {

const RelativeDecoratedComponent = hoc(multiple ? ['relative', 'dummy'] : 'relative')(() => <div>Relative component</div>)
const AbsoluteDecoratedComponent = hoc.absolute(multiple ? ['dummy', 'absolute'] : 'absolute')(() => <div>Absolute component</div>)
const NotFoundDecoratedComponent = hoc.notFound()(() => <div>Not found component</div>)
const App = provider(store)(() => (
<div>
<RelativeDecoratedComponent />
<AbsoluteDecoratedComponent />
<NotFoundDecoratedComponent />
</div>
))

Expand All @@ -58,4 +61,8 @@ describe('hoc', () => {
it('should print absolute route -strict equality-', matchApp('absolute', true))
it('should not print absolute route -child-', matchApp('absolute_child', true))
})

describe('not found', () => {
it('should print the failover component when route is not found', matchApp(undefined, false, true))
})
})
Loading