Skip to content

Commit 531b93d

Browse files
author
Bernabe Gonzalez
committed
add tests for checkAuth function
1 parent c07f87e commit 531b93d

File tree

4 files changed

+105
-12
lines changed

4 files changed

+105
-12
lines changed

.babelrc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"env": {
77
"development": {
88
"presets": [
9-
"latest",
10-
"react-hmre"
9+
"latest"
1110
]
1211
},
1312
"production": {
@@ -17,10 +16,6 @@
1716
"modules": false
1817
}
1918
}],
20-
],
21-
"plugins": [
22-
"transform-react-constant-elements",
23-
"transform-react-remove-prop-types"
2419
]
2520
},
2621
"test": {

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@
3535
"babel-eslint": "7.1.1",
3636
"babel-jest": "18.0.0",
3737
"babel-loader": "6.3.0",
38-
"babel-plugin-transform-react-constant-elements": "6.23.0",
39-
"babel-plugin-transform-react-remove-prop-types": "0.3.2",
4038
"babel-polyfill": "6.23.0",
4139
"babel-preset-latest": "6.22.0",
4240
"babel-preset-react": "6.23.0",
43-
"babel-preset-react-hmre": "1.1.1",
4441
"babel-preset-stage-1": "6.22.0",
4542
"babel-register": "6.23.0",
4643
"coveralls": "2.11.16",

src/__tests__/index.spec.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,100 @@ describe('API functions', () => {
6060
});
6161
});
6262

63+
describe('checkAuth', () => {
64+
let nextState;
65+
let replace;
66+
let next;
67+
beforeEach(() => {
68+
nextState = { location: { pathname: 'test' } };
69+
replace = jest.fn();
70+
next = jest.fn();
71+
});
72+
73+
describe('with logged user', () => {
74+
beforeEach(() => {
75+
__setUser(user);
76+
__setSession(session);
77+
});
78+
79+
test('does call next function', (done) => {
80+
const next = jest.fn(() => {
81+
expect(next).toHaveBeenCalled();
82+
done();
83+
});
84+
sessionService.checkAuth(nextState, replace, next);
85+
});
86+
87+
describe('with option refreshOnCheckAuth enable', () => {
88+
test('change authenticated flag to true and save the user', (done) => {
89+
sessionService.setOptions(store, true);
90+
// wait for change the redux store
91+
const unsubscribe = store.subscribe(() => {
92+
const state = store.getState();
93+
expect(state.authenticated).toEqual(true);
94+
// wait to change the user
95+
const { user } = state;
96+
if (!(Object.keys(user).length === 0 && user.constructor === Object)) {
97+
expect(state.user).toMatchObject(user);
98+
unsubscribe();
99+
done();
100+
}
101+
});
102+
103+
sessionService.checkAuth(nextState, replace, next);
104+
});
105+
});
106+
});
107+
108+
describe('without logged user', () => {
109+
beforeEach(() => {
110+
__setUser(undefined);
111+
__setSession(undefined);
112+
sessionService.setOptions(store, false, 'redirectionPath');
113+
});
114+
115+
test('does call replace function', (done) => {
116+
const replace = jest.fn(() => {
117+
expect(replace).toHaveBeenCalled();
118+
done();
119+
});
120+
sessionService.checkAuth(nextState, replace, next);
121+
});
122+
123+
test('does redirect to the redirectPath', (done) => {
124+
const expectedArg = {
125+
pathname: 'redirectionPath',
126+
state: { nextPathname: nextState.location.pathname }
127+
};
128+
const replace = jest.fn(() => {
129+
expect(replace).toHaveBeenCalledWith(expectedArg);
130+
done();
131+
});
132+
sessionService.checkAuth(nextState, replace, next);
133+
});
134+
135+
describe('with option refreshOnCheckAuth enable', () => {
136+
test('change authenticated flag to false and the user to empty object', (done) => {
137+
sessionService.setOptions(store, true);
138+
// wait for change the redux store
139+
const unsubscribe = store.subscribe(() => {
140+
const state = store.getState();
141+
expect(state.authenticated).toEqual(false);
142+
// wait to empty the user
143+
const { user } = state;
144+
if ((Object.keys(user).length === 0 && user.constructor === Object)) {
145+
expect(state.user).toEqual({});
146+
unsubscribe();
147+
done();
148+
}
149+
});
150+
151+
sessionService.checkAuth(nextState, replace, next);
152+
});
153+
});
154+
});
155+
});
156+
63157
describe('saveSession', () => {
64158
describe('localforage returns success', () => {
65159
test('change authenticated flag to true value', (done) => {

src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ export class sessionService {
2222
return instance;
2323
}
2424

25+
static setOptions(store, refreshOnCheckAuth = false, redirectPath = 'login') {
26+
instance.store = store;
27+
instance.refreshOnCheckAuth = refreshOnCheckAuth;
28+
instance.redirectPath = redirectPath;
29+
}
30+
2531
static initSessionService(store, refreshOnCheckAuth = false, redirectPath = 'login') {
2632
instance = new sessionService(store, refreshOnCheckAuth, redirectPath);
2733
sessionService.refreshFromLocalStorage();
2834
}
2935

3036
static refreshFromLocalStorage() {
31-
sessionService.loadSession()
37+
return sessionService.loadSession()
3238
.then(() => {
3339
instance.store.dispatch(getSessionSuccess());
3440
sessionService.loadUser().then((user) => {
@@ -52,6 +58,7 @@ export class sessionService {
5258
})
5359
.catch(() => {
5460
refreshOnCheckAuth && store.dispatch(getSessionError());
61+
refreshOnCheckAuth && store.dispatch(getUserSessionError());
5562
replace({
5663
pathname: instance.redirectPath,
5764
state: { nextPathname: nextState.location.pathname }
@@ -84,7 +91,7 @@ export class sessionService {
8491
return removeItem(constant.USER_SESSION).then(() => {
8592
instance.store.dispatch(getSessionError());
8693
browserHistory.replace(instance.redirectPath);
87-
});
94+
}).catch(err => err);
8895
}
8996

9097
static saveUser(user) {
@@ -110,7 +117,7 @@ export class sessionService {
110117
static deleteUser() {
111118
return removeItem(constant.USER_DATA).then(() => {
112119
instance.store.dispatch(getUserSessionError());
113-
});
120+
}).catch(err => err);
114121
}
115122
}
116123

0 commit comments

Comments
 (0)