UNPKG

browser-session-tabs-tracker

Version:

A light-weight library for tracking multiple browser tabs in a web session.

137 lines (136 loc) 6.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Cookie = require("js-cookie"); var index_1 = require("./index"); jest.mock('js-cookie', function () { return ({ get: jasmine.createSpy(), set: jasmine.createSpy() }); }); describe('StorageService', function () { var service; beforeEach(function () { service = new index_1.StorageService(); }); describe('session storage', function () { describe('sessionStorageSet()', function () { beforeEach(function () { spyOn(window.sessionStorage.__proto__, 'setItem'); }); it('should set value in session storage', function () { service.sessionStorageSet('my-key', 'my-value'); // session storage is called expect(window.sessionStorage.setItem).toHaveBeenCalled(); // value is stringified expect(window.sessionStorage.setItem).toHaveBeenCalledWith('my-key', '"my-value"'); }); it('should stringify object to json', function () { service.sessionStorageSet('my-key', { foo: 'bar' }); var stringifiedJson = JSON.stringify({ foo: 'bar' }); expect(window.sessionStorage.setItem).toHaveBeenCalledWith('my-key', stringifiedJson); }); }); describe('sessionStorageGet()', function () { var mockSessionStorageGetReturn; beforeEach(function () { mockSessionStorageGetReturn = null; spyOn(window.sessionStorage.__proto__, 'getItem').and.callFake(function () { return mockSessionStorageGetReturn; }); }); it('should return value from session storage', function () { mockSessionStorageGetReturn = JSON.stringify('foo'); var result = service.sessionStorageGet('my-key'); // session storage is called expect(window.sessionStorage.getItem).toHaveBeenCalled(); expect(window.sessionStorage.getItem).toHaveBeenCalledWith('my-key'); // session storage value is returned expect(result).toEqual('foo'); }); it('should parse json object', function () { mockSessionStorageGetReturn = JSON.stringify({ bar: 'foo' }); var result = service.sessionStorageGet('my-key'); // parsed value is returned expect(result).toEqual({ bar: 'foo' }); }); it('should return null if item not in session storage', function () { // item not found, `sessionStorage.getItem() => null` mockSessionStorageGetReturn = null; var result = service.sessionStorageGet('my-key'); expect(result).toBeNull(); }); it('should return provided default value if item not in session storage', function () { // item not found, `sessionStorage.getItem() => null` mockSessionStorageGetReturn = null; var defautlValue = { foo: 'my-default' }; var result = service.sessionStorageGet('my-key', defautlValue); expect(result).toBe(defautlValue); }); it('should return null if item in session storage is not valid', function () { // item is invalid if it's not a valid json string mockSessionStorageGetReturn = 'foo: bar'; // not a valid json var result = service.sessionStorageGet('my-key'); expect(result).toBeNull(); }); }); }); describe('session cookie', function () { beforeEach(function () { Cookie.set.calls.reset(); }); describe('sessionCookieSet()', function () { it('should set value in session cookie', function () { service.sessionCookieSet('my-key', 'my-value'); // session storage is called expect(Cookie.set).toHaveBeenCalled(); // value is stringified expect(Cookie.set).toHaveBeenCalledWith('my-key', '"my-value"'); }); it('should stringify object to json', function () { service.sessionCookieSet('my-key', { foo: 'bar' }); var stringifiedJson = JSON.stringify({ foo: 'bar' }); expect(Cookie.set).toHaveBeenCalledWith('my-key', stringifiedJson); }); }); describe('sessionStorageGet()', function () { var mockCookieValue; beforeEach(function () { mockCookieValue = undefined; var CookieGetSpy = Cookie.get; CookieGetSpy.and.callFake(function () { return mockCookieValue; }); CookieGetSpy.calls.reset(); }); it('should return value from session storage', function () { mockCookieValue = JSON.stringify('foo'); var result = service.sessionCookieGet('my-key'); // session cookie is called expect(Cookie.get).toHaveBeenCalled(); expect(Cookie.get).toHaveBeenCalledWith('my-key'); // session cookie value is returned expect(result).toEqual('foo'); }); it('should parse json object', function () { mockCookieValue = JSON.stringify({ bar: 'foo' }); var result = service.sessionCookieGet('my-key'); // parsed value is returned expect(result).toEqual({ bar: 'foo' }); }); it('should return null if item not in session storage', function () { // item not found, `Cookies.get() => undefined` mockCookieValue = null; var result = service.sessionCookieGet('my-key'); expect(result).toBeNull(); }); it('should return provided default value if item not in session storage', function () { // item not found, `Cookies.get() => undefined` mockCookieValue = null; var defautlValue = { foo: 'my-default' }; var result = service.sessionCookieGet('my-key', defautlValue); expect(result).toBe(defautlValue); }); it('should return null if item in session storage is not valid', function () { // item is invalid if it's not a valid json string mockCookieValue = 'foo: bar'; // not a valid json var result = service.sessionCookieGet('my-key'); expect(result).toBeNull(); }); }); }); });