Skip to content

Commit

Permalink
more test
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewoox committed Nov 8, 2017
1 parent 6285757 commit e5963ef
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 55 deletions.
3 changes: 3 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
'API_URL': 'api.early-birds.fr'
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"jest": "^21.2.1",
"jest-fetch-mock": "^1.3.3",
"raf": "^3.4.0",
"webpack": "^3.8.1"
},
Expand Down
36 changes: 31 additions & 5 deletions src/eb.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import makeSingleton from './utils/singleton'
import Cookies from './utils/Cookies'
import Config from '../config'
import { Encode } from './utils/Utils'
import {
cookieMatchProfile,
lastIdentifyOutdated,
profileHasChanged } from './modules/profileValidationCheck'
shouldInitiateIdentifyRequest } from './modules/profileValidationCheck'

const HTTP_PROTOCOL = (document.location.protocol == 'https:' ? 'https://' : 'http://');

class Eb {
constructor(trackerKey) {
Expand All @@ -20,8 +22,32 @@ class Eb {
init(trackerKey) {
this.trackerKey = trackerKey
}
identify() {

identify(profile) {
if (profile === undefined) return null;
if (!this.trackerKey) return null;
if (shouldInitiateIdentifyRequest(profile, this.defaultProfile)) {
return fetch(`${HTTP_PROTOCOL}${Config.API_URL}/tracker/${this.trackerKey}/identify`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(profile)
})
.then(x => x.json())
.then(response => {
const newProfile = {
...response.profile,
lastIdentify: new Date().getTime(),
hash: Encode(profile)
}
Cookies.setCookie(JSON.stringify(newProfile));
//this.profile = newProfile
return response;
})
.catch(console.log)
}
return this.defaultProfile;
}
getRecommendations() {
}
Expand Down
28 changes: 18 additions & 10 deletions src/modules/profileValidationCheck.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
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
// last identify is greater than duration
export const cookieDurationIsOutdated = (cookie, duration) => {
if (!cookie) return true;
if (cookie && cookie.lastIdentify == undefined) return true;
return (new Date().getTime() - cookie.lastIdentify) > duration
}

export const profileHasChanged =
(cookie, profile) =>
!isEqual(cookie.hash, Encode(profile))
// the given cookie hash is different from the given profile
export const cookieHashAndProfileMatch =
(cookie, profile) =>
isEqual(cookie.hash, Encode(profile))

export const shouldInitiateIdentifyRequest = (cookie, profile, duration) => {
if (!cookie)
return true
if (!cookieHashAndProfileMatch(cookie, profile))
return true
if (duration && cookieDurationIsOutdated(cookie, duration))
return true
return false
}
1 change: 0 additions & 1 deletion src/utils/Cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Cookies.getCookie = function(name) {
Cookies.setCookie = function(name, value, days) {
let expires = '';
if (days !== null) {
console.log('days exist')
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = `; expires=${date.toUTCString()}`;
Expand Down
140 changes: 137 additions & 3 deletions tests/integration/eb.integration.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import Eb from '../../src/eb'
import Cookies from '../../src/utils/Cookies'
import { Encode } from '../../src/utils/Utils'
import {
shouldInitiateIdentifyRequest } from '../../src/modules/profileValidationCheck'

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

describe('Earlybirds class', () => {
beforeEach(() => {
new Eb().reset()
})
describe('Class instantiated correctly', () => {
beforeEach(() => {
Cookies.getCookie = jest.fn(() => 'fakeCookie')
new Eb().reset()
})
it('should be instanciable', () => {
const ebInstance = new Eb().getInstance()
Expand Down Expand Up @@ -52,6 +62,9 @@ describe('Earlybirds class', () => {
beforeEach(() => {
global.eb = new Eb().getInstance()
})
afterEach(() => {
delete global.eb
})
it('should implement an identify method', () => {
expect(eb.identify).toBeDefined()
expect(typeof eb.identify).toEqual('function')
Expand All @@ -64,9 +77,130 @@ describe('Earlybirds class', () => {
expect(eb.trackActivity).toBeDefined()
expect(typeof eb.trackActivity).toEqual('function')
})
describe('Identify', () => {
it('should', () => {
})
describe('Identify', () => {
describe('shouldInitiateIdentifyRequest', () => {
beforeEach(() => {
Date.prototype.getTime = jest.fn(() => 5)
})
afterEach(() => {
Date.prototype.getTime.mockRestore()
})
it('should implement a "shouldInitiateIdentifyRequest" that checks if a new identify is required', () => {
expect(shouldInitiateIdentifyRequest).toBeDefined()
})
it('should be truthy if eb profile cookie does not exist', () => {
let cookie = undefined
const res = shouldInitiateIdentifyRequest(cookie, defaultProfile)
expect(res).toBeTruthy()
})
it('should be truthy if cookie is outdated', () => {
const fakeCookie = {
hash: null,
lastIdentify: 0,
}
const fakeDuration = 1
const res = shouldInitiateIdentifyRequest(fakeCookie, defaultProfile, fakeDuration)
expect(res).toBeTruthy()
})
it('should be truthy if cookie hash and profile does not match', () => {
const fakeCookie = {
hash: 'fakeHash',
lastIdentify: 50,
}
const res = shouldInitiateIdentifyRequest(fakeCookie, defaultProfile)
expect(res).toBeTruthy()
})
it('should be falsy if cookie hash and profile matches', () => {
const encodedDefaultProfile = Encode(defaultProfile)
const fakeCookie = {
hash: encodedDefaultProfile,
lastIdentify: 5,
}
const res = shouldInitiateIdentifyRequest(fakeCookie, defaultProfile)
expect(res).toBeFalsy()
})
})
it('should return null if no profile is provided', () => {
const eb = new Eb().getInstance()
expect(eb.identify()).toEqual(null);
})
it('should return the current profile if identify is not needed', () => {
let mock = jest.fn()
Cookies.setCookie = mock
const eb = new Eb().getInstance('fakeTrackerKey')
expect(eb.identify(defaultProfile)).toEqual(defaultProfile);

// should not set eb-profile cookie in this case
expect(mock).not.toBeCalled()
})
it('should return null if trackerKey is not set', () => {
const eb = new Eb().getInstance()
const res = eb.identify({})
expect(res).toEqual(null)
})
describe('should make a http request that returns a promise then set the eb-profile cookie if identify is outdated', () => {
const fakeProfile = {
hash: 'fakeHash',
lastIdentify: 10
}
beforeEach(() => {
global.fetch = require('jest-fetch-mock')
fetch.mockResponse(JSON.stringify({}))
Date.prototype.getTime = jest.fn(() => 0)
})
afterEach(() => {
global.fetch.mockRestore()
Date.prototype.getTime.mockRestore()
})
it('should call fetch', () => {
const eb = new Eb().getInstance('fakeTrackerKey')
eb.identify(fakeProfile)
expect(fetch).toBeCalled()
})
it('should set cookie', () => {
const eb = new Eb().getInstance('fakeTrackerKey')
Cookies.setCookie = jest.fn()
eb
.identify(fakeProfile)
.then(() => {
expect(Cookies.setCookie).toBeCalled()
})
.catch(err => {
})
})
it('should set cookie with the correct profile and update profile property', done => {
Cookies.setCookie = jest.fn()
//that's what the identify returns
let fakeResponse = {
profile: {
id: 'fakeId'
},
cookie: {
domain: ''
}
}
fetch.mockResponse(JSON.stringify(fakeResponse))
// the profile object built
const expectedResult = {
...fakeResponse.profile,
lastIdentify: 0,
hash: Encode(fakeProfile)
}
let fakeCookieExpected = JSON.stringify(expectedResult)

const eb = new Eb().getInstance('fakeTrackerKey')
eb
.identify(fakeProfile)
.then(() => {
console.log('roulite');
expect(Cookies.setCookie).toBeCalledWith(fakeCookieExpected)
//expect(eb.profile).toEqual(expectedResult)
done()
})
.catch((err) => {
console.log(err)
})
})
})
})
Expand Down
69 changes: 35 additions & 34 deletions tests/unit/profileValidationCheck.unit.test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import {
ebProfileExist,
cookieIsValid,
profileHasChanged } from '../../src/modules/profileValidationCheck'
cookieDurationIsOutdated,
cookieHashAndProfileMatch,
shouldInitiateIdentifyRequest } from '../../src/modules/profileValidationCheck'
import { Encode } from '../../src/utils/Utils'

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

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

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()
})
it('should be truthy if cookie hash and defaultProfile are the same', () => {
expect(cookieHashAndProfileMatch({ hash: Encode(defaultProfile) }, defaultProfile)).toBeTruthy()
})
it('should be falsy if cookie hash and defaultProfile are different', () => {
expect(cookieHashAndProfileMatch({ hash: 'hash' }, defaultProfile)).toBeFalsy()
})
})

describe('cookieIsValid', () => {
describe('cookieDurationIsOutdated', () => {

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()
})
it('should be truthy if cookie does not exist', () => {
expect(cookieDurationIsOutdated(null)).toBeTruthy()
})
it('should be truthy if cookie.lastIdentify does not exist', () => {
expect(cookieDurationIsOutdated({})).toBeTruthy()
})
it('should be truthy if lastIdentify > duration', () => {
Date.prototype.getTime = () => 5
const cookie = {
lastIdentify: 0
}
expect(cookieDurationIsOutdated(cookie, 2)).toBeTruthy()
})
it('should be falsy if lastIdentify < duration', () => {
Date.prototype.getTime = () => 0
const cookie = {
lastIdentify: 0
}
expect(cookieDurationIsOutdated(cookie, 2)).toBeFalsy()
})
})
})
})
Loading

0 comments on commit e5963ef

Please sign in to comment.