browser-session-tabs-tracker
Version:
A light-weight library for tracking multiple browser tabs in a web session.
332 lines (331 loc) • 16.3 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("./index");
jest.mock('../storage-service');
describe('BrowserTabTracker', function () {
var storageService = {
sessionStorageSet: jasmine.createSpy(),
sessionStorageGet: jasmine.createSpy(),
sessionCookieSet: jasmine.createSpy(),
sessionCookieGet: jasmine.createSpy()
};
var service;
beforeEach(function () {
service = new index_1.BrowserTabTracker(storageService);
});
describe('storageKey', function () {
it('should be `browser-session-info` by default', function () {
expect(service.storageKey).toEqual('browser-session-info');
});
});
describe('initialize', function () {
beforeEach(function () {
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(null);
});
describe('storageKey', function () {
var sessionIdGenerator;
beforeEach(function () {
sessionIdGenerator = function () { return 'dummy-id'; };
});
it('should be `browser-session-info` by default', function () {
expect(service.storageKey).toEqual('browser-session-info');
});
it('should be able to set storage key when initializing', function () {
service.initialize({
sessionIdGenerator: sessionIdGenerator,
storageKey: 'my-key'
});
expect(service.storageKey).toEqual('my-key');
});
it('should throw error if provided storage key is invalid', function () {
expect(function () {
service.initialize({
sessionIdGenerator: sessionIdGenerator,
storageKey: ' ' // invalid key
});
}).toThrow();
});
});
describe('sessionIdGenerator', function () {
it('should throw error if session id generator is invalid', function () {
expect(function () {
service.initialize({
sessionIdGenerator: null
});
}).toThrow();
});
it('should not throw error if session id generator is valid', function () {
var sessionIdGenerator = function () { return 'my-random-id'; };
expect(function () {
service.initialize({
sessionIdGenerator: sessionIdGenerator
});
}).not.toThrow();
});
});
describe('sessionStartedCallback', function () {
var sessionIdGenerator;
beforeEach(function () {
sessionIdGenerator = function () { return 'dummy-id'; };
});
it('should not throw error if session started callback is not provided', function () {
expect(function () {
service.initialize({
sessionIdGenerator: sessionIdGenerator
});
}).not.toThrow();
});
it('should be able to set storage key when initializing', function () {
// session info not in storage, so this will be a new session
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(null);
// new session id
sessionIdGenerator = function () { return 'my-new-session-id'; };
var callback = jasmine.createSpy();
service.initialize({
sessionIdGenerator: sessionIdGenerator,
sessionStartedCallback: callback
});
expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledWith('my-new-session-id', // session id
1 // tab id
);
});
it('should not trigger the callback multiple times', function () {
// session info not in storage, so this will be a new session
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(null);
var callback = jasmine.createSpy();
service.initialize({
sessionIdGenerator: sessionIdGenerator,
sessionStartedCallback: callback
});
// called the first time
expect(callback).toHaveBeenCalledTimes(1);
// try to reinitialize multiple times
service.initialize({
sessionIdGenerator: sessionIdGenerator,
sessionStartedCallback: callback
});
service.initialize({
sessionIdGenerator: sessionIdGenerator,
sessionStartedCallback: callback
});
// still only called the first time
expect(callback).toHaveBeenCalledTimes(1);
});
});
});
describe('tabId', function () {
var sessionIdGenerator;
beforeEach(function () {
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(null);
storageService.sessionStorageGet.calls.reset();
storageService.sessionStorageSet.calls.reset();
storageService.sessionCookieGet.calls.reset();
storageService.sessionCookieSet.calls.reset();
sessionIdGenerator = function () { return 'dummy-id'; };
});
it('should throw error if not initialized', function () {
expect(function () {
var result = service.tabId;
}).toThrow();
});
it('should not throw error if initialized', function () {
service.initialize({ sessionIdGenerator: sessionIdGenerator });
expect(function () {
var result = service.tabId;
}).not.toThrow();
});
it('should return a new tab id starting at 1 if there is not session storage', function () {
service.initialize({ sessionIdGenerator: sessionIdGenerator });
expect(service.tabId).toEqual(1);
});
it('should return tab id stored in session storage', function () {
var sessionInfo = buildSessionInfo({
tab: 4
});
storageService.sessionStorageGet.and.returnValue(sessionInfo);
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// same as already in session storage
expect(service.tabId).toEqual(4);
// taken from session storage
expect(storageService.sessionStorageGet).toHaveBeenCalled();
expect(storageService.sessionStorageGet).toHaveBeenCalledWith(service.storageKey);
// cookies are not checked
expect(storageService.sessionCookieGet).not.toHaveBeenCalled();
});
it('should return incremented tab id stored in session cookie', function () {
var sessionInfo = buildSessionInfo({
tab: 8
});
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(sessionInfo);
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// incremented tab id from previous tab
expect(service.tabId).toEqual(9);
// session storage is checked
expect(storageService.sessionStorageGet).toHaveBeenCalled();
expect(storageService.sessionStorageGet).toHaveBeenCalledWith(service.storageKey);
// then session cookies are checked
expect(storageService.sessionCookieGet).toHaveBeenCalled();
expect(storageService.sessionCookieGet).toHaveBeenCalledWith(service.storageKey);
});
it('should save new session info in session cookie and storage when generated', function () {
var sessionInfo = buildSessionInfo({
id: 'my-session-id',
tab: 3
});
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(sessionInfo);
// info is retrieved
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// saved in session storage (with incremented tab id)
expect(storageService.sessionStorageSet).toHaveBeenCalled();
expect(storageService.sessionStorageSet).toHaveBeenCalledWith(service.storageKey, {
id: 'my-session-id',
tab: 4
});
// saved in session cookie (with incremented tab id)
expect(storageService.sessionCookieSet).toHaveBeenCalled();
expect(storageService.sessionCookieSet).toHaveBeenCalledWith(service.storageKey, {
id: 'my-session-id',
tab: 4
});
});
it('should save in storage and cookie only the first time the value is retrieved', function () {
var sessionInfo = buildSessionInfo({
tab: 2
});
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(sessionInfo);
// first retrieval
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// saved the first time
expect(storageService.sessionStorageSet).toHaveBeenCalledTimes(1);
expect(storageService.sessionCookieSet).toHaveBeenCalledTimes(1);
// try to initialize multiple times
service.initialize({ sessionIdGenerator: sessionIdGenerator });
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// only saved the first time
expect(storageService.sessionStorageSet).toHaveBeenCalledTimes(1);
expect(storageService.sessionCookieSet).toHaveBeenCalledTimes(1);
});
});
describe('sessionId', function () {
var sessionIdGenerator;
beforeEach(function () {
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(null);
storageService.sessionStorageGet.calls.reset();
storageService.sessionStorageSet.calls.reset();
storageService.sessionCookieGet.calls.reset();
storageService.sessionCookieSet.calls.reset();
sessionIdGenerator = function () { return 'dummy-id'; };
});
it('should throw error if not initialized', function () {
expect(function () {
var result = service.sessionId;
}).toThrow();
});
it('should not throw error if initialized', function () {
service.initialize({ sessionIdGenerator: sessionIdGenerator });
expect(function () {
var result = service.sessionId;
}).not.toThrow();
});
it('should return a new session id that was returned by the session id generator', function () {
sessionIdGenerator = function () { return 'my-custom-random-session-id'; };
service.initialize({ sessionIdGenerator: sessionIdGenerator });
var result = service.sessionId;
expect(result).toEqual('my-custom-random-session-id');
});
it('should return session id stored in session storage', function () {
var sessionInfo = buildSessionInfo({
id: 'my-session-id-from-storage'
});
storageService.sessionStorageGet.and.returnValue(sessionInfo);
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// same as already in session storage
expect(service.sessionId).toEqual('my-session-id-from-storage');
// taken from session storage
expect(storageService.sessionStorageGet).toHaveBeenCalled();
expect(storageService.sessionStorageGet).toHaveBeenCalledWith(service.storageKey);
// cookies are not checked
expect(storageService.sessionCookieGet).not.toHaveBeenCalled();
});
it('should return incremented tab id stored in session cookie', function () {
var sessionInfo = buildSessionInfo({
id: 'my-session-id-from-cookie'
});
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(sessionInfo);
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// taken from session cookie
expect(service.sessionId).toEqual('my-session-id-from-cookie');
// session storage is checked
expect(storageService.sessionStorageGet).toHaveBeenCalled();
expect(storageService.sessionStorageGet).toHaveBeenCalledWith(service.storageKey);
// then session cookies are checked
expect(storageService.sessionCookieGet).toHaveBeenCalled();
expect(storageService.sessionCookieGet).toHaveBeenCalledWith(service.storageKey);
});
it('should save new session info in session cookie and storage when generated new tab id is created', function () {
var sessionInfo = buildSessionInfo({
id: 'my-session-id',
tab: 3
});
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(sessionInfo);
// info is retrieved
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// saved in session storage (with incremented tab id)
expect(storageService.sessionStorageSet).toHaveBeenCalled();
expect(storageService.sessionStorageSet).toHaveBeenCalledWith(service.storageKey, {
id: 'my-session-id',
tab: 4 // tab id is incremented
});
// saved in session cookie (with incremented tab id)
expect(storageService.sessionCookieSet).toHaveBeenCalled();
expect(storageService.sessionCookieSet).toHaveBeenCalledWith(service.storageKey, {
id: 'my-session-id',
tab: 4 // tab id is incremented
});
});
it('should save in storage and cookie only the first time the value is retrieved', function () {
var sessionInfo = buildSessionInfo({
id: 'my-session-id'
});
storageService.sessionStorageGet.and.returnValue(null);
storageService.sessionCookieGet.and.returnValue(sessionInfo);
// first retrieval
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// saved the first time
expect(storageService.sessionStorageSet).toHaveBeenCalledTimes(1);
expect(storageService.sessionCookieSet).toHaveBeenCalledTimes(1);
// initialize again multiple times
service.initialize({ sessionIdGenerator: sessionIdGenerator });
service.initialize({ sessionIdGenerator: sessionIdGenerator });
// only saved the first time
expect(storageService.sessionStorageSet).toHaveBeenCalledTimes(1);
expect(storageService.sessionCookieSet).toHaveBeenCalledTimes(1);
});
});
function buildSessionInfo(props) {
if (props === void 0) { props = {}; }
return __assign({}, props);
}
});