Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit ac9db37

Browse files
author
Alex T and Rodolfo S
committed
Added safe-mock implementation, but IntelliJ does not accept the mock types.
1 parent 5f6a026 commit ac9db37

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"nock": "^9.0.13",
3939
"react-hot-loader": "1.3.1",
4040
"react-test-renderer": "^15.5.4",
41+
"safe-mock": "0.2.10",
4142
"source-map-loader": "^0.2.1",
4243
"stylelint-webpack-plugin": "0.8.0",
4344
"typescript": "^2.4.1",

src/main/actions/__test__/actions.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
jest.mock("../../api/Api");
1+
import {SafeMock, verify} from "safe-mock";
22
import {ThunkAction} from "redux-thunk";
3-
import {decrementAction, incrementAction, CounterEnum, getGreetingAction, CounterType} from "../actions";
3+
import {CounterAction, CounterEnum, CounterType, decrementAction, getGreetingAction, incrementAction} from "../actions";
44
import {Api, Greeting} from "../../api/Api";
5+
import {Dispatch} from "redux";
6+
7+
jest.mock("../../api/Api");
58
import getGreeting = Api.getGreeting;
69

710
describe("incrementAction", () => {
@@ -25,17 +28,18 @@ describe("decrementAction", () => {
2528

2629
describe("fetch greeting", () => {
2730
it("gets the greeting from the api", async () => {
28-
const greetingThunk: ThunkAction<void, CounterType, any> = getGreetingAction();
2931
let greeting = new Greeting("hello");
3032

3133
(getGreeting as any)
3234
.mockReturnValue(Promise.resolve(greeting));
33-
const dispatch = jest.fn();
3435

35-
await greetingThunk(dispatch, jest.fn(), null);
36+
let mockDispatchCounter = SafeMock.mockFunction<Dispatch<CounterType>>();
37+
let mockCounterAction = SafeMock.mockFunction<CounterAction>();
3638

37-
expect(dispatch)
38-
.toHaveBeenCalledWith({type: "RECEIVE_GREETING", payload: greeting});
39+
await getGreetingAction()(mockDispatchCounter, mockCounterAction, null);
40+
41+
verify(mockDispatchCounter)
42+
.calledWith({type: CounterEnum.RECEIVE_GREETING, payload: greeting});
3943
expect(getGreeting).toHaveBeenCalled()
4044
});
4145

src/main/actions/actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ const greetingReceivedAction: CounterAction = (greeting: Greeting) => {
2727
return {type: CounterEnum.RECEIVE_GREETING, payload: greeting}
2828
};
2929

30-
export const getGreetingAction: () => ThunkAction<Promise<void>, CounterType, null>
31-
= (): ThunkAction<Promise<void>, CounterType, null> => {
30+
export const getGreetingAction: () => ThunkAction<Promise<void>, CounterType, any>
31+
= (): ThunkAction<Promise<void>, CounterType, any> => {
3232
return (dispatch: Dispatch<CounterType>) => {
3333
return Api.getGreeting()
3434
.then((greeting: Greeting) => {
@@ -39,7 +39,7 @@ export const getGreetingAction: () => ThunkAction<Promise<void>, CounterType, nu
3939
new Greeting("Create a greeting endpoint " +
4040
"for a custom greeting"))
4141
);
42-
})
42+
});
4343
}
4444
};
4545

0 commit comments

Comments
 (0)