Skip to content

Commit

Permalink
added most basic test for profileeditor
Browse files Browse the repository at this point in the history
needed to replace new Option() with basic jQuery append since Option wasn't working in the test
  • Loading branch information
jasoncalabrese committed Aug 14, 2015
1 parent 8cb893d commit 4978830
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 8 deletions.
35 changes: 27 additions & 8 deletions static/profile/js/profile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';


//for the tests window isn't the global object
var $ = window.$;
var _ = window._;
var moment = window.moment;
var Nightscout = window.Nightscout;

(function () {
var c_profile = null;
Expand Down Expand Up @@ -123,7 +127,7 @@
// Load timezones
$('#pe_timezone').empty();
moment.tz.names().forEach(function addTz(tz) {
$('#pe_timezone').append(new Option(tz,tz));
$('#pe_timezone').append('<option value="' + tz + '">' + tz + '</option>');
});

$('#pe_form').find('button').click(profileSubmit);
Expand Down Expand Up @@ -225,17 +229,24 @@
var tr = $('<tr>');
var select = $('<select>').attr('class','pe_selectabletime').attr('id',e.prefix+'_from_'+i);
var lowesttime=-1;
var selectedValue;
for (var t=0;t<48;t++) {
if (shouldAddTime(i,t,e.array)) {
if (lowesttime === -1) { lowesttime = t*30; }
var selected = toMinutesFromMidnight(c_profile[e.array][i].time) === t*30;
select.append(new Option(serverSettings.settings.timeFormat==='24' ? mmoltime[t] : mgtime[t], toTimeString(t*30),selected,selected));
if (lowesttime === -1) { lowesttime = t*30; }
var label = serverSettings.settings.timeFormat==='24' ? mmoltime[t] : mgtime[t];
var value = toTimeString(t*30);
if (toMinutesFromMidnight(c_profile[e.array][i].time) === t*30) {
selectedValue = value;
}
select.append('<option value="' + value + '">' + label + '</option>');
}
}
select.val(selectedValue);

tr.append($('<td>').append('From: ').append(select));
tr.append($('<td>').append(e.label).append($('<input type="text">').attr('id',e.prefix+'_val_'+i).attr('value',c_profile[e.array][i].value)));
var icons_td = $('<td>').append($('<img>').attr('class','addsingle').attr('style','cursor:pointer').attr('title','Add new interval before').attr('src',icon_add).attr('array',e.array).attr('pos',i));
if (c_profile[e.array].lenght>1) {
if (c_profile[e.array].length>1) {
icons_td.append($('<img>').attr('class','delsingle').attr('style','cursor:pointer').attr('title','Delete interval').attr('src',icon_remove).attr('array',e.array).attr('pos',i));
}
tr.append(icons_td);
Expand Down Expand Up @@ -281,13 +292,21 @@
var tr = $('<tr>');
var select = $('<select>').attr('class','pe_selectabletime').attr('id','pe_targetbg_from_'+i);
var lowesttime=-1;
var selectedValue;
for (var t=0;t<48;t++) {
if (shouldAddTime(i,t,'target_low')) {
if (lowesttime === -1) { lowesttime = t*30; }
var selected = toMinutesFromMidnight(c_profile.target_low[i].time) === t*30;
select.append(new Option(serverSettings.settings.timeFormat==='24' ? mmoltime[t] : mgtime[t], toTimeString(t*30),selected,selected));
var label = serverSettings.settings.timeFormat==='24' ? mmoltime[t] : mgtime[t];
var value = toTimeString(t*30);
console.info('>>>>>value', value);
if (toMinutesFromMidnight(c_profile.target_low[i].time) === t*30) {
selectedValue = value;
console.info('>>>>>selectedValue', selectedValue);
}
select.append('<option value="' + value + '">' + label + '</option>');
}
}
select.val(selectedValue);
tr.append($('<td>').append('From: ').append(select));
tr.append($('<td>').append('Low : ').append($('<input type="text">').attr('id','pe_targetbg_low_'+i).attr('value',c_profile.target_low[i].value)));
tr.append($('<td>').append('High : ').append($('<input type="text">').attr('id','pe_targetbg_high_'+i).attr('value',c_profile.target_high[i].value)));
Expand Down
136 changes: 136 additions & 0 deletions tests/profileeditor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
'use strict';

require('should');
var benv = require('benv');
var read = require('fs').readFileSync;
var serverSettings = require('./fixtures/default-server-settings');

var nowData = {
sgvs: [
{ mgdl: 100, mills: Date.now(), direction: 'Flat', type: 'sgv' }
]
};


var defaultprofile = {
//General values
'dia':3,

// Simple style values, 'from' are in minutes from midnight
'carbratio': [
{
'time': '00:00',
'value': 30
}],
'carbs_hr':30,
'delay': 20,
'sens': [
{
'time': '00:00',
'value': 17
}],
'startDate': new Date(),
'timezone': 'UTC',

//perGIvalues style values
'perGIvalues': false,
'carbs_hr_high': 30,
'carbs_hr_medium': 30,
'carbs_hr_low': 30,
'delay_high': 15,
'delay_medium': 20,
'delay_low': 20,

'basal':[
{
'time': '00:00',
'value': 0.1
}],
'target_low':[
{
'time': '00:00',
'value': 0
}],
'target_high':[
{
'time': '00:00',
'value': 0
}]
};
defaultprofile.startDate.setSeconds(0);
defaultprofile.startDate.setMilliseconds(0);


describe('profile editor', function ( ) {
var self = 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/profile/index.html', 'utf8');
self.$('body').html(indexHtml);

self.$.ajax = function mockAjax (url, opts) {
return {
done: function mockDone (fn) {
if (opts && opts.success && opts.success.call) {
opts.success([defaultprofile]);
}
fn();
}
};
};

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 ( ) { }
};
}
}
});

benv.require(__dirname + '/../bundle/bundle.source.js');
benv.require(__dirname + '/../static/profile/js/profile.js');

done();
});
});

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

it ('don\'t blow up', function (done) {
var plugins = require('../lib/plugins/')().registerClientDefaults();
var client = require('../lib/client');

var hashauth = require('../lib/hashauth');
hashauth.init(client,$);
hashauth.verifyAuthentication = function mockVerifyAuthentication(next) {
hashauth.authenticated = true;
next(true);
};


client.init(serverSettings, plugins);
client.dataUpdate(nowData);

done();
});

});

1 comment on commit 4978830

@MilosKozak
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you should make this page plugin first because you'll need to rewrite tests after change
4978830#diff-701fa5ed63f8c62d599232a36b18f37bR74
this will probably not work after refactoring as we expect to have only fragments there

Please sign in to comment.