else {
scope.dtparts = {date: '', time: ''};
}
+ validate();
};
function updateParent() {
- var incompleteDateTime = _.isEmpty(scope.dtparts.date) ^ _.isEmpty(scope.dtparts.time);
- ngModel.$setValidity('incompleteDateTime', !incompleteDateTime);
+ validate();
if (_.isEmpty(scope.dtparts.date) && _.isEmpty(scope.dtparts.time)) {
ngModel.$setViewValue(' ');
scope.$watch('dtparts.date', updateParent);
scope.$watch('dtparts.time', updateParent);
+ function validate() {
+ var incompleteDateTime = _.isEmpty(scope.dtparts.date) ^ _.isEmpty(scope.dtparts.time);
+ ngModel.$setValidity('incompleteDateTime', !incompleteDateTime);
+ }
+
function updateRequired() {
scope.required = scope.$parent.$eval(attrs.ngRequired);
}
--- /dev/null
+'use strict';
+
+describe('crmMailingRadioDate', function() {
+
+ beforeEach(function() {
+ module('crmResource');
+ module('crmUtil');
+ module('crmMailing');
+ });
+
+ var standardMarkup = '<form name="myForm">' +
+ ' <div crm-mailing-radio-date="mySchedule" ng-model="model.the_date" name="myRadioDate">' +
+ ' <input ng-model="mySchedule.mode" type="radio" name="send" value="now" class="radio-now" />' +
+ ' <input ng-model="mySchedule.mode" type="radio" name="send" value="at" class="radio-at" />' +
+ ' <span crm-ui-date-time ng-model="mySchedule.datetime" ng-required="mySchedule.mode == \'at\'"/>' +
+ ' </div>' +
+ '</form>';
+
+ describe('crmMailingRadioDate directive', function() {
+ var $compile,
+ $rootScope,
+ $interval,
+ $timeout,
+ model,
+ element;
+
+ beforeEach(inject(function(_$compile_, _$rootScope_, _$interval_, _$timeout_) {
+ $compile = _$compile_;
+ $rootScope = _$rootScope_;
+ $interval = _$interval_;
+ $timeout = _$timeout_;
+
+ $rootScope.model = model = {
+ the_date: ''
+ };
+ }));
+
+ it('should update the UI after changing the model', function() {
+ element = $compile(standardMarkup)($rootScope);
+
+ model.the_date = '';
+ $rootScope.$digest();
+ expect($rootScope.myForm.$valid).toBe(true);
+ expect(element.find('.radio-now').prop('checked')).toBe(true);
+ expect(element.find('.radio-at').prop('checked')).toBe(false);
+ expect(element.find('.dateplugin').datepicker('getDate')).toBe(null);
+ expect(element.find('.hasTimeEntry').timeEntry('getTime')).toBe(null);
+
+ model.the_date = ' ';
+ $rootScope.$digest();
+ expect($rootScope.myForm.$valid).toBe(false);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+ expect(element.find('.dateplugin').datepicker('getDate')).toBe(null);
+ expect(element.find('.hasTimeEntry').timeEntry('getTime')).toBe(null);
+
+ model.the_date = '2014-01-01 ';
+ $rootScope.$digest();
+ expect($rootScope.myForm.$valid).toBe(false);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+ expect(element.find('.dateplugin').datepicker('getDate').toDateString()).toEqual('Wed Jan 01 2014');
+ expect(element.find('.hasTimeEntry').timeEntry('getTime')).toBe(null);
+
+ model.the_date = ' 02:03:04';
+ $rootScope.$digest();
+ expect($rootScope.myForm.$valid).toBe(false);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+ expect(element.find('.dateplugin').datepicker('getDate')).toBe(null);
+ expect(element.find('.hasTimeEntry').timeEntry('getTime').getMinutes()).toBe(3);
+
+ model.the_date = '2014-01-02 02:03:04';
+ $rootScope.$digest();
+ expect($rootScope.myForm.$valid).toBe(true);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+ expect(element.find('.dateplugin').datepicker('getDate').toDateString()).toEqual('Thu Jan 02 2014');
+ expect(element.find('.hasTimeEntry').timeEntry('getTime').getMinutes()).toBe(3);
+ });
+
+ it('should update the model after changing the date and time', function() {
+ element = $compile(standardMarkup)($rootScope);
+ model.the_date = '';
+ $rootScope.$digest();
+ expect($rootScope.myForm.$valid).toBe(true);
+ expect(element.find('.radio-now').prop('checked')).toBe(true);
+ expect(element.find('.radio-at').prop('checked')).toBe(false);
+
+ element.find('.dateplugin').datepicker('setDate', '2014-01-03').trigger('change');
+ $rootScope.$digest();
+ expect(model.the_date).toBe('2014-01-03 ');
+ expect($rootScope.myForm.$valid).toBe(false);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+
+ element.find('.hasTimeEntry').timeEntry('setTime', '04:05').trigger('change');
+ $rootScope.$digest();
+ expect(model.the_date).toBe('2014-01-03 04:05');
+ expect($rootScope.myForm.$valid).toBe(true);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+
+ element.find('.dateplugin').datepicker('setDate', '').trigger('change');
+ $rootScope.$digest();
+ expect(model.the_date).toBe(' 04:05');
+ expect($rootScope.myForm.$valid).toBe(false);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+
+ element.find('.radio-now').click().trigger('click').trigger('change');
+ $rootScope.$digest();
+ expect(model.the_date).toBe(null);
+ expect($rootScope.myForm.$valid).toBe(true);
+ expect(element.find('.radio-now').prop('checked')).toBe(true);
+ expect(element.find('.radio-at').prop('checked')).toBe(false);
+ });
+ });
+});