Skip to content

Commit

Permalink
Merge pull request #1 from Financial-Times/tests
Browse files Browse the repository at this point in the history
Adding Tests
  • Loading branch information
aintgoin2goa committed May 14, 2015
2 parents 9a7b6f7 + 75d624a commit 319084a
Show file tree
Hide file tree
Showing 15 changed files with 393 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/
node_modules/
bower_components/
test/app/public/

11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: false
language: node_js
node_js:
- "0.12.2"
before_install:
- npm install -g karma-cli
- npm install -g origami-build-tools
install:
- make install
script:
- make verify test
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
.PHONY: test

clean:
git clean -xfd

install:
origami-build-tools install

verify:
nbt verify
nbt verify --skip-layout-checks

test:
./node_modules/karma/bin/karma start
karma start

test-app:
rm -rf test/app/public/
mkdir test/app/public/
browserify test/app/main.js --debug --transform debowerify > test/app/public/bundle.js
node test/app/server.js
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"tests"
],
"dependencies": {
"isomorphic-fetch": "~2.0.2"
"isomorphic-fetch": "~2.0.2",
"es6-promise": "~2.1.0"
}
}
95 changes: 51 additions & 44 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,84 @@
'use strict';
// Karma configuration
// Generated on Tue May 12 2015 10:54:45 GMT+0100 (BST)

module.exports = function(config) {
var configuration = {
var configuration = {

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['browserify', 'mocha'],
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['browserify', 'mocha'],


// list of files / patterns to load in the browser
files: [
'src/*.js',
'test/*.spec.js'
],
// list of files / patterns to load in the browser
files: [
'test/*.spec.js'
],


// list of files to exclude
exclude: [
],
// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/*.js': [ 'browserify' ]
},
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/*.js': [ 'browserify' ]
},

browserify: {
transform: ['debowerify'],
debug: true
},
browserify: {
transform: ['debowerify'],
debug: true
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,
// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,
// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
};
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

customLaunchers: {
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
}
};

if(process.env.TRAVIS){
configuration.singleRun = true;
configuration.browsers = ['PhantomJS', 'Chrome_travis_ci'];
configuration.browsers = ['PhantomJS'];
}

config.set(configuration);
Expand Down
44 changes: 30 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
'use strict';
var request = require('./src/request');
var cache = require('./src/cache');

function makeRequest(url){
return request(url).then(function(response){
return response.ok ? response.json() : Promise.resolve(false);
}).catch(function(e){
setTimeout(function(){
throw e;
}, 0);
})

function details(){
// if it has products then it must've been a full call
if(cache('products')){
return Promise.resolve(cache());
}

return request('/').then(function(sessionDetails){
cache(sessionDetails);
return sessionDetails;
});
}

function uuid(){
if(cache('uuid')){
return Promise.resolve({uuid:cache('uuid')});
}

return request('/uuid').then(function(response){
cache('uuid', response.uuid);
return response;
});
}

function session(){
return makeRequest('/');
function validate(){
return request('/validate');
}

session.validate = function(){
return makeRequest('/validate');
module.exports = {
details : details,
uuid : uuid,
validate : validate,
cache : cache
};

module.exports = session;
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@
},
"homepage": "https://github.com/Financial-Times/next-session-component",
"devDependencies": {
"browserify": "^10.1.3",
"chai": "^2.3.0",
"debowerify": "^1.2.1",
"express": "^4.12.3",
"fetch-mock": "^1.4.1",
"isomorphic-fetch": "^2.0.2",
"karma": "^0.12.31",
"karma-browserify": "^4.1.2",
"karma-chrome-launcher": "^0.1.10",
"karma-mocha": "^0.1.10",
"karma-phantomjs-launcher": "^0.1.4",
"mocha": "^2.2.4",
"next-build-tools": "^2.3.4",
"sinon": "^1.14.1"
},
"dependencies": {
"origami-build-tools": "^2.13.0",
"sinon": "^1.14.1"
}
}
32 changes: 32 additions & 0 deletions src/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var detailsCache = {};


function cache(name, value) {
if(typeof name === 'object'){
detailsCache = name;
return;
}

if(typeof name === 'string' && typeof value === 'string') {
detailsCache[name] = value;
return;
}

if(typeof name === 'string' && typeof value === 'undefined') {
return detailsCache[name] || null;
}

if(typeof name === 'undefined' && typeof value === 'undefined') {
return detailsCache;
}

throw new Error('Invalid arguments');
}

cache.clear = function(){
detailsCache = {};
};

module.exports = cache;
44 changes: 38 additions & 6 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,59 @@ require('isomorphic-fetch');
require('es6-promise').polyfill();

var sessionServiceEndpoint = 'https://session-next.ft.com';
var callbackName = '$$$JSONP_CALLBACK';
var jsonpTimeout = 2000;

window.FT = window.FT || {};

function fetchRequest(url){
return fetch(url, {credentials:'include'});
return fetch(sessionServiceEndpoint + url, {credentials:'include'});
}

function jsonpRequest(url){
return new Promise(function(resolve, reject){
window.FT.$$$JSONP_CALLBACK = function(response){
resolve({ok:response.sucess, json:function(){
window.FT[callbackName] = function(response){
resolve({ok:response.success, json:function(){
return Promise.resolve(JSON.parse(response.data));
}});
};

var scriptTag = document.creatElement('script');
var scriptTag = document.createElement('script');
scriptTag.async = true;
scriptTag.defer = true;
scriptTag.src = url + '?callback=$$$JSONP_CALLBACK';
scriptTag.src = sessionServiceEndpoint + url + '?callback='+callbackName;
document.body.appendChild(scriptTag);

setTimeout(function(){
resolve({ok:false, json:function(){
return Promise.resolve({});
}});
}, jsonpTimeout);
});
}




function request(url){
// if we don't have a session token cookie, don't bother..
if(document.cookie.indexOf('FTSession=') === -1){
return Promise.resolve(false);
}

var requestFunc = ('XDomainRequest' in window) ? jsonpRequest : fetchRequest;
return requestFunc(url).then(function(response){
if(response.ok){
return (url === '/validate') ? Promise.resolve(true) : response.json();
}else{
return Promise.resolve(false);
}

}).catch(function(e){
setTimeout(function(){
throw e;
}, 0);
});
}

module.exports = ('XDomainRequest' in window) ? jsonpRequest : fetchRequest;
module.exports = request;
22 changes: 22 additions & 0 deletions test/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>APP TEST</title>
<link rel="stylesheet" href="//build.origami.ft.com/bundles/css?modules=o-buttons@^2.0.3" />

</head>
<body>

<div class="buttons">
<button id="details" data-method="details" class="o-buttons o-buttons--big">Session Details</button>
<button id="uuid" data-method="uuid" class="o-buttons o-buttons--big">Get UUID</button>
<button id="validate" data-method="validate" class="o-buttons o-buttons--big">Validate Session</button>
</div>

<div class="results">

</div>
<script src="bundle.js"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions test/app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var session = require('../../main');

function handleClick(e){
var target = e.target;
var method = target.getAttribute('data-method');
console.log('Call method ' + method);
session[method]().then(function(response){
console.log(response);
});
}

function addClick(el){
console.log('add click to', el);
el.addEventListener('click', handleClick);
}

[].slice.call(document.querySelectorAll('.buttons button'), 0).forEach(addClick);
Loading

0 comments on commit 319084a

Please sign in to comment.