Skip to content

Commit 82d5a48

Browse files
cellogtimdorr
authored andcommitted
migrate test helpers to typescript (reduxjs#3515)
1 parent a3c4c5e commit 82d5a48

File tree

5 files changed

+59
-23
lines changed

5 files changed

+59
-23
lines changed

test/helpers/actionCreators.js renamed to test/helpers/actionCreators.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import {
77
THROW_ERROR,
88
UNKNOWN_ACTION
99
} from './actionTypes'
10+
import { Action, AnyAction, Dispatch } from '../..'
1011

11-
export function addTodo(text) {
12+
export function addTodo(text: string): AnyAction {
1213
return { type: ADD_TODO, text }
1314
}
1415

15-
export function addTodoAsync(text) {
16-
return dispatch =>
16+
export function addTodoAsync(text: string) {
17+
return (dispatch: Dispatch): Promise<void> =>
1718
new Promise(resolve =>
1819
setImmediate(() => {
1920
dispatch(addTodo(text))
@@ -22,49 +23,49 @@ export function addTodoAsync(text) {
2223
)
2324
}
2425

25-
export function addTodoIfEmpty(text) {
26-
return (dispatch, getState) => {
26+
export function addTodoIfEmpty(text: string) {
27+
return (dispatch: Dispatch, getState: () => any) => {
2728
if (!getState().length) {
2829
dispatch(addTodo(text))
2930
}
3031
}
3132
}
3233

33-
export function dispatchInMiddle(boundDispatchFn) {
34+
export function dispatchInMiddle(boundDispatchFn: () => void): AnyAction {
3435
return {
3536
type: DISPATCH_IN_MIDDLE,
3637
boundDispatchFn
3738
}
3839
}
3940

40-
export function getStateInMiddle(boundGetStateFn) {
41+
export function getStateInMiddle(boundGetStateFn: () => void): AnyAction {
4142
return {
4243
type: GET_STATE_IN_MIDDLE,
4344
boundGetStateFn
4445
}
4546
}
4647

47-
export function subscribeInMiddle(boundSubscribeFn) {
48+
export function subscribeInMiddle(boundSubscribeFn: () => void): AnyAction {
4849
return {
4950
type: SUBSCRIBE_IN_MIDDLE,
5051
boundSubscribeFn
5152
}
5253
}
5354

54-
export function unsubscribeInMiddle(boundUnsubscribeFn) {
55+
export function unsubscribeInMiddle(boundUnsubscribeFn: () => void): AnyAction {
5556
return {
5657
type: UNSUBSCRIBE_IN_MIDDLE,
5758
boundUnsubscribeFn
5859
}
5960
}
6061

61-
export function throwError() {
62+
export function throwError(): Action {
6263
return {
6364
type: THROW_ERROR
6465
}
6566
}
6667

67-
export function unknownAction() {
68+
export function unknownAction(): Action {
6869
return {
6970
type: UNKNOWN_ACTION
7071
}
File renamed without changes.

test/helpers/middleware.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/helpers/middleware.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { MiddlewareAPI, Dispatch, AnyAction } from '../..'
2+
3+
type ThunkAction<T extends any = any> = T extends AnyAction
4+
? AnyAction
5+
: T extends Function
6+
? T
7+
: never
8+
9+
export function thunk({ dispatch, getState }: MiddlewareAPI) {
10+
return (next: Dispatch) => <T>(action: ThunkAction) =>
11+
typeof action === 'function' ? action(dispatch, getState) : next(action)
12+
}

test/helpers/reducers.js renamed to test/helpers/reducers.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ import {
66
UNSUBSCRIBE_IN_MIDDLE,
77
THROW_ERROR
88
} from './actionTypes'
9+
import { AnyAction } from '../..'
910

10-
function id(state = []) {
11+
function id(state: { id: number }[] = []) {
1112
return (
1213
state.reduce((result, item) => (item.id > result ? item.id : result), 0) + 1
1314
)
1415
}
1516

16-
export function todos(state = [], action) {
17+
export interface Todo {
18+
id: number
19+
text: string
20+
}
21+
export type TodoAction = { type: 'ADD_TODO'; text: string } | AnyAction
22+
23+
export function todos(state: Todo[] = [], action: TodoAction) {
1724
switch (action.type) {
1825
case ADD_TODO:
1926
return [
@@ -28,7 +35,7 @@ export function todos(state = [], action) {
2835
}
2936
}
3037

31-
export function todosReverse(state = [], action) {
38+
export function todosReverse(state: Todo[] = [], action: TodoAction) {
3239
switch (action.type) {
3340
case ADD_TODO:
3441
return [
@@ -43,7 +50,12 @@ export function todosReverse(state = [], action) {
4350
}
4451
}
4552

46-
export function dispatchInTheMiddleOfReducer(state = [], action) {
53+
export function dispatchInTheMiddleOfReducer(
54+
state = [],
55+
action:
56+
| { type: 'DISPATCH_IN_MIDDLE'; boundDispatchFn: () => void }
57+
| AnyAction
58+
) {
4759
switch (action.type) {
4860
case DISPATCH_IN_MIDDLE:
4961
action.boundDispatchFn()
@@ -53,7 +65,12 @@ export function dispatchInTheMiddleOfReducer(state = [], action) {
5365
}
5466
}
5567

56-
export function getStateInTheMiddleOfReducer(state = [], action) {
68+
export function getStateInTheMiddleOfReducer(
69+
state = [],
70+
action:
71+
| { type: 'DISPATCH_IN_MIDDLE'; boundGetStateFn: () => void }
72+
| AnyAction
73+
) {
5774
switch (action.type) {
5875
case GET_STATE_IN_MIDDLE:
5976
action.boundGetStateFn()
@@ -63,7 +80,12 @@ export function getStateInTheMiddleOfReducer(state = [], action) {
6380
}
6481
}
6582

66-
export function subscribeInTheMiddleOfReducer(state = [], action) {
83+
export function subscribeInTheMiddleOfReducer(
84+
state = [],
85+
action:
86+
| { type: 'DISPATCH_IN_MIDDLE'; boundSubscribeFn: () => void }
87+
| AnyAction
88+
) {
6789
switch (action.type) {
6890
case SUBSCRIBE_IN_MIDDLE:
6991
action.boundSubscribeFn()
@@ -73,7 +95,12 @@ export function subscribeInTheMiddleOfReducer(state = [], action) {
7395
}
7496
}
7597

76-
export function unsubscribeInTheMiddleOfReducer(state = [], action) {
98+
export function unsubscribeInTheMiddleOfReducer(
99+
state = [],
100+
action:
101+
| { type: 'DISPATCH_IN_MIDDLE'; boundUnsubscribeFn: () => void }
102+
| AnyAction
103+
) {
77104
switch (action.type) {
78105
case UNSUBSCRIBE_IN_MIDDLE:
79106
action.boundUnsubscribeFn()
@@ -83,7 +110,7 @@ export function unsubscribeInTheMiddleOfReducer(state = [], action) {
83110
}
84111
}
85112

86-
export function errorThrowingReducer(state = [], action) {
113+
export function errorThrowingReducer(state = [], action: AnyAction) {
87114
switch (action.type) {
88115
case THROW_ERROR:
89116
throw new Error()

0 commit comments

Comments
 (0)