From c491b84f7aa7ebcc1c9b286f1aa085f29c79c1d6 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 14 Sep 2015 23:42:39 -0400 Subject: [PATCH 1/4] adds redux-actions --- redux-actions/redux-actions-tests.ts | 54 ++++++++++++++++++++++++++++ redux-actions/redux-actions.d.ts | 34 ++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 redux-actions/redux-actions-tests.ts create mode 100644 redux-actions/redux-actions.d.ts diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts new file mode 100644 index 0000000000..68d93ff7b2 --- /dev/null +++ b/redux-actions/redux-actions-tests.ts @@ -0,0 +1,54 @@ +/// + +const minimalAction: ReduxActions.Action = { type: 'INCREMENT' }; +const richerAction: ReduxActions.Action = { + type: 'INCREMENT', + payload: 2, + error: false, + meta: { + remote: true + } +}; + +const incrementAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction( + 'INCREMENT', + (amount: number) => amount +); +const action: ReduxActions.Action = incrementAction(42); + + const incrementByAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction( + 'INCREMENT_BY', + (amount: number) => amount, + amount => ({ remote: true }) +); + +const actionHandler = ReduxActions.handleAction( + 'INCREMENT', + (state: number, action: ReduxActions.Action) => state + 1 +); +actionHandler(0, { type: 'INCREMENT' }); + + +const actionHandlerWithReduceMap = ReduxActions.handleAction( + 'INCREMENT_BY', { + next(state: number, action: ReduxActions.Action) { + return state + action.payload; + }, + throw(state: number) { return state } + } +); +actionHandlerWithReduceMap(0, { type: 'INCREMENT' }); + +const actionsHandler = ReduxActions.handleActions({ + 'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1, + 'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1 +}); +actionsHandler(0, { type: 'INCREMENT' }); + +const actionsHandlerWithInitialState = ReduxActions.handleActions({ + 'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1, + 'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1 +}, 0); +actionsHandlerWithInitialState(0, { type: 'INCREMENT' }); + + diff --git a/redux-actions/redux-actions.d.ts b/redux-actions/redux-actions.d.ts new file mode 100644 index 0000000000..522f184625 --- /dev/null +++ b/redux-actions/redux-actions.d.ts @@ -0,0 +1,34 @@ +// Type definitions for redux-actions v0.8.0 +// Project: https://github.com/acdlite/redux-actions +// Definitions by: Jack Hsu +// Definitions: https://github.com/borisyankov/DefinitelyTyped +declare module ReduxActions { + // FSA-compliant action. + // See: https://github.com/acdlite/flux-standard-action + type Action = { + type: string + payload?: any + error?: boolean + meta?: any + }; + + type PayloadCreator = (...args: any[]) => T; + type MetaCreator = (...args: any[]) => any; + + type Reducer = (state: T, action: Action) => T; + + type ReducerMap = { + [actionType: string]: Reducer + }; + + export function createAction(actionType: string, payloadCreator?: PayloadCreator, metaCreator?: MetaCreator): (...args: any[]) => Action; + + export function handleAction(actionType: string, reducer: Reducer | ReducerMap): Reducer; + + export function handleActions(reducerMap: ReducerMap, initialState?: T): Reducer; +} + +declare module 'redux-actions' { + export = ReduxActions; +} + From 9e5f06b0f2cafccd67356a0f88c819cefc26c5c8 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 15 Sep 2015 01:26:36 -0400 Subject: [PATCH 2/4] adds test for return type of actionHandler/reducer --- redux-actions/redux-actions-tests.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 68d93ff7b2..53305a2c9d 100644 --- a/redux-actions/redux-actions-tests.ts +++ b/redux-actions/redux-actions-tests.ts @@ -22,11 +22,13 @@ const action: ReduxActions.Action = incrementAction(42); amount => ({ remote: true }) ); +let state: number; + const actionHandler = ReduxActions.handleAction( 'INCREMENT', (state: number, action: ReduxActions.Action) => state + 1 ); -actionHandler(0, { type: 'INCREMENT' }); +state = actionHandler(0, { type: 'INCREMENT' }); const actionHandlerWithReduceMap = ReduxActions.handleAction( @@ -37,18 +39,18 @@ const actionHandlerWithReduceMap = ReduxActions.handleAction( throw(state: number) { return state } } ); -actionHandlerWithReduceMap(0, { type: 'INCREMENT' }); +state = actionHandlerWithReduceMap(0, { type: 'INCREMENT' }); const actionsHandler = ReduxActions.handleActions({ 'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1, 'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1 }); -actionsHandler(0, { type: 'INCREMENT' }); +state = actionsHandler(0, { type: 'INCREMENT' }); const actionsHandlerWithInitialState = ReduxActions.handleActions({ 'INCREMENT': (state: number, action: ReduxActions.Action) => state + 1, 'DECREMENT': (state: number, action: ReduxActions.Action) => state - 1 }, 0); -actionsHandlerWithInitialState(0, { type: 'INCREMENT' }); +state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' }); From 00fee00fb40152310d1b205f4f8eed00d81ee983 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 15 Sep 2015 01:31:51 -0400 Subject: [PATCH 3/4] adds missing generic type for handleAction test --- redux-actions/redux-actions-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 53305a2c9d..918bfcf175 100644 --- a/redux-actions/redux-actions-tests.ts +++ b/redux-actions/redux-actions-tests.ts @@ -24,14 +24,14 @@ const action: ReduxActions.Action = incrementAction(42); let state: number; -const actionHandler = ReduxActions.handleAction( +const actionHandler = ReduxActions.handleAction( 'INCREMENT', (state: number, action: ReduxActions.Action) => state + 1 ); state = actionHandler(0, { type: 'INCREMENT' }); -const actionHandlerWithReduceMap = ReduxActions.handleAction( +const actionHandlerWithReduceMap = ReduxActions.handleAction( 'INCREMENT_BY', { next(state: number, action: ReduxActions.Action) { return state + action.payload; From f06106d28c1fa5faf0c7395d1ac3f308720729af Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 15 Sep 2015 02:03:30 -0400 Subject: [PATCH 4/4] fix indentation --- redux-actions/redux-actions-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redux-actions/redux-actions-tests.ts b/redux-actions/redux-actions-tests.ts index 918bfcf175..30dd00a970 100644 --- a/redux-actions/redux-actions-tests.ts +++ b/redux-actions/redux-actions-tests.ts @@ -16,7 +16,7 @@ const incrementAction: (...args: any[]) => ReduxActions.Action = ReduxActions.cr ); const action: ReduxActions.Action = incrementAction(42); - const incrementByAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction( +const incrementByAction: (...args: any[]) => ReduxActions.Action = ReduxActions.createAction( 'INCREMENT_BY', (amount: number) => amount, amount => ({ remote: true }) @@ -30,7 +30,6 @@ const actionHandler = ReduxActions.handleAction( ); state = actionHandler(0, { type: 'INCREMENT' }); - const actionHandlerWithReduceMap = ReduxActions.handleAction( 'INCREMENT_BY', { next(state: number, action: ReduxActions.Action) { @@ -54,3 +53,4 @@ const actionsHandlerWithInitialState = ReduxActions.handleActions({ state = actionsHandlerWithInitialState(0, { type: 'INCREMENT' }); +