From 2962a2d89a2e670a6da783047c9469b1a8b6ac67 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Mon, 23 Mar 2015 21:22:25 -0700 Subject: [PATCH] fix(timepicker): move render logic to formatter - Moves render logic converting model values into date or null object into a formatter, which allows the render function to more correctly render using the viewValue - remove parentheses as per CR --- src/timepicker/test/timepicker.spec.js | 34 ++++++++++++++++++++++++++ src/timepicker/timepicker.js | 6 ++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/timepicker/test/timepicker.spec.js b/src/timepicker/test/timepicker.spec.js index a73bcef322..f1a83d3147 100644 --- a/src/timepicker/test/timepicker.spec.js +++ b/src/timepicker/test/timepicker.spec.js @@ -101,6 +101,10 @@ describe('timepicker directive', function () { expect(getModelState()).toEqual([14, 40]); }); + it('should be pristine', function() { + expect(element.controller('ngModel').$pristine).toBe(true); + }); + it('has `selected` current time when model is initially cleared', function() { $rootScope.time = null; element = $compile('')($rootScope); @@ -800,6 +804,36 @@ describe('timepicker directive', function () { }); }); + describe('$formatter', function () { + var ngModel, + date; + + beforeEach(function () { + ngModel = element.controller('ngModel'); + date = new Date('Mon Mar 23 2015 14:40:11 GMT-0700 (PDT)'); + }); + + it('should have one formatter', function () { + expect(ngModel.$formatters.length).toBe(1); + }); + + it('should convert a date to a new reference representing the same date', function () { + expect(ngModel.$formatters[0](date)).toEqual(date); + }); + + it('should convert a valid date string to a date object', function () { + expect(ngModel.$formatters[0]('Mon Mar 23 2015 14:40:11 GMT-0700 (PDT)')).toEqual(date); + }); + + it('should set falsy values as null', function () { + expect(ngModel.$formatters[0](undefined)).toBe(null); + expect(ngModel.$formatters[0](null)).toBe(null); + expect(ngModel.$formatters[0]('')).toBe(null); + expect(ngModel.$formatters[0](0)).toBe(null); + expect(ngModel.$formatters[0](NaN)).toBe(null); + }); + }); + describe('user input validation', function () { var changeInputValueTo; diff --git a/src/timepicker/timepicker.js b/src/timepicker/timepicker.js index ee69d3f86c..928e55051d 100644 --- a/src/timepicker/timepicker.js +++ b/src/timepicker/timepicker.js @@ -20,6 +20,10 @@ angular.module('ui.bootstrap.timepicker', []) ngModelCtrl = ngModelCtrl_; ngModelCtrl.$render = this.render; + ngModelCtrl.$formatters.unshift(function (modelValue) { + return modelValue ? new Date( modelValue ) : null; + }); + var hoursInputEl = inputs.eq(0), minutesInputEl = inputs.eq(1); @@ -217,7 +221,7 @@ angular.module('ui.bootstrap.timepicker', []) }; this.render = function() { - var date = ngModelCtrl.$modelValue ? new Date( ngModelCtrl.$modelValue ) : null; + var date = ngModelCtrl.$viewValue; if ( isNaN(date) ) { ngModelCtrl.$setValidity('time', false);