2
Fork 0

wip: made karma test running

This commit is contained in:
Willem 2016-04-22 10:34:11 +02:00
parent eff1b95b32
commit 3e1ddaf6eb
15 changed files with 360 additions and 99 deletions

View file

@ -1,3 +0,0 @@
'use strict';
require('mocha-jshint')(['es5-ff-spa-loader.js','example']);

3
test/jshint.spec.js.bak Normal file
View file

@ -0,0 +1,3 @@
'use strict';
//require('mocha-jshint')(['es5-ff-spa-loader.js','example']);

50
test/karma.conf.js Normal file
View file

@ -0,0 +1,50 @@
module.exports = function(config) {
config.set({
basePath: '..',
frameworks: ['jasmine', 'requirejs'],
files: [{
pattern: 'es5-ff-spa-loader.js',
included: false
}, {
pattern: 'test/spec/**/*.js',
included: false
},
'test/spec-runner.js'],
preprocessors: {
'es5-ff-spa-loader.js': ['coverage']
},
reporters: ['coverage', 'mocha', 'junit'],
// the default configuration
junitReporter: {
outputDir: 'test/data/coverage', // results will be saved as $outputDir/$browserName.xml
outputFile: undefined, // if included, results will be saved as $outputDir/$browserName/$outputFile
suite: '', // suite will become the package name attribute in xml testsuite element
useBrowserName: false, // add browser name to report and classes names
nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element,
properties: {} // key value pair of properties to add to the <properties> section of the report
},
plugins: [
'karma-requirejs',
'karma-coverage',
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-mocha-reporter',
'karma-junit-reporter'
],
coverageReporter: {
// specify a common output directory
dir: 'test/data/coverage',
reporters: [
{ type: 'lcov', subdir: 'report-lcov' },
{ type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' }
]
},
browsers: ['PhantomJS']
});
};

26
test/spec-runner.js Normal file
View file

@ -0,0 +1,26 @@
var testModules = [];
var TEST_REGEXP = /^\/base\/test\/spec\/\S*.js$/i;
var pathToModule = function(path) {
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};
testModules.push('es5-ff-spa-loader');
Object.keys(window.__karma__.files).forEach(function(file) {
//console.log('file: '+file);
if (TEST_REGEXP.test(file)) {
var fileNew = pathToModule(file);
testModules.push(fileNew);
//console.log('file: '+file+' fileNew: '+fileNew);
}
});
require.config({
baseUrl : '/base/',
enforceDefine : true,
xhtml : false,
waitSeconds : 30,
deps : testModules,
callback : window.__karma__.start
});

33
test/spec/test-boot.js Normal file
View file

@ -0,0 +1,33 @@
'use strict';
define(['es5-ff-spa-loader'], function(FFSpaLoader) {
describe('Start loader', function() {
it('FFSpaLoader start with error', function(done) {
FFSpaLoader.options.server.url = 'http://localhost:9999';
FFSpaLoader.options.error.handler = function(err) {
done();
};
FFSpaLoader.start();
});
it('FFSpaLoader starts', function(done) {
FFSpaLoader.options.server.url = 'http://localhost:9090/test';
FFSpaLoader.options.server.assets = '/static/spa-client-resources';
FFSpaLoader.options.error.handler = function(err) {
fail(); // TIODI
};
FFSpaLoader.start(function() {
done();
});
});
it('FFSpaLoader starts again', function(done) {
FFSpaLoader.options.server.url = 'http://localhost:9090/test';
FFSpaLoader.options.server.assets = '/static/spa-client-resources';
FFSpaLoader.options.error.handler = function(err) {
fail(); // TIODI
};
FFSpaLoader.start(function() {
done();
});
});
});
});

37
test/spec/test-exports.js Normal file
View file

@ -0,0 +1,37 @@
'use strict';
define(['es5-ff-spa-loader'], function(FFSpaLoader) {
describe('Check module exports', function() {
describe('Check undefined', function() {
it('FFSpaLoader.options should be defined', function() {
expect(FFSpaLoader.options === undefined).toEqual(false);
});
it('FFSpaLoader.factory should be defined', function() {
expect(FFSpaLoader.factory === undefined).toEqual(false);
});
it('FFSpaLoader.start should be defined', function() {
expect(FFSpaLoader.start === undefined).toEqual(false);
});
it('FFSpaLoader.clearServerUrl should be defined', function() {
expect(FFSpaLoader.clearServerUrl === undefined).toEqual(false);
});
it('FFSpaLoader.clearCache should be defined', function() {
expect(FFSpaLoader.clearCache === undefined).toEqual(false);
});
});
describe('Check function', function() {
it('FFSpaLoader.start should be function', function() {
expect(typeof FFSpaLoader.start === 'function').toEqual(true);
});
it('FFSpaLoader.clearServerUrl should be function', function() {
expect(typeof FFSpaLoader.clearServerUrl === 'function').toEqual(true);
});
it('FFSpaLoader.clearCache should be function', function() {
expect(typeof FFSpaLoader.clearCache === 'function').toEqual(true);
});
});
});
});

21
test/spec/test-factory.js Normal file
View file

@ -0,0 +1,21 @@
'use strict';
define(['es5-ff-spa-loader'], function(FFSpaLoader) {
describe('Check factory detect', function() {
it('FFSpaLoader.factory.detect.localStorage', function() {
expect(FFSpaLoader.factory.detect.localStorage()).toEqual(true);
});
it('FFSpaLoader.factory.detect.openDatabase', function() {
expect(FFSpaLoader.factory.detect.openDatabase()).toEqual(true);
});
it('FFSpaLoader.factory.detect.sqlitePlugin', function() {
expect(FFSpaLoader.factory.detect.sqlitePlugin()).toEqual(false);
});
it('FFSpaLoader.factory.detect.cordova', function() {
expect(FFSpaLoader.factory.detect.cordova()).toEqual(false);
});
it('FFSpaLoader.factory.detect.cordovaDevice', function() {
expect(FFSpaLoader.factory.detect.cordovaDevice()).toEqual(false);
});
});
});

View file

@ -1,32 +0,0 @@
'use strict';
var assert = require('assert');
var FFSpaLoader = require('../es5-ff-spa-loader');
describe('Check module exports', function() {
it('FFSpaLoader.options should be defined', function() {
assert.equal(false, FFSpaLoader.options === undefined);
});
it('FFSpaLoader.factory should be defined', function() {
assert.equal(false, FFSpaLoader.factory === undefined);
});
it('FFSpaLoader.start should be defined', function() {
assert.equal(false, FFSpaLoader.start === undefined);
});
it('FFSpaLoader.start should be function', function() {
assert.equal(true, typeof FFSpaLoader.start === 'function');
});
it('FFSpaLoader.clearServerUrl should be defined', function() {
assert.equal(false, FFSpaLoader.clearServerUrl === undefined);
});
it('FFSpaLoader.clearServerUrl should be function', function() {
assert.equal(true, typeof FFSpaLoader.clearServerUrl === 'function');
});
it('FFSpaLoader.clearCache should be defined', function() {
assert.equal(false, FFSpaLoader.clearCache === undefined);
});
it('FFSpaLoader.clearCache should be function', function() {
assert.equal(true, typeof FFSpaLoader.clearCache === 'function');
});
});

View file

@ -1,23 +0,0 @@
'use strict';
var assert = require('assert');
var FFSpaLoader = require('../es5-ff-spa-loader');
describe('Check factory detect', function() {
it('FFSpaLoader.factory.detect.localStorage', function() {
assert.equal(false, FFSpaLoader.factory.detect.localStorage());
});
it('FFSpaLoader.factory.detect.openDatabase', function() {
assert.equal(false, FFSpaLoader.factory.detect.openDatabase());
});
it('FFSpaLoader.factory.detect.sqlitePlugin', function() {
assert.equal(false, FFSpaLoader.factory.detect.sqlitePlugin());
});
it('FFSpaLoader.factory.detect.cordova', function() {
assert.equal(false, FFSpaLoader.factory.detect.cordova());
});
it('FFSpaLoader.factory.detect.cordovaDevice', function() {
assert.equal(false, FFSpaLoader.factory.detect.cordovaDevice());
});
});

142
test/test-server.js Normal file
View file

@ -0,0 +1,142 @@
'use strict';
var httpPort = 9090;
var express = require('express');
var path = require('path');
var fs = require('fs');
var fetch = require('fetch');
var cors = require('cors');
var morgan = require('morgan');
var UglifyJS = require("uglify-js");
var Hashes = require('jshashes');
var minify = require('minify');
var clientResourcesWeb = [];
var clientResources = {
js: [],
css: [],
dss: []
};
var addClientResource = function(clientResource, resourceType) {
clientResources[resourceType].push(clientResource);
};
var fetchHashResource = function(fetchEntry,cb) {
var serverUrl = 'http://localhost:'+httpPort;
var hashDigest = new Hashes.SHA1;
fetch.fetchUrl(serverUrl + fetchEntry.url,function(err, meta, data) {
if (err !== null) { return cb(err); }
var assetHash = hashDigest.hex(''+data);
clientResourcesWeb.push({
url: fetchEntry.url,
type: fetchEntry.type,
hash: assetHash
});
cb(null);
});
};
var fetchHashResources = function(fetchList, cb) {
var resourceStack = fetchList;
var resourceLoader = function() {
resourceStack = resourceStack.slice(1);
if (resourceStack.length === 0) {
cb(null);
} else {
fetchHashResource(resourceStack[0],resourceLoader);
}
};
fetchHashResource(resourceStack[0],resourceLoader);
};
var createClientResourceFetchList = function() {
var fetchList = [];
for (var clientResourceIdxJs in clientResources.js) {
var urlJs = clientResources.js[clientResourceIdxJs];
fetchList.push({url:urlJs,type:'js'});
}
for (var clientResourceIdxCss in clientResources.css) {
var urlCss = clientResources.css[clientResourceIdxCss];
fetchList.push({url:urlCss,type:'css'});
}
for (var clientResourceIdxCssData in clientResources.dss) {
var urlCssData = clientResources.cssData[clientResourceIdxCssData];
fetchList.push({url:urlCssData,type:'dss'});
}
return fetchList;
};
function renderTemplatePath(viewPath) {
return function (req, res) {
res.locals.query = req.query;
var qi = req.url.indexOf('?');
if (qi === -1) {
qi = req.url.length;
}
res.render(viewPath + req.url.substring(req.route.path.length-1, qi));
};
}
function renderIndex() {
return function (req, res) {
var inlineScript = UglifyJS.minify(__dirname+'/../../es5-ff-spa-loader.js');
minify(__dirname+'/../../es5-ff-spa-loader.css', {}, function(err, data) {
res.render('index', {
inlineScript: inlineScript.code,
inlineStyle: data
});
});
};
}
// Add resources ORDERED per type
addClientResource('/static/module/jquery/jquery.js','js');
//addClientResource('/static/module/angular/angular.js','js');
//addClientResource('/static/module/angular-route/angular-route.js','js');
addClientResource('/static/module/bootstrap/css/bootstrap.css','css');
addClientResource('/static/module/bootstrap/js/bootstrap.js','js');
//addClientResource('/static/css/boot.css','css');
//addClientResource('/static/css/style.css','css');
//addClientResource('/static/js/example-app.js','js'); // deps: jquery,angular
//addClientResource('/static/js/controller/page-bar.js','js'); // deps: example-app.js
//addClientResource('/static/js/controller/page-foo.js','js');
//addClientResource('/static/js/controller/page-index.js','js');
var appPath = '/test';
var server = express();
server.use(morgan('dev'));
server.use(cors({credentials: true, origin: '*', exposedHeaders: ['X-My-Api']}));
server.set('view engine', 'ejs');
server.set('views', path.join(__dirname,'www_views'));
server.use(function(req, res, next) { res.header('X-My-Api', 'noknok');next(); });
server.use(appPath+'/static', express.static(path.join(__dirname,'www_static')));
server.use(appPath+'/static/module/bootstrap', express.static(path.join(__dirname,'../node_modules/bootstrap/dist')));
server.use(appPath+'/static/module/jquery', express.static(path.join(__dirname,'../node_modules/jquery/dist')));
server.use(appPath+'/static/module/angular', express.static(path.join(__dirname,'../node_modules/angular')));
server.use(appPath+'/static/module/angular-route', express.static(path.join(__dirname,'../node_modules/angular-route')));
server.get(appPath+'/static/spa-client-resources', function (req,res) {res.json({data: {resources: clientResourcesWeb}});});
server.get(appPath+'/static/spa-loader.css', function (req,res) {res.sendFile('es5-ff-spa-loader.css', { root: path.join(__dirname, '/../../') });});
server.get(appPath+'/', function (req, res) {res.redirect(appPath+'/example-ui');});
server.get(appPath+'/example-ui/thtml/*', renderTemplatePath('thtml/'));
server.get(appPath+'/example-ui', renderIndex());
server.get('/', function (req, res) {res.redirect(appPath);});
var serverPort = null;
module.exports={
start: function(cb) {
serverPort = server.listen(httpPort);
console.info('Server started on port '+httpPort);
var res = createClientResourceFetchList();
fetchHashResources(res, function(err) {
cb(err);
console.log('Total assets build: '+clientResourcesWeb.length);
});
},
stop: function(cb) {
serverPort.close(cb);
}
};