Skip to content

Commit

Permalink
extract logic from identify into a profile validation check. that'd b…
Browse files Browse the repository at this point in the history
…e easier for unit testing
  • Loading branch information
spacewoox committed Nov 7, 2017
1 parent 2a561fe commit 6285757
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/eb.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import makeSingleton from './utils/singleton'
import Cookies from './utils/Cookies'
import {
cookieMatchProfile,
lastIdentifyOutdated,
profileHasChanged } from './modules/profileValidationCheck'

class Eb {
constructor(trackerKey) {
this.defaultProfile = {
hash: null,
lastIdentify: null
}
this.profile = Cookies.getCookie('eb-profile')
this.profile = this.retrieveEbProfile()
this.trackerKey = trackerKey || null
}
retrieveEbProfile() {
return Cookies.getCookie('eb-profile') || this.defaultProfile
}
init(trackerKey) {
this.trackerKey = trackerKey
}
identify() {

}
getRecommendations() {
}
Expand Down
15 changes: 15 additions & 0 deletions src/modules/profileValidationCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isEqual, Encode } from '../utils/Utils'

export const ebProfileExist = isEqual

export const cookieIsValid = (cookie, duration) => {
if (!cookie) return false
if (!cookie.lastIdentify) return false
console.log('hello')
return new Date().getTime() - cookie.lastIdentify < duration
}

export const profileHasChanged =
(cookie, profile) =>
!isEqual(cookie.hash, Encode(profile))

30 changes: 30 additions & 0 deletions src/utils/Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const isEqual = (obj1, obj2) => {
for (var i in obj1) {
if (obj1.hasOwnProperty(i)) {
if (!obj2.hasOwnProperty(i)) return false;
if (obj1[i] != obj2[i]) return false;
}
}
for (var i in obj2) {
if (obj2.hasOwnProperty(i)) {
if (!obj1.hasOwnProperty(i)) return false;
if (obj1[i] != obj2[i]) return false;
}
}
return true;
}

export const Encode = object => {
const str = JSON.stringify(object);
let hash = 0;
let i;
let chr;
let len;
if (str.length === 0) return hash;
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash;
}
5 changes: 1 addition & 4 deletions src/utils/singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ const makeSingleton = obj => {
let instance = null
return class {
getInstance(params) {
if (instance) return instance
console.log('new instance')
instance = new obj(params)
return instance
return instance || (instance = new obj(params), instance)
}
reset(params) {
instance = null
Expand Down
12 changes: 10 additions & 2 deletions tests/integration/eb.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('Earlybirds class', () => {
lastIdentify: null
})
})
it('should implement a retrieveEbProfile that return the eb-profile cookie content', () => {
const eb = new Eb().getInstance()
expect(eb.retrieveEbProfile).toBeDefined()
expect(eb.retrieveEbProfile()).toEqual('fakeCookie')
})
it('should have a profile property that store the eb-profile cookie', () => {
const eb = new Eb().getInstance()
expect(eb.profile).toBeDefined()
Expand Down Expand Up @@ -59,7 +64,10 @@ describe('Earlybirds class', () => {
expect(eb.trackActivity).toBeDefined()
expect(typeof eb.trackActivity).toEqual('function')
})
})
describe('Identify', () => {
describe('Identify', () => {
it('should', () => {

})
})
})
})
45 changes: 45 additions & 0 deletions tests/unit/profileValidationCheck.unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
ebProfileExist,
cookieIsValid,
profileHasChanged } from '../../src/modules/profileValidationCheck'

const defaultProfile = {
hash: null,
lastIdentify: null,
}

describe('profileValidationCheck', () => {
describe('ebProfileExist', () => {

it('should be truthy when cookie and defaultProfile are the same', () => {
expect(ebProfileExist(defaultProfile, defaultProfile)).toBeTruthy()
})
it('should be falsy when cookie and defaultProfile are different', () => {
expect(ebProfileExist({}, defaultProfile)).toBeFalsy()
})
})

describe('cookieIsValid', () => {

it('should be falsy if cookie does not exist', () => {
expect(cookieIsValid(null)).toBeFalsy()
})
it('should be falsy if cookie.lastIdentify does not exist', () => {
expect(cookieIsValid({})).toBeFalsy()
})
it('should be falsy if lastIdentify > duration', () => {
Date.prototype.getTime = () => 5
const cookie = {
lastIdentify: 0
}
expect(cookieIsValid(cookie, 2)).toBeFalsy()
})
it('should be truthy if lastIdentify < duration', () => {
Date.prototype.getTime = () => 0
const cookie = {
lastIdentify: 0
}
expect(cookieIsValid(cookie, 2)).toBeTruthy()
})
})
})

0 comments on commit 6285757

Please sign in to comment.