-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1214 Update date-picker in vacation form & add date-to-moment directive See merge request linagora/lgs/openpaas/linagora.esn.unifiedinbox!495
- Loading branch information
Showing
3 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(function(angular) { | ||
angular.module('linagora.esn.unifiedinbox') | ||
.directive('dateToMoment', dateToMoment); | ||
|
||
function dateToMoment(moment) { | ||
var directive = { | ||
restrict: 'A', | ||
require: 'ngModel', | ||
link: function(scope, element, attrs, ngModel) { | ||
ngModel.$parsers.unshift(ensureModelHasMoment); | ||
|
||
function ensureModelHasMoment(value) { | ||
var result = moment(value); | ||
|
||
return result.isValid() ? result : undefined; | ||
} | ||
} | ||
}; | ||
|
||
return directive; | ||
} | ||
})(angular); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
/* global chai: false */ | ||
|
||
var expect = chai.expect; | ||
|
||
describe('The dateToMoment directive', function() { | ||
beforeEach(function() { | ||
module('jadeTemplates'); | ||
angular.mock.module('esn.core'); | ||
angular.mock.module('linagora.esn.unifiedinbox'); | ||
|
||
inject(['$compile', '$rootScope', 'moment', function($c, $r, moment) { | ||
this.$compile = $c; | ||
this.$rootScope = $r; | ||
this.moment = moment; | ||
this.$scope = this.$rootScope.$new(); | ||
|
||
this.initDirective = function(scope) { | ||
var html = '<input ng-model="event.end" date-to-moment/>'; | ||
var element = this.$compile(html)(scope); | ||
|
||
scope.$digest(); | ||
|
||
return element; | ||
}; | ||
}]); | ||
}); | ||
|
||
it('should return a moment', function() { | ||
this.$scope.event = {}; | ||
var element = this.initDirective(this.$scope); | ||
var parser = element.controller('ngModel').$parsers[0]; | ||
|
||
expect(moment.isMoment(parser('2015-07-03 10:30'))).to.be.true; | ||
}); | ||
|
||
describe('comportment for invalid date', function() { | ||
/* global moment: false */ | ||
|
||
beforeEach(function() { | ||
moment.suppressDeprecationWarnings = true; | ||
}); | ||
|
||
it('should return undefined for invalid date', function() { | ||
this.$scope.event = { | ||
allDay: false | ||
}; | ||
var element = this.initDirective(this.$scope); | ||
var parser = element.controller('ngModel').$parsers[0]; | ||
|
||
expect(parser('this is a bad date')).to.be.undefined; | ||
}); | ||
|
||
afterEach(function() { | ||
moment.suppressDeprecationWarnings = false; | ||
}); | ||
}); | ||
}); |