-
Notifications
You must be signed in to change notification settings - Fork 0
/
userAction.test.js
87 lines (75 loc) · 2.62 KB
/
userAction.test.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import axios from "axios";
import thunk from "redux-thunk";
import { configureStore } from "@reduxjs/toolkit";
import { registerUser, userLogin } from "./userAction";
import { registerUserReducer, loginUserReducer } from "../reducers/userReducer";
const middleware = [thunk];
jest.mock("axios", () => ({
post: jest.fn(() => Promise.resolve({ data: {} })),
}));
const createTestStore = (initialState) => {
return configureStore({
reducer: {
registerUserReducer: registerUserReducer,
loginUserReducer: loginUserReducer,
},
middleware,
devTools: true,
preloadedState: initialState,
});
};
describe("userActions.js", () => {
describe("registerUser:", () => {
const user = {
firstName: "John",
lastName: "Doe",
email: "john@example.com",
password: "password123",
};
const initialState = {
registerUserReducer: {},
};
it("USER_REGISTER_SUCCESS:", async () => {
axios.post.mockResolvedValueOnce({ data: user });
const store = createTestStore(initialState);
await store.dispatch(registerUser(user));
expect(store.getState().registerUserReducer.loading).toBe(false);
expect(store.getState().registerUserReducer.success).toBe(true);
});
it("USER_REGISTER_FAILED:", async () => {
const error = { message: "Error registering user" };
axios.post.mockRejectedValueOnce(error);
const store = createTestStore(initialState);
await store.dispatch(registerUser(user));
expect(store.getState().registerUserReducer.loading).toBe(false);
expect(store.getState().registerUserReducer.error).toEqual(error);
});
});
describe("loginUser:", () => {
const user = {
email: "john@example.com",
password: "password123",
};
const initialState = {
loginUserReducer: {},
};
it("USER_LOGIN_SUCCESS:", async () => {
axios.post.mockResolvedValueOnce({ data: { token: "jwt_token" } });
const store = createTestStore(initialState);
await store.dispatch(userLogin(user));
const state = store.getState().loginUserReducer;
expect(state.loading).toBe(false);
expect(state.success).toBe(true);
expect(state.currentUser).toEqual({ token: "jwt_token" });
});
it("USER_LOGIN_FAILED:", async () => {
const error = { message: "Invalid email or password" };
axios.post.mockRejectedValueOnce(error);
const store = createTestStore(initialState);
await store.dispatch(userLogin(user));
const state = store.getState().loginUserReducer;
expect(state.loading).toBe(false);
expect(state.error).toEqual(error);
});
});
});