Add tests for session.js

This commit is contained in:
Theresa Gresch 2019-08-12 10:26:26 +02:00
parent a438ab73c5
commit 0548810d5c

View file

@ -1,8 +1,20 @@
import assert from 'assert';
import Session from 'common/session';
import User from 'model/user';
import MockAdapter from "axios-mock-adapter";
import Api from "common/api";
const mock = new MockAdapter(Api);
mock
.onPost("session").reply(200, {token: "8877", user: {email: "test@test.com", password: "passwd"}})
.onDelete("session/8877").reply(200);
describe('common/session', () => {
beforeEach(() => {
window.onbeforeunload = () => 'Oh no!';
});
it('should construct session', () => {
const storage = window.localStorage;
const session = new Session(storage);
@ -118,6 +130,16 @@ describe('common/session', () => {
session.deleteUser();
});
it('should test login and logout', async() => {
const storage = window.localStorage;
const session = new Session(storage);
assert.equal(session.session_token, null);
assert.equal(session.storage.user, undefined);
await session.login("test@test.com", "passwd");
assert.equal(session.session_token, 8877);
assert.equal(session.storage.user, "{\"email\":\"test@test.com\",\"password\":\"passwd\"}");
await session.logout();
assert.equal(session.session_token, null);
});
});