Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
fix(datepicker): throw error if model is not a Date. For #5266
Browse files Browse the repository at this point in the history
  • Loading branch information
jelbourn committed Nov 4, 2015
1 parent 27d1867 commit 71976be
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/components/datepicker/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,15 @@

var self = this;
ngModelCtrl.$render = function() {
self.date = self.ngModelCtrl.$viewValue;
self.inputElement.value = self.dateLocale.formatDate(self.date);
var value = self.ngModelCtrl.$viewValue;

if (value && !(value instanceof Date)) {
throw Error('The ng-model for md-datepicker must be a Date instance. ' +
'Currently the model is a: ' + (typeof value));
}

self.date = value;
self.inputElement.value = self.dateLocale.formatDate(value);
self.resizeInputElement();
self.setErrorFlags();
};
Expand Down
19 changes: 19 additions & 0 deletions src/components/datepicker/datePicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ describe('md-date-picker', function() {
expect(controller.inputElement.placeholder).toBe('Fancy new placeholder');
});

it('should throw an error when the model is not a date', function() {
expect(function() {
pageScope.myDate = '2015-01-01';
pageScope.$apply();
}).toThrowError('The ng-model for md-datepicker must be a Date instance. ' +
'Currently the model is a: string');
});

it('should support null and undefined values', function() {
expect(function() {
pageScope.myDate = null;
pageScope.$apply();

pageScope.myDate = undefined;
pageScope.$apply();

}).not.toThrow();
});

it('should throw an error when inside of md-input-container', function() {
var template =
'<md-input-container>' +
Expand Down

0 comments on commit 71976be

Please sign in to comment.