Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Commit

Permalink
Improved check is busy mountPath
Browse files Browse the repository at this point in the history
Update README.md.
Remove 5 version node from CI.
Improve examples.
  • Loading branch information
MrEfrem committed Nov 26, 2016
1 parent e43e13a commit 5d465cf
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 43 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
node_js:
- "4"
- "5"
- "6"

cache:
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ This API allow you to creation of reused components, organization effective code

#### Example of container component creation which stores the state in store of Redux.
```javascript
// Root component
import React from 'react';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { enhanceStore } from 'redux-fly';
import Counter from './Counter';

const store = createStore(null, enhanceStore);

export default () => (
<Provider store={store}>
<Counter/>
</Provider>
);

// Counter component
import React from 'react';
import { createReducer } from 'redux-fly';
Expand All @@ -24,26 +39,11 @@ const Counter = ({ reduxState: { counter }, reduxSetState }) => (
);

export default createReducer({
mountPath: 'ui counter',
mountPath: 'ui counter', // Reducer mounting path
initialState: {
counter: 0
}
})(Counter);

// Root component
import React from 'react';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { enhanceStore } from 'redux-fly';
import Counter from './Counter';

const store = createStore(null, enhanceStore);

export default () => (
<Provider store={store}>
<Counter/>
</Provider>
);
```

#### Example of creation reused modal window component which stores the state in store of Redux.
Expand Down
50 changes: 50 additions & 0 deletions __tests__/createReducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,56 @@ test('Test is invalid to create of reducer in same mounting path', () => {
)).toThrowError('Mount path "ui component" already busy')
})

test('Test is invalid to create of reducer in partially same mounting path (1)', () => {
const store = createStore(null, enhanceStore)
const Component = ({ children }) => <div>{children}</div>
Component.propTypes = {
children: PropTypes.element
}
const ExtendedComponent = createReducer({
mountPath: 'ui component main',
initialState: {}
})(Component)

const ExtendedComponent1 = createReducer({
mountPath: 'ui component',
initialState: {}
})(Component)

expect(renderer.create.bind(renderer,
<Provider store={store}>
<ExtendedComponent>
<ExtendedComponent1/>
</ExtendedComponent>
</Provider>
)).toThrowError('Mount path "ui component" already busy')
})

test('Test is invalid to create of reducer in partially same mounting path (2)', () => {
const store = createStore(null, enhanceStore)
const Component = ({ children }) => <div>{children}</div>
Component.propTypes = {
children: PropTypes.element
}
const ExtendedComponent = createReducer({
mountPath: 'ui component',
initialState: {}
})(Component)

const ExtendedComponent1 = createReducer({
mountPath: 'ui component main',
initialState: {}
})(Component)

expect(renderer.create.bind(renderer,
<Provider store={store}>
<ExtendedComponent>
<ExtendedComponent1/>
</ExtendedComponent>
</Provider>
)).toThrowError('Mount path "ui component main" already busy')
})

test('Test is valid to create of reducer after create of reducer', () => {
const store = createStore(null, enhanceStore)
const Component = ({ children }) => <div>{children || 'Last reducer'}</div>
Expand Down
46 changes: 46 additions & 0 deletions __tests__/registerReducers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,52 @@ test('Test is invalid to register of reducer in same mounting path', () => {
)).toThrowError('Mount path "ui component" already busy')
})

test('Test is invalid to register of reducer in partially same mounting path (1)', () => {
const store = createStore(null, enhanceStore)
const Component = ({ children }) => <div>{children}</div>
Component.propTypes = {
children: PropTypes.element
}
const ExtendedComponent = registerReducers({
'ui component main': () => ({})
})(Component)

const ExtendedComponent1 = registerReducers({
'ui component': () => ({})
})(Component)

expect(renderer.create.bind(renderer,
<Provider store={store}>
<ExtendedComponent>
<ExtendedComponent1/>
</ExtendedComponent>
</Provider>
)).toThrowError('Mount path "ui component" already busy')
})

test('Test is invalid to register of reducer in partially same mounting path (2)', () => {
const store = createStore(null, enhanceStore)
const Component = ({ children }) => <div>{children}</div>
Component.propTypes = {
children: PropTypes.element
}
const ExtendedComponent = registerReducers({
'ui component': () => ({})
})(Component)

const ExtendedComponent1 = registerReducers({
'ui component main': () => ({})
})(Component)

expect(renderer.create.bind(renderer,
<Provider store={store}>
<ExtendedComponent>
<ExtendedComponent1/>
</ExtendedComponent>
</Provider>
)).toThrowError('Mount path "ui component main" already busy')
})

test('Test is valid to register of reducer after register of reducer', () => {
const store = createStore(null, enhanceStore)
const Component = ({ children }) => <div>{children || 'Last reducer'}</div>
Expand Down
1 change: 1 addition & 0 deletions examples/nested_reused_components/src/components/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createReducer } from 'redux-fly'
type PropsType = { reduxState: Object, reduxSetState: Function }
const Modal = ({ reduxState: { clicks }, reduxSetState }: PropsType) => (
<div>
<br/>
<button onClick={() => reduxSetState('CLICK', (state) => ({ clicks: state.clicks + 1 }))}>
Clicks {clicks}
</button>
Expand Down
2 changes: 1 addition & 1 deletion examples/nested_reused_components/src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const style = {
container: (opened) => ({
display: opened ? 'block' : 'none',
position: 'absolute',
left: '100px',
left: 'calc(50% - 130px)',
top: '150px',
width: '200px',
height: '200px',
Expand Down
2 changes: 1 addition & 1 deletion examples/reused_components/src/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const style = {
container: (opened) => ({
display: opened ? 'block' : 'none',
position: 'absolute',
left: '100px',
left: 'calc(50% - 130px)',
top: '150px',
width: '200px',
height: '200px',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-fly",
"version": "0.0.1-rc.1",
"version": "0.0.1",
"description": "Simple redux",
"browser": "dist/redux-fly.js",
"main": "lib/index.js",
Expand Down
30 changes: 17 additions & 13 deletions src/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,28 @@ export default ({
static contextTypes = {
store: process.env.NODE_ENV === 'test' ? PropTypes.object : storeShape,
reduxMountPaths: PropTypes.arrayOf(PropTypes.string),
reduxMountPath: PropTypes.string
lastReduxMountPath: PropTypes.string
}

static childContextTypes = {
reduxMountPaths: PropTypes.arrayOf(PropTypes.string),
reduxMountPath: PropTypes.string
lastReduxMountPath: PropTypes.string
}

getChildContext = () => ({
reduxMountPaths: this.reduxMountPaths,
reduxMountPath: this.reduxMountPath
lastReduxMountPath: this.lastReduxMountPath
});

store: ?Object
reduxSetState: any
reduxSetState: ?Function
reduxResetState: any
ChildComponent: any
persist: any
persist: ?boolean
actionPrefix: any
reduxMountPaths: any
dispatch: any
reduxMountPath: any
dispatch: ?Function
lastReduxMountPath: ?string

props: {
reduxMountPath: string,
Expand All @@ -129,7 +129,7 @@ export default ({
super(props, context)
let { store } = context
this.reduxMountPaths = context.reduxMountPaths || []
this.reduxMountPath = context.reduxMountPath || ''
this.lastReduxMountPath = context.lastReduxMountPath || ''
this.store = null

let { reduxMountPath: propMountPath, reduxPersist: propPersist, reduxActionPrefix: propActionPrefix } = props
Expand All @@ -143,11 +143,15 @@ export default ({
}
const _mountPath = normalizeMountPath(`${propMountPath || ''} ${mountPath || ''}`)

if (this.reduxMountPaths.indexOf(_mountPath) !== -1) {

if (this.reduxMountPaths.some(path =>
((path.indexOf(_mountPath) === 0 && !((path.substr(_mountPath.length)[0] || '').trim())) ||
(_mountPath.indexOf(path) === 0 && !((_mountPath.substr(path.length)[0] || '').trim()))))
) {
throw new Error(`Mount path "${_mountPath}" already busy`)
}
if (this.reduxMountPath && _mountPath.indexOf(this.reduxMountPath) === -1) {
throw new Error(`Mount path "${_mountPath}" must be contain "${this.reduxMountPath}"`)
if (this.lastReduxMountPath && _mountPath.indexOf(this.lastReduxMountPath) === -1) {
throw new Error(`Mount path "${_mountPath}" must be contain "${this.lastReduxMountPath}"`)
}
this.reduxMountPaths.push(_mountPath)

Expand Down Expand Up @@ -195,7 +199,7 @@ export default ({
if (typeof store.registerReducers !== 'function') {
throw new Error('Redux store must be enhanced with redux-fly')
}
this.reduxMountPath = normalizeMountPath(propMountPath)
this.lastReduxMountPath = normalizeMountPath(propMountPath)
}

if (typeof store === 'undefined' || typeof store.registerReducers !== 'function') {
Expand Down Expand Up @@ -244,7 +248,7 @@ export default ({
this.persist = null
this.reduxMountPaths = null
this.dispatch = null
this.reduxMountPath = null
this.lastReduxMountPath = null
}

render() {
Expand Down
23 changes: 13 additions & 10 deletions src/registerReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ export default (
static contextTypes = {
store: process.env.NODE_ENV === 'test' ? PropTypes.object : storeShape,
reduxMountPaths: PropTypes.arrayOf(PropTypes.string),
reduxMountPath: PropTypes.string
lastReduxMountPath: PropTypes.string
}

static childContextTypes = {
reduxMountPaths: PropTypes.arrayOf(PropTypes.string),
reduxMountPath: PropTypes.string
lastReduxMountPath: PropTypes.string
}

getChildContext = () => ({
reduxMountPaths: this.reduxMountPaths,
reduxMountPath: this.reduxMountPath
lastReduxMountPath: this.lastReduxMountPath
});

store: ?Object
reduxMountPaths: any
reduxMountPath: any
lastReduxMountPath: ?string

constructor(props: any, context: any) {
super(props, context)
let { store } = context
this.reduxMountPaths = context.reduxMountPaths || []
this.reduxMountPath = context.reduxMountPath || ''
this.lastReduxMountPath = context.lastReduxMountPath || ''
this.store = null

const { reduxMountPath: propMountPath } = props
Expand All @@ -64,11 +64,14 @@ export default (
let _normReducers = {}
Object.keys(_reducers).forEach(key => {
const normalizedMountPath = normalizeMountPath(`${propMountPath || ''} ${key || ''}`)
if (this.reduxMountPaths.indexOf(normalizedMountPath) !== -1) {
if (this.reduxMountPaths.some(path =>
((path.indexOf(normalizedMountPath) === 0 && !((path.substr(normalizedMountPath.length)[0] || '').trim())) ||
(normalizedMountPath.indexOf(path) === 0 && !((normalizedMountPath.substr(path.length)[0] || '').trim()))))
) {
throw new Error(`Mount path "${normalizedMountPath}" already busy`)
}
if (this.reduxMountPath && normalizedMountPath.indexOf(this.reduxMountPath) === -1) {
throw new Error(`Mount path "${normalizedMountPath}" must be contain "${this.reduxMountPath}"`)
if (this.lastReduxMountPath && normalizedMountPath.indexOf(this.lastReduxMountPath) === -1) {
throw new Error(`Mount path "${normalizedMountPath}" must be contain "${this.lastReduxMountPath}"`)
}
this.reduxMountPaths.push(normalizedMountPath)
_normReducers[normalizedMountPath] = _reducers[key]
Expand All @@ -81,7 +84,7 @@ export default (
if (typeof store.registerReducers !== 'function') {
throw new Error('Redux store must be enhanced with redux-fly')
}
this.reduxMountPath = normalizeMountPath(propMountPath)
this.lastReduxMountPath = normalizeMountPath(propMountPath)
}

if (typeof store === 'undefined' || typeof store.registerReducers !== 'function') {
Expand All @@ -101,7 +104,7 @@ export default (
componentWillUnmount() {
this.store = null
this.reduxMountPaths = null
this.reduxMountPath = null
this.lastReduxMountPath = null
}

render() {
Expand Down

0 comments on commit 5d465cf

Please sign in to comment.