From 79b359528bb2c2eb280f9f828c2185cdcc7f2365 Mon Sep 17 00:00:00 2001 From: Nicolas Janvier Date: Thu, 3 Sep 2020 14:11:59 +0200 Subject: [PATCH] fix: tests --- src/config.ts | 3 +-- src/cookiePersister.ts | 33 --------------------------------- src/index.ts | 3 +-- src/userSessionPersister.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 37 deletions(-) delete mode 100644 src/cookiePersister.ts diff --git a/src/config.ts b/src/config.ts index a98c98d..8c24f67 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,8 +1,7 @@ import { Condition } from './condition'; -import { CookiePersister } from './cookiePersister'; import { removeAbTestParameter } from './query'; import { ConsoleTracking, Tracking } from './tracking'; -import { UserSessionPersister } from './userSessionPersister'; +import { CookiePersister, UserSessionPersister } from './userSessionPersister'; export interface Config { cookieName: string; diff --git a/src/cookiePersister.ts b/src/cookiePersister.ts deleted file mode 100644 index 8ff6587..0000000 --- a/src/cookiePersister.ts +++ /dev/null @@ -1,33 +0,0 @@ -import config from './config'; -import { UserSessionPersister } from './userSessionPersister'; - -export class CookiePersister implements UserSessionPersister { - private static createCookie(name: string, value: string, days: number): void { - let expires = ''; - if (days) { - const date = new Date(); - date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); - expires = '; expires=' + date.toUTCString(); - } - document.cookie = name + '=' + value + expires + '; path=/'; - } - private static readCookie(name: string): string | null { - const nameEq = name + '='; - const ca = document.cookie.split(';'); - for (let c of ca) { - while (c.charAt(0) === ' ') { - c = c.substring(1, c.length); - } - if (c.indexOf(nameEq) === 0) { - return c.substring(nameEq.length, c.length); - } - } - return null; - } - public loadUserSession() { - return CookiePersister.readCookie(config.cookieName); - } - public saveUserSession(userSession: string, daysToLive: number) { - CookiePersister.createCookie(config.cookieName, userSession, daysToLive); - } -} diff --git a/src/index.ts b/src/index.ts index 5f36f41..c520121 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -import { CookiePersister } from './cookiePersister'; import { InMemoryPersister } from './inMemoryPersister'; import { config, @@ -14,7 +13,7 @@ import { } from './main'; import { SplitTest } from './splitTest'; import { uiFactory } from './ui'; -import { UserSessionPersister } from './userSessionPersister'; +import { CookiePersister, UserSessionPersister } from './userSessionPersister'; const ui = uiFactory( testsObservable, diff --git a/src/userSessionPersister.ts b/src/userSessionPersister.ts index bbdf19b..ebc03ff 100644 --- a/src/userSessionPersister.ts +++ b/src/userSessionPersister.ts @@ -3,3 +3,33 @@ export interface UserSessionPersister { loadUserSession(): string | null; saveUserSession(userSession: string, daysToLive: number): void; } +export class CookiePersister implements UserSessionPersister { + private static createCookie(name: string, value: string, days: number): void { + let expires = ''; + if (days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = '; expires=' + date.toUTCString(); + } + document.cookie = name + '=' + value + expires + '; path=/'; + } + private static readCookie(name: string): string | null { + const nameEq = name + '='; + const ca = document.cookie.split(';'); + for (let c of ca) { + while (c.charAt(0) === ' ') { + c = c.substring(1, c.length); + } + if (c.indexOf(nameEq) === 0) { + return c.substring(nameEq.length, c.length); + } + } + return null; + } + public loadUserSession() { + return CookiePersister.readCookie(config.cookieName); + } + public saveUserSession(userSession: string, daysToLive: number) { + CookiePersister.createCookie(config.cookieName, userSession, daysToLive); + } +}