Skip to content

Commit

Permalink
allow UI tests to re-use/share more code
Browse files Browse the repository at this point in the history
Tighten up the boilerplate a bit.
  • Loading branch information
bewest committed Aug 6, 2016
1 parent 30e9507 commit 0a77b90
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 183 deletions.
43 changes: 14 additions & 29 deletions tests/careportal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,25 @@ var nowData = {
describe('client', function ( ) {
var self = this;

var headless = require('./fixtures/headless')(benv, this);

before(function (done) {
benv.setup(function() {
self.$ = require('jquery');
self.$.localStorage = require('./fixtures/localstorage');

self.$.fn.tipsy = function mockTipsy ( ) { };

var indexHtml = read(__dirname + '/../static/index.html', 'utf8');
self.$('body').html(indexHtml);

var d3 = require('d3');
//disable all d3 transitions so most of the other code can run with jsdom
d3.timer = function mockTimer() { };

benv.expose({
$: self.$
, jQuery: self.$
, d3: d3
, io: {
connect: function mockConnect ( ) {
return {
on: function mockOn ( ) { }
};
}
}
});
done();
});
done( );
});

after(function (done) {
benv.teardown();
done();
done( );
});

beforeEach(function (done) {
headless.setup({ }, done);
});

afterEach(function (done) {
headless.teardown( );
done( );
});

it ('open careportal, and enter a treatment', function (done) {
var plugins = require('../lib/plugins/')().registerClientDefaults();
var client = require('../lib/client');
Expand Down
127 changes: 127 additions & 0 deletions tests/fixtures/headless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

var read = require('fs').readFileSync;
var _ = require('lodash');

function headless (benv, binding) {
var self = binding;
function root ( ) {
return benv;
}

function init (opts, callback) {
var localStorage = opts.localStorage || './localstorage';
var htmlFile = opts.htmlFile || __dirname + '/../../static/index.html';
var serverSettings = opts.serverSettings || require('./default-server-settings');
var someData = opts.mockAjax || { };
benv.setup(function() {
self.$ = require('jquery');
self.$.localStorage = require(localStorage);

self.$.fn.tipsy = function mockTipsy ( ) { };

var indexHtml = read(htmlFile, 'utf8');
self.$('body').html(indexHtml);

var d3 = require('d3');
//disable all d3 transitions so most of the other code can run with jsdom
d3.timer = function mockTimer() { };

if (opts.mockProfileEditor) {
self.$.plot = function mockPlot () {
};

self.$.fn.tipsy = function mockTipsy ( ) { };

self.$.fn.dialog = function mockDialog (opts) {
function maybeCall (name, obj) {
if (obj[name] && obj[name].call) {
obj[name]();
}

}
maybeCall('open', opts);

_.forEach(opts.buttons, function (button) {
maybeCall('click', button);
});
};
}
if (opts.mockSimpleAjax) {
someData = opts.mockSimpleAjax;
self.$.ajax = function mockAjax (url, opts) {
var returnVal = someData[url] || [];
if (opts && typeof opts.success === 'function') {
opts.success(returnVal);
}
return self.$.Deferred().resolveWith(returnVal);
};
}
if (opts.mockAjax) {
self.$.ajax = function mockAjax (url, opts) {
//logfile.write(url+'\n');
//console.log(url,opts);
if (opts && opts.success && opts.success.call) {
return {
done: function mockDone (fn) {
if (someData[url]) {
console.log('+++++Data for ' + url + ' sent');
opts.success(someData[url]);
} else {
console.log('-----Data for ' + url + ' missing');
opts.success([]);
}
fn();
return self.$.ajax();
},
fail: function mockFail () {
return self.$.ajax();
}
};
}
return {
done: function mockDone (fn) {
fn({message: 'OK'});
return self.$.ajax();
},
fail: function mockFail () {
return self.$.ajax();
}
};
};
}


benv.expose({
$: self.$
, jQuery: self.$
, d3: d3
, serverSettings: serverSettings
, io: {
connect: function mockConnect ( ) {
return {
on: function mockOn ( ) { }
};
}
}
});

var extraRequires = opts.benvRequires || [ ];
extraRequires.forEach(function (req) {
benv.require(req);
});
callback( );
});

}

function teardown ( ) {
benv.teardown();
}
root.setup = init;
root.teardown = teardown;

return root;
}

module.exports = headless;

20 changes: 20 additions & 0 deletions tests/hashauth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ var serverSettings = require('./fixtures/default-server-settings');

describe('hashauth', function ( ) {
var self = this;
var headless = require('./fixtures/headless')(benv, this);

before(function (done) {
done( );
});

after(function (done) {
done( );
});

beforeEach(function (done) {
headless.setup({ }, done);
});

afterEach(function (done) {
headless.teardown( );
done( );
});
/*
before(function (done) {
benv.setup(function() {
self.$ = require('jquery');
Expand Down Expand Up @@ -41,6 +60,7 @@ describe('hashauth', function ( ) {
benv.teardown();
done();
});
*/

it ('should make module unauthorized', function () {
var plugins = require('../lib/plugins/')().registerClientDefaults();
Expand Down
24 changes: 14 additions & 10 deletions tests/pluginbase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ require('should');
var benv = require('benv');

describe('pluginbase', function ( ) {
var headless = require('./fixtures/headless')(benv, this);

before(function (done) {
benv.setup(function() {
benv.expose({
$: require('jquery')
, jQuery: require('jquery')
});
done();
});
done( );
});

after(function (done) {
benv.teardown();
done();
done( );
});

beforeEach(function (done) {
headless.setup({ }, done);
});

afterEach(function (done) {
headless.teardown( );
done( );
});


it('does stuff', function() {

function div (clazz) {
Expand Down Expand Up @@ -51,4 +55,4 @@ describe('pluginbase', function ( ) {
majorPills.length.should.equal(1);
});

});
});
102 changes: 20 additions & 82 deletions tests/profileeditor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,94 +72,32 @@ var someData = {

describe('Profile editor', function ( ) {
var self = this;
var headless = require('./fixtures/headless')(benv, this);

before(function (done) {
benv.setup(function() {
self.$ = require('jquery');
self.$.localStorage = require('./fixtures/localstorage');

self.$.fn.tipsy = function mockTipsy ( ) { };

self.$.fn.dialog = function mockDialog (opts) {
function maybeCall (name, obj) {
if (obj[name] && obj[name].call) {
obj[name]();
}

}
maybeCall('open', opts);

_.forEach(opts.buttons, function (button) {
maybeCall('click', button);
});
};

var indexHtml = read(__dirname + '/../static/profile/index.html', 'utf8');
self.$('body').html(indexHtml);

//var filesys = require('fs');
//var logfile = filesys.createWriteStream('out.txt', { flags: 'a'} )

self.$.ajax = function mockAjax (url, opts) {
//logfile.write(url+'\n');
//console.log(url,opts);
if (opts && opts.success && opts.success.call) {
return {
done: function mockDone (fn) {
if (someData[url]) {
console.log('+++++Data for ' + url + ' sent');
opts.success(someData[url]);
} else {
console.log('-----Data for ' + url + ' missing');
opts.success([]);
}
fn();
return self.$.ajax();
},
fail: function mockFail () {
return self.$.ajax();
}
};
}
return {
done: function mockDone (fn) {
fn({message: 'OK'});
return self.$.ajax();
},
fail: function mockFail () {
return self.$.ajax();
}
};
};

var d3 = require('d3');
//disable all d3 transitions so most of the other code can run with jsdom
d3.timer = function mockTimer() { };

benv.expose({
$: self.$
, jQuery: self.$
, d3: d3
, serverSettings: serverSettings
, io: {
connect: function mockConnect ( ) {
return {
on: function mockOn ( ) { }
};
}
}
});
done( );
});

benv.require(__dirname + '/../bundle/bundle.source.js');
benv.require(__dirname + '/../static/profile/js/profileeditor.js');
after(function (done) {
done( );
});

done();
});
beforeEach(function (done) {
var opts = {
htmlFile: __dirname + '/../static/profile/index.html'
, mockProfileEditor: true
, mockAjax: someData
, benvRequires: [
__dirname + '/../bundle/bundle.source.js'
, __dirname + '/../static/profile/js/profileeditor.js'
]
};
headless.setup(opts, done);
});

after(function (done) {
benv.teardown(true);
done();
afterEach(function (done) {
headless.teardown( );
done( );
});

it ('should produce some html', function (done) {
Expand Down
Loading

0 comments on commit 0a77b90

Please sign in to comment.