From 497883024c4c9a14a645b19959a24c5a005e790b Mon Sep 17 00:00:00 2001 From: Jason Calabrese Date: Thu, 13 Aug 2015 23:44:29 -0700 Subject: [PATCH] added most basic test for profileeditor needed to replace new Option() with basic jQuery append since Option wasn't working in the test --- static/profile/js/profile.js | 35 ++++++--- tests/profileeditor.test.js | 136 +++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+), 8 deletions(-) create mode 100644 tests/profileeditor.test.js diff --git a/static/profile/js/profile.js b/static/profile/js/profile.js index 8ab5d43b511..10162522df9 100644 --- a/static/profile/js/profile.js +++ b/static/profile/js/profile.js @@ -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; @@ -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(''); }); $('#pe_form').find('button').click(profileSubmit); @@ -225,17 +229,24 @@ var tr = $(''); var select = $('').attr('id',e.prefix+'_val_'+i).attr('value',c_profile[e.array][i].value))); var icons_td = $('').append($('').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($('').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); @@ -281,13 +292,21 @@ var tr = $(''); var select = $('').attr('id','pe_targetbg_low_'+i).attr('value',c_profile.target_low[i].value))); tr.append($('').append('High : ').append($('').attr('id','pe_targetbg_high_'+i).attr('value',c_profile.target_high[i].value))); diff --git a/tests/profileeditor.test.js b/tests/profileeditor.test.js new file mode 100644 index 00000000000..99366429c9a --- /dev/null +++ b/tests/profileeditor.test.js @@ -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(); + }); + +});