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

migrate test helpers to typescript #3515

Merged
merged 1 commit into from
Aug 18, 2019
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
23 changes: 12 additions & 11 deletions test/helpers/actionCreators.js → test/helpers/actionCreators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
THROW_ERROR,
UNKNOWN_ACTION
} from './actionTypes'
import { Action, AnyAction, Dispatch } from '../..'

export function addTodo(text) {
export function addTodo(text: string): AnyAction {
return { type: ADD_TODO, text }
}

export function addTodoAsync(text) {
return dispatch =>
export function addTodoAsync(text: string) {
return (dispatch: Dispatch): Promise<void> =>
new Promise(resolve =>
setImmediate(() => {
dispatch(addTodo(text))
Expand All @@ -22,49 +23,49 @@ export function addTodoAsync(text) {
)
}

export function addTodoIfEmpty(text) {
return (dispatch, getState) => {
export function addTodoIfEmpty(text: string) {
return (dispatch: Dispatch, getState: () => any) => {
if (!getState().length) {
dispatch(addTodo(text))
}
}
}

export function dispatchInMiddle(boundDispatchFn) {
export function dispatchInMiddle(boundDispatchFn: () => void): AnyAction {
return {
type: DISPATCH_IN_MIDDLE,
boundDispatchFn
}
}

export function getStateInMiddle(boundGetStateFn) {
export function getStateInMiddle(boundGetStateFn: () => void): AnyAction {
return {
type: GET_STATE_IN_MIDDLE,
boundGetStateFn
}
}

export function subscribeInMiddle(boundSubscribeFn) {
export function subscribeInMiddle(boundSubscribeFn: () => void): AnyAction {
return {
type: SUBSCRIBE_IN_MIDDLE,
boundSubscribeFn
}
}

export function unsubscribeInMiddle(boundUnsubscribeFn) {
export function unsubscribeInMiddle(boundUnsubscribeFn: () => void): AnyAction {
return {
type: UNSUBSCRIBE_IN_MIDDLE,
boundUnsubscribeFn
}
}

export function throwError() {
export function throwError(): Action {
return {
type: THROW_ERROR
}
}

export function unknownAction() {
export function unknownAction(): Action {
return {
type: UNKNOWN_ACTION
}
Expand Down
File renamed without changes.
4 changes: 0 additions & 4 deletions test/helpers/middleware.js

This file was deleted.

12 changes: 12 additions & 0 deletions test/helpers/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MiddlewareAPI, Dispatch, AnyAction } from '../..'

type ThunkAction<T extends any = any> = T extends AnyAction
? AnyAction
: T extends Function
? T
: never

export function thunk({ dispatch, getState }: MiddlewareAPI) {
return (next: Dispatch) => <T>(action: ThunkAction) =>
typeof action === 'function' ? action(dispatch, getState) : next(action)
}
43 changes: 35 additions & 8 deletions test/helpers/reducers.js → test/helpers/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import {
UNSUBSCRIBE_IN_MIDDLE,
THROW_ERROR
} from './actionTypes'
import { AnyAction } from '../..'

function id(state = []) {
function id(state: { id: number }[] = []) {
return (
state.reduce((result, item) => (item.id > result ? item.id : result), 0) + 1
)
}

export function todos(state = [], action) {
export interface Todo {
id: number
text: string
}
export type TodoAction = { type: 'ADD_TODO'; text: string } | AnyAction

export function todos(state: Todo[] = [], action: TodoAction) {
switch (action.type) {
case ADD_TODO:
return [
Expand All @@ -28,7 +35,7 @@ export function todos(state = [], action) {
}
}

export function todosReverse(state = [], action) {
export function todosReverse(state: Todo[] = [], action: TodoAction) {
switch (action.type) {
case ADD_TODO:
return [
Expand All @@ -43,7 +50,12 @@ export function todosReverse(state = [], action) {
}
}

export function dispatchInTheMiddleOfReducer(state = [], action) {
export function dispatchInTheMiddleOfReducer(
state = [],
action:
| { type: 'DISPATCH_IN_MIDDLE'; boundDispatchFn: () => void }
| AnyAction
) {
switch (action.type) {
case DISPATCH_IN_MIDDLE:
action.boundDispatchFn()
Expand All @@ -53,7 +65,12 @@ export function dispatchInTheMiddleOfReducer(state = [], action) {
}
}

export function getStateInTheMiddleOfReducer(state = [], action) {
export function getStateInTheMiddleOfReducer(
state = [],
action:
| { type: 'DISPATCH_IN_MIDDLE'; boundGetStateFn: () => void }
| AnyAction
) {
switch (action.type) {
case GET_STATE_IN_MIDDLE:
action.boundGetStateFn()
Expand All @@ -63,7 +80,12 @@ export function getStateInTheMiddleOfReducer(state = [], action) {
}
}

export function subscribeInTheMiddleOfReducer(state = [], action) {
export function subscribeInTheMiddleOfReducer(
state = [],
action:
| { type: 'DISPATCH_IN_MIDDLE'; boundSubscribeFn: () => void }
| AnyAction
) {
switch (action.type) {
case SUBSCRIBE_IN_MIDDLE:
action.boundSubscribeFn()
Expand All @@ -73,7 +95,12 @@ export function subscribeInTheMiddleOfReducer(state = [], action) {
}
}

export function unsubscribeInTheMiddleOfReducer(state = [], action) {
export function unsubscribeInTheMiddleOfReducer(
state = [],
action:
| { type: 'DISPATCH_IN_MIDDLE'; boundUnsubscribeFn: () => void }
| AnyAction
) {
switch (action.type) {
case UNSUBSCRIBE_IN_MIDDLE:
action.boundUnsubscribeFn()
Expand All @@ -83,7 +110,7 @@ export function unsubscribeInTheMiddleOfReducer(state = [], action) {
}
}

export function errorThrowingReducer(state = [], action) {
export function errorThrowingReducer(state = [], action: AnyAction) {
switch (action.type) {
case THROW_ERROR:
throw new Error()
Expand Down