Skip to content

Commit

Permalink
Merge branch 'uitesting' of git@github.com:codemeasandwich/redux-auto…
Browse files Browse the repository at this point in the history
….git into uitesting

# Conflicts:
#	example/ui/index.jsx
  • Loading branch information
Brian committed Jun 16, 2017
2 parents 7f61964 + 62f2143 commit 8a26848
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 33 deletions.
24 changes: 12 additions & 12 deletions example/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
],
"env": {
"test": {
"presets": ["es2015","react"]
"presets": ["es2015","react"],
"plugins": [
[
"module-resolver",
{
"alias": {
"redux-auto":"./../index.js"
}
}
]
]
}
},
"plugins": [
[
"module-resolver",
{
"alias": {
"redux-auto":"./../index.js"
}
}
]
]
}
}
4 changes: 3 additions & 1 deletion example/store/posts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export function pending (posts, payload){
export function fulfilled (posts, payload, serverPosts){
console.log("posts / init / fulfilled")
return serverPosts
}
} fulfilled.chain = ()=> console.log(" Changing actions togetter is easy ")


export function rejected (posts, payload, error){
console.log("posts / init / rejected")
return posts;
Expand Down
3 changes: 2 additions & 1 deletion example/ui/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const MainUi = (props) => {
)
}

const mapStateToProps = ({ user,posts }) => {
const mapStateToProps = ( { user,posts }) => {
//throw new Error(JSON.stringify(x))
return { user,posts }
};

Expand Down
42 changes: 23 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ const reducersAfterCombineReducersErrorMessage = "You need to pass an object of
let autoWasCalled = false, reducers = { auto_function_not_call_before_combineReducers: ()=>{throw new Error(reducersBeforeAutoErrorMessage)} }

function chaining(actionType){
if(undefined === chaining[actionType])
return
if("function" === typeof chaining[actionType]){debugger;chaining[actionType]()}

else
throw new Error(`Chaining function used with ${actionType} was not a function. ${typeof chaining[actionType]}`)
// console.log(actionType, typeof chaining[actionType])
//if(undefined === chaining[actionType])
// return;
if("function" === typeof chaining[actionType])
chaining[actionType]();
// else throw new Error(`Chaining function used with ${actionType} was not a function. ${typeof chaining[actionType]}`)
}


Expand Down Expand Up @@ -55,11 +55,11 @@ function mergeReducers(otherReducers){
// get reducer name
const reducerName = key.match(/(.*)[\/\\]/)[1].substring(2);//||null;

if(actionName.includes(".")) throw new Error(`file ${actionName} in ${reducerName} contains a DOT in its name`)
if(reducerName.includes(".")) throw new Error(`the folder ${reducerName} contains a DOT in its name`)
//if(actionName.includes(".")) throw new Error(`file ${actionName} in ${reducerName} contains a DOT in its name`)
//if(reducerName.includes(".")) throw new Error(`the folder ${reducerName} contains a DOT in its name`)

// get action name starts with _ skip it
if(actionName.startsWith("_") || null === reducerName)
if(actionName.startsWith("_") || null === reducerName || "index" === actionName)
return;


Expand All @@ -80,8 +80,10 @@ function mergeReducers(otherReducers){
// get reducer name
const reducerName = key.match(/(.*)[\/\\]/)[1].substring(2);//||null;

if(actionName.includes(".")) throw new Error(`file ${actionName} in ${reducerName} contains a DOT in its name`)
if(reducerName.includes(".")) throw new Error(`the folder ${reducerName} contains a DOT in its name`)
if(actionName.includes("."))
throw new Error(`file ${actionName} in ${reducerName} contains a DOT in its name`)
if(reducerName.includes("."))
throw new Error(`the folder ${reducerName} contains a DOT in its name`)

// get action name starts with _ skip it
if(actionName.startsWith("_") || null === reducerName)
Expand Down Expand Up @@ -149,7 +151,7 @@ function mergeReducers(otherReducers){
// if( stage === "pending" || stage === "fulfilled" || stage === "rejected" ){
if ("function" === typeof lookup[reducerName][avabileAction][stage]) {
newState = lookup[reducerName][avabileAction][stage](data, action.reqPayload, payload);
if("function" === typeof lookup[reducerName][avabileAction][stage].chain){ debugger;
if("function" === typeof lookup[reducerName][avabileAction][stage].chain){
chaining[action.type] = lookup[reducerName][avabileAction][stage].chain
}
} else {
Expand All @@ -166,8 +168,9 @@ function mergeReducers(otherReducers){
}
} else {
newState = lookup[reducerName][avabileAction](data, payload);

if("function" === typeof lookup[reducerName][avabileAction].chain){
chaining.after[action.type] = lookup[reducerName][avabileAction].chain
chaining[action.type] = lookup[reducerName][avabileAction].chain
}
}
} else {// if("index" in lookup[reducerName]){
Expand All @@ -182,12 +185,13 @@ function mergeReducers(otherReducers){

if(newAsyncVal && "object" === typeof newState ){
// I am a redux-auto proto
let _p_ = newState.__proto__;
if(newState.__proto__.hasOwnProperty("async")){
_p_ = newState.__proto__.__proto__;
}
const _newProto_ = {async}
_newProto_.__proto__ = _p_

if(newState.__proto__.hasOwnProperty("async"))
_newProto_.__proto__ = newState.__proto__.__proto__;
else
_newProto_.__proto__ = newState.__proto__;

newState.__proto__ = _newProto_;
}
return newState
Expand All @@ -199,7 +203,7 @@ function mergeReducers(otherReducers){
if(actionName !== "index"){

const actionPreProcessor = modules(key).action;
actionsBuilder[reducerName] = actionsBuilder[reducerName] || {};
// actionsBuilder[reducerName] = actionsBuilder[reducerName] || {};

actionsBuilder[reducerName][actionName] = (payload,getState) => {

Expand Down
109 changes: 109 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,115 @@ describe('action middlware', () => {
actions[propName][actionName]();
})

//++++++++++ should chain actions for default function
//++++++++++++++++++++++++++++++++++++++++++++++++++++

it('should chain actions for default function', (done) => {


webpackModules.set(propName,"index","default",(data={})=> data )

const actionFunction = data => data;
actionFunction.chain = done;

webpackModules.set(propName,actionName,"default",actionFunction)
RefrashStore();
actions[propName][actionName]();
})

//++++++++++ should chain actions for PENDING function
//++++++++++++++++++++++++++++++++++++++++++++++++++++

it('should chain actions for PENDING function', (done) => {

const actionFunction = function (data, payload){ return data }
actionFunction.chain = done;

webpackModules.set(propName,"index","default",(data={})=> data )
webpackModules.set(propName,actionName,"default",(data)=>data)

webpackModules.set(propName,actionName,"PENDING",actionFunction)

webpackModules.set(propName,actionName,"action",()=> Promise.resolve() )

RefrashStore();
actions[propName][actionName]();

})

//++++++++++ should chain actions for fulfilled function
//++++++++++++++++++++++++++++++++++++++++++++++++++++

it('should chain actions for fulfilled function', (done) => {

const actionFunction = data => data;
actionFunction.chain = done;

webpackModules.set(propName,"index","default",(data={})=> data )
webpackModules.set(propName,actionName,"default", data => data )

webpackModules.set(propName,actionName,"fulfilled",actionFunction)

webpackModules.set(propName,actionName,"action",()=> Promise.resolve() )

RefrashStore();
actions[propName][actionName]();

})

//+++++++++ should chain actions for rejected function
//++++++++++++++++++++++++++++++++++++++++++++++++++++

it('should chain actions for rejected function', (done) => {

const actionFunction = data => data;
actionFunction.chain = done;

webpackModules.set(propName,"index","default",(data={})=> data )
webpackModules.set(propName,actionName,"default", data => data )

webpackModules.set(propName,actionName,"rejected",actionFunction)

webpackModules.set(propName,actionName,"action",()=> Promise.reject({}) )

RefrashStore();
actions[propName][actionName]();

})

//++++ should chain actions for async default function
//++++++++++++++++++++++++++++++++++++++++++++++++++++

it('should chain actions for async default function', (done) => {

const actionFunction = data => data;
actionFunction.chain = done;

webpackModules.set(propName,"index","default",(data={})=> data )
webpackModules.set(propName,actionName,"default",actionFunction)
webpackModules.set(propName,actionName,"action",()=> Promise.resolve() )

RefrashStore();
actions[propName][actionName]();

})

//++ should throw if chained actions is not a function
//++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
it('should throw if chained actions is not a function', () => {
const actionFunction = data => data
actionFunction.chain = "";
webpackModules.set(propName,"index","default",(data={})=> data )
webpackModules.set(propName,actionName,"default", actionFunction )
RefrashStore();
expect(actions[propName][actionName]).toThrow(new RegExp(`Chaining function used with ${actions[propName][actionName]} was not a function. string`));
})
*/
})


Expand Down

0 comments on commit 8a26848

Please sign in to comment.