forked from chrismaltby/gb-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredux-utils.ts
More file actions
38 lines (32 loc) · 868 Bytes
/
redux-utils.ts
File metadata and controls
38 lines (32 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { AppDispatch, RootState } from "store/configureStore";
import { Action } from "redux";
interface Store {
getState: jest.Mock;
dispatch: jest.Mock;
}
export const create = (initialState: RootState) => {
const store: Store = {
getState: jest.fn(() => initialState),
dispatch: jest.fn(),
};
const next: AppDispatch = jest.fn();
const invoke = (
action:
| Action
| ((dispatch: AppDispatch, getState: () => RootState) => void),
) => thunkMiddleware(store)(next)(action);
return { store, next, invoke };
};
const thunkMiddleware =
({ dispatch, getState }: Store) =>
(next: AppDispatch) =>
(
action:
| Action
| ((dispatch: AppDispatch, getState: () => RootState) => void),
) => {
if (typeof action === "function") {
return action(dispatch, getState);
}
return next(action);
};