Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Janvier committed Sep 3, 2020
1 parent 4269867 commit 79b3595
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
33 changes: 0 additions & 33 deletions src/cookiePersister.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CookiePersister } from './cookiePersister';
import { InMemoryPersister } from './inMemoryPersister';
import {
config,
Expand All @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions src/userSessionPersister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 79b3595

Please sign in to comment.