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

Add optional name param in createCirquitAction #2

Merged
merged 4 commits into from
Mar 30, 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ store.dispatch(increment(1)); // state is { counter: { count: 1 } }

[Full example](https://github.com/airtoxin/redux-cirquit-example)

## API

### export createCirquitReducer<State>(initialState: State): Redux.Reducer<State>

Creates redux-cirquit's reducer that manages your application's state.

### export createCirquitAction<State>(reducer: State => State, name?: string): Redux.Action

Creates basic redux action to reduce you application's state.
If this invoked with optional `name` argument or
reducer in arguments has [function name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name),
this function returns redux action that has its name parameter. Otherwise action.name set "anonymous".

## License

MIT
38 changes: 36 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,42 @@ describe("createCirquitAction", () => {
});
});

it("should return anonymous named cirquitAction", () => {
expect(cirquit.createCirquitAction(state => state).name).toBe("anonymous");
describe("cirquitAction name", () => {
it("should be anonymous when reducer is arrow function", () => {
expect(cirquit.createCirquitAction(state => state).name).toBe(
"anonymous"
);
});

it("should be anonymous when reducer is anonymous function", () => {
expect(
cirquit.createCirquitAction(function(state: State) {
return state;
}).name
).toBe("anonymous");
});

it("should named by inferred arrow function", () => {
const namedReducer = (state: State) => state;
expect(cirquit.createCirquitAction(namedReducer).name).toBe(
"namedReducer"
);
});

it("should named by named function", () => {
expect(
cirquit.createCirquitAction(function namedReducer(state: State) {
return state;
}).name
).toBe("namedReducer");
});

it("should named when invoked with name argument", () => {
const namedReducer = (state: State) => state;
expect(cirquit.createCirquitAction(namedReducer, "nameParams").name).toBe(
"nameParams"
);
});
});
});

Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export interface CirquitAction<State> extends Action {
}

export const createCirquitAction = <State>(
reducer: CirquitReducer<State>
reducer: CirquitReducer<State>,
name?: string
): CirquitAction<State> => ({
type: CirquitActionType,
name: reducer.name || "anonymous",
name: name || reducer.name || "anonymous",
reducer
});

Expand Down