From 5a57a8f2f5b0ead6c8fba3eed5e54c11097570fb Mon Sep 17 00:00:00 2001 From: Michael Anderton Date: Sat, 22 Nov 2014 18:42:54 -0700 Subject: [PATCH 1/3] Added clockDisplay tests --- app/static/js/app.js | 1 + app/static/tests/clock-tests.js | 55 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 app/static/tests/clock-tests.js diff --git a/app/static/js/app.js b/app/static/js/app.js index f87660f..23d08b2 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -10,6 +10,7 @@ angular.module('JobDashClock', ['ngMaterial']) * */ var self = this; + phones = 3; self.time = "Loading ..."; self.tickInterval = 1000; // ms diff --git a/app/static/tests/clock-tests.js b/app/static/tests/clock-tests.js new file mode 100644 index 0000000..f59ff0e --- /dev/null +++ b/app/static/tests/clock-tests.js @@ -0,0 +1,55 @@ +// describe('TimeCtrl', function(){ + +// beforeEach(module('JobDashClock')); + + +// it('should have a phones variable = 3', inject(function($controller) { +// var scope = {}, +// ctrl = new $controller('TimeCtrl', {$scope:scope});; + +// expect(phones).toBe(3); +// })); + +// }); +describe('filter: clockDisplay', function() { + beforeEach(module('JobDashClock')); + + beforeEach(inject(function(_clockDisplayFilter_) { + clockDisplayFilter = _clockDisplayFilter_; + })); + + it('should create a formatted string from a date or moment object', function() { + // if the value is a string, it should be unchanged + expect(clockDisplayFilter('November 22nd 2014, 6:05:42 pm')).toBe('November 22nd 2014, 6:05:42 pm'); + // if the value is a Moment object, it should return a formated string + expect(clockDisplayFilter(moment('2013-02-08 09:30'))).toBe('February 8th 2013, 9:30:00 am'); + // if the value is a Date object, it should return a formated string + var testTime = new Date(2013, 01, 08, 9, 30); + expect(clockDisplayFilter(testTime)).toBe('February 8th 2013, 9:30:00 am'); + }); + + +}); + + + +// describe('directive: datetimePicker', function() { + +// beforeEach(module('JobDashClock')); + +// beforeEach(inject(function(_$rootScope_, _$compile_, _$httpBackend_) { +// $rootScope = _$rootScope_; +// $compile = _$compile_; +// $httpBackend = _$httpBackend_; +// scope = $rootScope; +// })); + +// it('should fill the date form with the current date', function() { +// $httpBackend.when('GET', '/static/partials/datetime-picker.html').respond('yes'); +// element = $compile('')(scope) +// $rootScope.$digest(); +// console.log(element); +// // console.log(element.find('input').attr()) + +// }); +// }); From 2cdbc86063d9eb72a32e09820e769e3e619c7be4 Mon Sep 17 00:00:00 2001 From: Michael Anderton Date: Mon, 24 Nov 2014 21:19:20 -0700 Subject: [PATCH 2/3] directive and controller test added --- README.md | 2 +- app/static/index.html | 2 +- app/static/js/app.js | 7 ++-- app/static/tests/clock-tests.js | 70 +++++++++++++++++++-------------- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 15bb1f0..e4d6551 100644 --- a/README.md +++ b/README.md @@ -79,4 +79,4 @@ The following pages are useful introductions to testing in AngularJS. - [Angular tips](http://angular-tips.com/blog/categories/unit-test/) - [Angular Recipes](http://fdietz.github.io/recipes-with-angular-js/directives/testing-directives.html) - [ng-newsletter](http://www.ng-newsletter.com/advent2013/#!/day/19) -- [Core Angular Docs](https://docs.angularjs.org/guide/unit-testing) +- [Core Angular Docs](https://docs.angularjs.org/guide/unit-testing) \ No newline at end of file diff --git a/app/static/index.html b/app/static/index.html index be4712d..174dcdc 100644 --- a/app/static/index.html +++ b/app/static/index.html @@ -51,4 +51,4 @@

Alarm at {{ alarm.value | clockDisplay }}

- + \ No newline at end of file diff --git a/app/static/js/app.js b/app/static/js/app.js index 23d08b2..789aac8 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -10,10 +10,9 @@ angular.module('JobDashClock', ['ngMaterial']) * */ var self = this; - phones = 3; self.time = "Loading ..."; self.tickInterval = 1000; // ms - + // self.phones = 3; self.tick = function() { self.time = moment() // get the current time $timeout(self.tick, self.tickInterval); // reset the timer @@ -25,6 +24,7 @@ angular.module('JobDashClock', ['ngMaterial']) self.alarms = []; self.newAlarm = new Date(); + self.alarmTest = false; self.addAlarm = function () { var alarm = moment(self.newAlarm); @@ -33,7 +33,8 @@ angular.module('JobDashClock', ['ngMaterial']) if (moment().isBefore(alarm)) { alertTimer = $timeout( function() { - alert('Alarm!') + alert('Alarm!'); + self.alarmTest = true; }, alarm.diff(moment()) // ms between the alarm time and now ) diff --git a/app/static/tests/clock-tests.js b/app/static/tests/clock-tests.js index f59ff0e..72dc88d 100644 --- a/app/static/tests/clock-tests.js +++ b/app/static/tests/clock-tests.js @@ -1,55 +1,65 @@ -// describe('TimeCtrl', function(){ - -// beforeEach(module('JobDashClock')); +describe('controller: TimeCtrl', function(){ + beforeEach(module('JobDashClock')); -// it('should have a phones variable = 3', inject(function($controller) { -// var scope = {}, -// ctrl = new $controller('TimeCtrl', {$scope:scope});; + it('should create an alarm that will activate in two seconds and then remove said alarm', inject(function($controller) { + var scope = {}, + ctrl = new $controller('TimeCtrl', {$scope:scope}); + // testAlarm = new Date(); + testTime = moment(); + ctrl.newAlarm = moment(); + ctrl.newAlarm.add(2, 'seconds'); //creates an alarm that will activate two seconds after creation + ctrl.addAlarm(); + expect(ctrl.alarms[0].value.format()).toBe(testTime.add(2, 'seconds').format()) + setTimeout(function() { expect(ctrl.newAlarm).toBe(true); }, 2010); //tests to see if the alarm has activated after two seconds. + })) +}) -// expect(phones).toBe(3); -// })); -// }); describe('filter: clockDisplay', function() { + beforeEach(module('JobDashClock')); beforeEach(inject(function(_clockDisplayFilter_) { clockDisplayFilter = _clockDisplayFilter_; - })); + })) it('should create a formatted string from a date or moment object', function() { + // if the value is a string, it should be unchanged - expect(clockDisplayFilter('November 22nd 2014, 6:05:42 pm')).toBe('November 22nd 2014, 6:05:42 pm'); + expect(clockDisplayFilter('February 8th 2013, 9:30:00 am')).toBe('February 8th 2013, 9:30:00 am'); + // if the value is a Moment object, it should return a formated string expect(clockDisplayFilter(moment('2013-02-08 09:30'))).toBe('February 8th 2013, 9:30:00 am'); + // if the value is a Date object, it should return a formated string var testTime = new Date(2013, 01, 08, 9, 30); expect(clockDisplayFilter(testTime)).toBe('February 8th 2013, 9:30:00 am'); - }); - -}); + }) +}) +describe('directive: datetimePicker', function() { -// describe('directive: datetimePicker', function() { + beforeEach(module('JobDashClock')); -// beforeEach(module('JobDashClock')); + beforeEach(inject(function(_$rootScope_, _$compile_, _$httpBackend_) { + $rootScope = _$rootScope_; + $compile = _$compile_; + $httpBackend = _$httpBackend_; + scope = $rootScope; + })); -// beforeEach(inject(function(_$rootScope_, _$compile_, _$httpBackend_) { -// $rootScope = _$rootScope_; -// $compile = _$compile_; -// $httpBackend = _$httpBackend_; -// scope = $rootScope; -// })); + it('should fill the date form with the current date', function() { + $httpBackend.when('GET', '/static/partials/datetime-picker.html').respond('yes'); + element = $compile('')(scope) + scope.$digest(); + console.log(element); + console.log(element.find('label')); + console.log(element.find('clock.newAlarm')); + console.log(element.find('applytime')); -// it('should fill the date form with the current date', function() { -// $httpBackend.when('GET', '/static/partials/datetime-picker.html').respond('yes'); -// element = $compile('')(scope) -// $rootScope.$digest(); -// console.log(element); -// // console.log(element.find('input').attr()) -// }); -// }); + }); +}); From cfcbf1cce4adeb72869b25d1faffdd482081abd8 Mon Sep 17 00:00:00 2001 From: Michael Anderton Date: Mon, 24 Nov 2014 21:23:08 -0700 Subject: [PATCH 3/3] removed an unnecessary line --- app/static/js/app.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/static/js/app.js b/app/static/js/app.js index 789aac8..e2be5f4 100644 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -12,7 +12,6 @@ angular.module('JobDashClock', ['ngMaterial']) var self = this; self.time = "Loading ..."; self.tickInterval = 1000; // ms - // self.phones = 3; self.tick = function() { self.time = moment() // get the current time $timeout(self.tick, self.tickInterval); // reset the timer