Skip to content

Commit

Permalink
new earlybirds-js poc with unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewoox committed Nov 6, 2017
0 parents commit 2a561fe
Show file tree
Hide file tree
Showing 13 changed files with 3,870 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
'presets': ['es2015', 'stage-2']
}
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Eb from './src/Eb'

export default Eb
34 changes: 34 additions & 0 deletions package.json
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"
]
}
}
6 changes: 6 additions & 0 deletions samples/index.html
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>
5 changes: 5 additions & 0 deletions samples/index.js
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);
24 changes: 24 additions & 0 deletions src/eb.js
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)
31 changes: 31 additions & 0 deletions src/utils/Cookies.js
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
17 changes: 17 additions & 0 deletions src/utils/singleton.js
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
24 changes: 24 additions & 0 deletions tests/integration/cookies.integration.test.js
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')
})
})
65 changes: 65 additions & 0 deletions tests/integration/eb.integration.test.js
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', () => {
})
})
24 changes: 24 additions & 0 deletions tests/integration/singleton.integration.test.js
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)
})
})
27 changes: 27 additions & 0 deletions webpack.config.js
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
Loading

0 comments on commit 2a561fe

Please sign in to comment.