Skip to content

Commit

Permalink
change action name
Browse files Browse the repository at this point in the history
  • Loading branch information
lake2 committed Jun 20, 2020
1 parent f91693d commit 86ac94a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/createAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export function createAction<S, R>(params: Imdux.CreateActionParams<S>): Imdux.A
this.dispatch = {};
this.reducers = {};
this.reducer = (state: any, action: any) => {
const split: Array<string> = action.type.split(".");
const split: Array<string> = action.type.split("/");
const name = split[0];
const type = split[1];
const type = split.slice(1).join("/");
if (state === undefined) {
return params.initialState;
} else if (name !== this.name) {
return state;
} else if (split.length === 2 && this.reducers[type]) {
} else if (split.length >= 2 && this.reducers[type]) {
return this.reducers[type](state, action.payload);
} else {
return state;
Expand All @@ -42,7 +42,7 @@ export function createAction<S, R>(params: Imdux.CreateActionParams<S>): Imdux.A
if (!isPlainObject(payload) && !isPrimitiveType(payload) && !Array.isArray(payload)) {
this.options.payloadNotValidWarn && console.warn(payloadNotValid);
}
this.redux.dispatch({ type: `${this.name}.${name}`, payload });
this.redux.dispatch({ type: `${this.name}/${name}`, payload });
}
};
});
Expand Down
31 changes: 31 additions & 0 deletions src/test/action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createAction, createStore } from "../";
// import { Imdux } from "../types";

describe("change action name", () => {
type State = typeof initialState;
type Reducers = typeof reducers;

const initialState = {
bracket: false,
};

const reducers = {
["bracket/set"](draft: State) {
draft.bracket = true;
},
};

it("createStore", () => {
let flag: number;

const home = createAction<State, Reducers>({ initialState, reducers });
const actions = { home };
const { Dispatch, Query, redux } = createStore(actions);

flag = 0;
redux.subscribe(() => flag = 1);
Dispatch.home["bracket/set"]();
expect(Query.home.bracket).toBe(true);
expect(flag).toBe(1);
});
});

0 comments on commit 86ac94a

Please sign in to comment.