Modernize session script with minor fixes and additional tests

Change-Id: Ic76d591e982e4f6e17fbe0f57400a3ac9f5bfa14
diff --git a/dev/js/runner/all.html b/dev/js/runner/all.html
index 48e7353..037d04e 100644
--- a/dev/js/runner/all.html
+++ b/dev/js/runner/all.html
@@ -50,7 +50,8 @@
         'spec/tourSpec',
         'spec/utilSpec',
         'spec/stateSpec',
-        'spec/pipeSpec'
+        'spec/pipeSpec',
+        'spec/sessionSpec'
       ],
       function () {
         window.onload();
diff --git a/dev/js/spec/sessionSpec.js b/dev/js/spec/sessionSpec.js
new file mode 100644
index 0000000..147b512
--- /dev/null
+++ b/dev/js/spec/sessionSpec.js
@@ -0,0 +1,61 @@
+define(['session'], function (sessionClass) {
+
+  beforeEach(
+    function () {
+      document.cookie.split(';').forEach(
+        function (i) {
+          var pair = i.split('=');
+          var name = pair[0].trim().toLowerCase();
+
+        }
+      );
+    }
+  );
+
+  describe('KorAP.Session', function () {  
+    it('should be initializable', function () {
+      let s = sessionClass.create();
+      
+      expect(s).toBeTruthy();
+
+      expect(s.toString().startsWith('korap=')).toBeTruthy();
+
+      s = sessionClass.create('kalamar');
+      
+      expect(s).toBeTruthy();
+
+      expect(s.toString().startsWith('kalamar=')).toBeTruthy();
+    });
+
+    it('should settable and gettable', function () {
+      let s = sessionClass.create('koraptest');
+      s.set("test1", "works");
+
+      expect(s.get("test1")).toEqual("works");
+
+      s.set("test2", "\"wor}ks\"");
+
+      expect(s.get("test1")).toEqual("works");
+      expect(s.get("test2")).toEqual("\"wor}ks\"");
+
+      expect(s.toString().includes("test1")).toBeTruthy();
+      expect(s.toString().includes("test2")).toBeTruthy();
+      expect(s.toString().includes("works")).toBeTruthy();
+      expect(s.toString().includes("%5C%22wor%7Dks%5C%22")).toBeTruthy();
+    });
+
+    it('should write to cookie', function () {
+      let s = sessionClass.create('koraptest');
+      s.clear();     
+      expect(s.toString().includes("koraptest=%7B%7D;")).toBeTruthy();
+      expect(document.cookie.includes("koraptest=")).toBeTruthy();
+      s.set("test3", "works");
+      expect(s.toString().includes("koraptest=%7B%7D;")).toBeFalsy();
+      expect(s.toString().includes("koraptest=%7B%22test3%22%3A%22works%22%7D")).toBeTruthy();
+      expect(document.cookie.includes("koraptest=%7B%22test3%22%3A%22works%22%7D")).toBeTruthy();
+      s.clear();     
+      expect(document.cookie.includes("koraptest=")).toBeTruthy();
+      expect(s.toString()).toEqual("koraptest=%7B%7D;");
+    });   
+  })
+});
diff --git a/dev/js/src/session.js b/dev/js/src/session.js
index 178b83c..92bbadf 100644
--- a/dev/js/src/session.js
+++ b/dev/js/src/session.js
@@ -4,19 +4,23 @@
  *
  * @author Nils Diewald
  */
+
+"use strict";
+
 define({
   /**
    * Create a new session.
    * Expects a name or defaults to 'korap'
    */
   create : function (name = 'korap') {
-    var obj = Object.create(this);
+    const obj = Object.create(this);
     obj._name = name.toLowerCase();
     obj._hash = {};
     obj._parse();
     return obj;
   },
 
+
   /**
    * Get a value based on a key.
    * The value can be complex, as the value is stored as JSON.
@@ -25,6 +29,7 @@
     return this._hash[key.toLowerCase()];
   },
 
+
   /**
    * Set a value based on a key.
    * The value can be complex, as the value is stored as JSON.
@@ -34,37 +39,49 @@
     this._store();
   },
 
+
   /**
    * Clears the session by removing the cookie
    */
   clear : function () {
+    this._hash = {};
     document.cookie = this._name + '=; expires=-1';
   },
 
+
   /* Store cookie */
   _store : function () {
+    document.cookie = this.toString();
+  },
+
+
+  /**
+   * Stringify session cookie.
+   */
+  toString : function () {
     /*
       var date = new Date();
       date.setYear(date.getFullYear() + 1);
     */
-    document.cookie =
-      this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';';
+    return this._name + '=' + encodeURIComponent(JSON.stringify(this._hash)) + ';';
   },
 
-  /* Parse cookie */
+
+  /**
+   * Parse cookie
+   */
   _parse : function () {
-    var c = document.cookie;
     document.cookie.split(';').forEach(
-      function(i) {
-        var pair = i.split('=');
-        var name = pair[0].trim().toLowerCase();
+      function (i) {
+        const pair = i.split('=');
+        const name = pair[0].trim().toLowerCase();
         if (name === this._name) {
 	        if (pair.length === 1 || pair[1].length === 0)
 	          return;
           this._hash = JSON.parse(decodeURIComponent(pair[1]));
 	        return;
         };
-      }
-    );
+      },
+      this);
   }
 });