From 0548810d5c3896c960747e69b09c235864374696 Mon Sep 17 00:00:00 2001 From: Theresa Gresch Date: Mon, 12 Aug 2019 10:26:26 +0200 Subject: [PATCH] Add tests for session.js --- frontend/tests/unit/common/session_test.js | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/frontend/tests/unit/common/session_test.js b/frontend/tests/unit/common/session_test.js index ae30c8fca..f80918a4c 100644 --- a/frontend/tests/unit/common/session_test.js +++ b/frontend/tests/unit/common/session_test.js @@ -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); + }); }); \ No newline at end of file