Skip to content

Commit

Permalink
logout user
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Nov 14, 2016
1 parent 9602086 commit 7c29426
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ const initialState = {

},
},
accessToken: localStorage.getItem(ACCESS_TOKEN),
refreshToken: localStorage.getItem(REFRESH_TOKEN),
user: null,
};

Expand Down Expand Up @@ -221,6 +219,11 @@ const Accounts = {

return toReturn;
},
async logout() {
const result = await this.client.logout();
this.setUser(null);
return result;
},
validateLogin({ user, password }) {
if (isEmpty(trim(user))) {
this.addError({
Expand Down
33 changes: 33 additions & 0 deletions src/Accounts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,37 @@ describe('Accounts', () => {
expect(form.fields.user.errors).to.include.members(['Username already exists']);
});
});
describe('logout', () => {
it('logs in and out succesfully', async () => {
// Mock the result of a succesful login
Accounts.client = {
login() {
return new Promise(resolve => resolve({
accessToken: 'access token',
refreshToken: 'refresh token',
userId: '123',
username: 'user',
}));
},
logout() {
return new Promise(resolve => resolve());
},
};

const res = await Accounts.login({
user: 'user',
password: '123',
});

expect(res.userId).to.eql('123');
let accounts = Accounts.store.getState().accounts;
expect(accounts.user).to.eql({
userId: '123',
username: 'user',
});
await Accounts.logout();
accounts = Accounts.store.getState().accounts;
expect(accounts.user).to.eql(null);
});
});
});

0 comments on commit 7c29426

Please sign in to comment.