forked from nightscout/cgm-remote-monitor
-
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.
allow UI tests to re-use/share more code
Tighten up the boilerplate a bit.
- Loading branch information
Showing
6 changed files
with
218 additions
and
183 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
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,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; | ||
|
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
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
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
Oops, something went wrong.