/
authActions.spec.js
156 lines (141 loc) · 5.2 KB
/
authActions.spec.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import moxios from 'moxios';
import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import mockData from '../__mocks__/mockData';
import mockLocalStorage from '../__mocks__/mockLocalStorage';
import { login } from '../../actions/authActions/login';
import logout from '../../actions/authActions/logout';
import { signUp } from '../../actions/authActions/signup';
import requestResetPassword from '../../actions/authActions/requestResetPassword';
import resetPassword from '../../actions/authActions/resetPassword';
import actionTypes from '../../actions/actionTypes';
import notify from '../__mocks__/notify';
import { request } from 'https';
const middleware = [thunk];
const mockStore = configureMockStore(middleware);
window.localStorage = mockLocalStorage;
describe('Auth Actions', () => {
beforeEach(() => moxios.install());
afterEach(() => moxios.uninstall());
describe('login', () => {
it('creates LOGIN, AUTH_LOADING and SET_LOGIN_STATUS' +
' when login is successful', () => {
const { authResponse } = mockData;
moxios.stubRequest('/api/v1/users/signin', {
status: 200,
response: authResponse
});
const expectedActions = [
{ type: actionTypes.AUTH_LOADING, state: true },
{ type: actionTypes.LOGIN, user: authResponse },
{ type: actionTypes.SET_LOGIN_STATUS, isLoggedIn: true },
{ type: actionTypes.AUTH_LOADING, state: false }
];
const store = mockStore({});
return store.dispatch(login({})).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it('creates AUTH_LOADING and SET_LOGIN_STATUS on login failure', () => {
const { authResponse } = mockData;
moxios.stubRequest('/api/v1/users/signin', {
status: 400,
response: authResponse
});
const expectedActions = [
{ type: actionTypes.AUTH_LOADING, state: true },
{ type: actionTypes.AUTH_LOADING, state: false }
];
const store = mockStore({});
return store.dispatch(login({})).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});
describe('signup', () => {
it('creates LOGIN, AUTH_LOADING and SET_LOGIN_STATUS' +
' when sign up is successful', () => {
const { authResponse } = mockData;
moxios.stubRequest('/api/v1/users/signup', {
status: 200,
response: authResponse
});
const expectedActions = [
{ type: actionTypes.AUTH_LOADING, state: true },
{ type: actionTypes.SIGN_UP, user: authResponse },
{ type: actionTypes.SET_LOGIN_STATUS, isLoggedIn: true },
{ type: actionTypes.AUTH_LOADING, state: false }
];
const store = mockStore({});
return store.dispatch(signUp({})).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it('creates AUTH_LOADING on signup failure', () => {
const { authResponse } = mockData;
moxios.stubRequest('/api/v1/users/signup', {
status: 400,
response: authResponse
});
const expectedActions = [
{ type: actionTypes.AUTH_LOADING, state: true },
{ type: actionTypes.AUTH_LOADING, state: false }
];
const store = mockStore({});
return store.dispatch(signUp({})).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});
describe('logout', () => {
it('creates LOGOUT when logout action is successful', (done) => {
const store = mockStore({});
const expectedActions = [{ type: actionTypes.LOGOUT }];
store.dispatch(logout());
expect(store.getActions()).toEqual(expectedActions);
done();
});
});
describe('requestResetPassword', () => {
it('provides a notification on success', () => {
moxios.stubRequest('/api/v1/users/forgot-password', {
status: 200,
response: mockData.authResponse
});
expect(notify.success).toHaveBeenCalled();
});
it("provides a notification on success", () => {
window.requestResetPassword = () => () => Promise.resolve(1);
requestResetPassword("password", "token");
expect(notify.success).toHaveBeenCalled();
});
it('provides a notification on failure', () => {
moxios.stubRequest('/api/v1/users/forgot-password', {
status: 500,
response: mockData.authResponse
});
expect(notify.error).toHaveBeenCalled();
});
});
describe('resetPassword', () => {
it('provides a notification on success', () => {
moxios.stubRequest('/api/v1/users/reset-password/1234yyjhkopi123', {
status: 200,
response: mockData.authResponse
});
expect(notify.success).toHaveBeenCalled();
});
it('provides a notification on success', () => {
window.resetPassword = () => () => Promise.resolve(1);
resetPassword('password', 'token');
expect(notify.success).toHaveBeenCalled();
});
it('provides a notification on failure', () => {
moxios.stubRequest('/api/v1/users/reset-password/1234yyjhkopi123', {
status: 500,
response: mockData.authResponse
});
expect(notify.error).toHaveBeenCalled();
});
});
});