-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract logic from identify into a profile validation check. that'd b…
…e easier for unit testing
- Loading branch information
Showing
6 changed files
with
110 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |