-
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.
new earlybirds-js poc with unit testing
- Loading branch information
0 parents
commit 2a561fe
Showing
13 changed files
with
3,870 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
'presets': ['es2015', 'stage-2'] | ||
} |
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,3 @@ | ||
import Eb from './src/Eb' | ||
|
||
export default Eb |
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,34 @@ | ||
{ | ||
"dependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-loader": "^7.1.2", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-stage-2": "^6.24.1", | ||
"jest": "^21.2.1", | ||
"raf": "^3.4.0", | ||
"webpack": "^3.8.1" | ||
}, | ||
"name": "eb-poc", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"directories": { | ||
"test": "tests" | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "jest --verbose", | ||
"test:watch": "jest --watchAll --verbose", | ||
"dev": "webpack-dev-server --content-base samples", | ||
"build": "webpack", | ||
"build:production": "NODE_ENV=production webpack -p" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"jest": { | ||
"setupFiles": [ | ||
"raf/polyfill" | ||
] | ||
} | ||
} |
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,6 @@ | ||
<html> | ||
<body> | ||
<div id='app' /> | ||
<script charset="utf-8" src='./bundle.js'></script> | ||
</body> | ||
</html> |
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,5 @@ | ||
import Eb from '..' | ||
|
||
console.log('hello world lol') | ||
const eb = new Eb() | ||
console.log(eb); |
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,24 @@ | ||
import makeSingleton from './utils/singleton' | ||
import Cookies from './utils/Cookies' | ||
|
||
class Eb { | ||
constructor(trackerKey) { | ||
this.defaultProfile = { | ||
hash: null, | ||
lastIdentify: null | ||
} | ||
this.profile = Cookies.getCookie('eb-profile') | ||
this.trackerKey = trackerKey || null | ||
} | ||
init(trackerKey) { | ||
this.trackerKey = trackerKey | ||
} | ||
identify() { | ||
} | ||
getRecommendations() { | ||
} | ||
trackActivity() { | ||
} | ||
} | ||
|
||
export default makeSingleton(Eb) |
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,31 @@ | ||
function Cookies() {} | ||
Cookies.retrieveCookie = function(name) { | ||
const nameEQ = `${name}=`; | ||
const ca = document.cookie.split(';'); | ||
for (let i = 0; i < ca.length; i++) { | ||
let c = ca[i]; | ||
while (c.charAt(0) === ' ') c = c.substring(1, c.length); | ||
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); | ||
} | ||
} | ||
Cookies.getCookie = function(name) { | ||
const retrievedCookie = this.retrieveCookie(name) | ||
let data = null; | ||
try { | ||
data = JSON.parse(retrievedCookie); | ||
} catch (e) { | ||
return retrievedCookie || null; | ||
} | ||
return data; | ||
} | ||
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()}`; | ||
} | ||
document.cookie = `${name}=${value}${expires}; path=/`; | ||
} | ||
export default Cookies |
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,17 @@ | ||
const makeSingleton = obj => { | ||
if (!obj) return null | ||
let instance = null | ||
return class { | ||
getInstance(params) { | ||
if (instance) return instance | ||
console.log('new instance') | ||
instance = new obj(params) | ||
return instance | ||
} | ||
reset(params) { | ||
instance = null | ||
} | ||
} | ||
} | ||
|
||
export default makeSingleton |
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,24 @@ | ||
import Cookies from '../../src/utils/Cookies' | ||
|
||
describe('Cookies', () => { | ||
it('should implement getCookie', () => { | ||
expect(Cookies.getCookie).toBeDefined() | ||
}) | ||
it('should implement setCookie', () => { | ||
expect(Cookies.setCookie).toBeDefined() | ||
}) | ||
it('should store a cookie and retrieve it by its name', () => { | ||
Cookies.setCookie('foo', 'bar') | ||
expect(Cookies.getCookie('foo')).toEqual('bar') | ||
}) | ||
it('should take into account the cookie duration', () => { | ||
|
||
// duration <= 0. should not create the cookie | ||
Cookies.setCookie('foo', 'bar', 0) | ||
expect(Cookies.getCookie('foo')).toEqual(null) | ||
|
||
// duration > 0. should create the cookie | ||
Cookies.setCookie('foo', 'bar', 1) | ||
expect(Cookies.getCookie('foo')).toEqual('bar') | ||
}) | ||
}) |
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,65 @@ | ||
import Eb from '../../src/eb' | ||
import Cookies from '../../src/utils/Cookies' | ||
|
||
describe('Earlybirds class', () => { | ||
describe('Class instantiated correctly', () => { | ||
beforeEach(() => { | ||
Cookies.getCookie = jest.fn(() => 'fakeCookie') | ||
new Eb().reset() | ||
}) | ||
it('should be instanciable', () => { | ||
const ebInstance = new Eb().getInstance() | ||
expect(ebInstance).toBeDefined() | ||
}) | ||
it('should be a singleton', () => { | ||
const a = new Eb().getInstance() | ||
const b = new Eb().getInstance() | ||
expect(a).toBe(b) | ||
}) | ||
it('should have a default profile property that keeps hash and lastIdentify', () => { | ||
const eb = new Eb().getInstance() | ||
expect(eb.defaultProfile).toEqual({ | ||
hash: null, | ||
lastIdentify: null | ||
}) | ||
}) | ||
it('should have a profile property that store the eb-profile cookie', () => { | ||
const eb = new Eb().getInstance() | ||
expect(eb.profile).toBeDefined() | ||
expect(eb.profile).toEqual('fakeCookie') | ||
}) | ||
it('should implement a init method that set the trackerKey attribute with the given value', () => { | ||
const eb = new Eb().getInstance() | ||
expect(eb.init).toBeDefined() | ||
// tracker key null. should not define it | ||
eb.init() | ||
expect(eb.trackerKey).not.toBeDefined() | ||
|
||
eb.init('fakeTrackerKey') | ||
expect(eb.trackerKey).toEqual('fakeTrackerKey') | ||
}) | ||
it('should accept a tracker key during instantiation', () => { | ||
const eb = new Eb().getInstance('fakeTrackerKey') | ||
expect(eb.trackerKey).toEqual('fakeTrackerKey') | ||
}) | ||
}) | ||
describe('Compliance to earlybirds API', () => { | ||
beforeEach(() => { | ||
global.eb = new Eb().getInstance() | ||
}) | ||
it('should implement an identify method', () => { | ||
expect(eb.identify).toBeDefined() | ||
expect(typeof eb.identify).toEqual('function') | ||
}) | ||
it('should implement a getRecommendations method', () => { | ||
expect(eb.getRecommendations).toBeDefined() | ||
expect(typeof eb.getRecommendations).toEqual('function') | ||
}) | ||
it('should implement a trackActivity method', () => { | ||
expect(eb.trackActivity).toBeDefined() | ||
expect(typeof eb.trackActivity).toEqual('function') | ||
}) | ||
}) | ||
describe('Identify', () => { | ||
}) | ||
}) |
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,24 @@ | ||
import makeSingleton from '../../src/utils/singleton' | ||
|
||
describe('makeSingleton', () => { | ||
it('should return null if obj params is undefined', () => { | ||
const mySingleton = makeSingleton() | ||
expect(mySingleton).toEqual(null) | ||
}) | ||
it('should return an instanciable class', () => { | ||
const mySingleton = makeSingleton(() => {}) | ||
expect(typeof mySingleton).toEqual('function') | ||
expect(new mySingleton()).toBeDefined() | ||
}) | ||
it('should implement a getInstance method that return the current instance', () => { | ||
const mySingleton = makeSingleton(() => {}) | ||
expect(new mySingleton().getInstance).toBeDefined() | ||
expect(typeof new mySingleton().getInstance()).toEqual('object') | ||
}) | ||
it('should not re-instantiate an object', () => { | ||
const mySingleton = makeSingleton(() => {}) | ||
const a = new mySingleton().getInstance() | ||
const b = new mySingleton().getInstance() | ||
expect(a).toBe(b) | ||
}) | ||
}) |
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,27 @@ | ||
const webpack = require('webpack') | ||
const path = require('path') | ||
|
||
let config = { | ||
entry: [ | ||
'./index.js' | ||
], | ||
output: { | ||
path: path.resolve(__dirname, 'dist'), | ||
filename: 'bundle.js', | ||
}, | ||
plugins: [ | ||
new webpack.optimize.ModuleConcatenationPlugin() | ||
], | ||
module: { | ||
rules: [{ | ||
test: /\.js$/, | ||
use: 'babel-loader', | ||
}] | ||
} | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
config.entry.push('./samples/index.js') | ||
} | ||
|
||
module.exports = config |
Oops, something went wrong.