2 /* global $, CRM:true */
4 describe('crmMailingRadioDate', function() {
6 beforeEach(function() {
12 var standardMarkup
= '<form name="myForm">' +
13 ' <div crm-mailing-radio-date="mySchedule" ng-model="model.the_date" name="myRadioDate">' +
14 ' <input ng-model="mySchedule.mode" type="radio" name="send" value="now" class="radio-now" />' +
15 ' <input ng-model="mySchedule.mode" type="radio" name="send" value="at" class="radio-at" />' +
16 ' <input crm-ui-datepicker ng-model="mySchedule.datetime" ng-required="mySchedule.mode == \'at\'"/>' +
20 describe('crmMailingRadioDate directive', function() {
28 beforeEach(inject(function(_
$compile_
, _
$rootScope_
, _
$interval_
, _
$timeout_
) {
29 $compile
= _
$compile_
;
30 $rootScope
= _
$rootScope_
;
31 $interval
= _
$interval_
;
32 $timeout
= _
$timeout_
;
34 // Global settings needed for crmUiDatepicker
36 CRM
.config
= CRM
.config
|| {};
37 CRM
.config
.dateInputFormat
= 'mm/dd/yy';
38 CRM
.config
.timeIs24Hr
= true;
40 $rootScope
.model
= model
= {
45 it('should update the UI after changing the model', function() {
46 element
= $compile(standardMarkup
)($rootScope
);
50 expect($rootScope
.myForm
.$valid
).toBe(true);
51 expect(element
.find('.radio-now').prop('checked')).toBe(true);
52 expect(element
.find('.radio-at').prop('checked')).toBe(false);
53 expect(element
.find('.crm-form-date').datepicker('getDate')).toBe(null);
54 expect(element
.find('.crm-form-time').timeEntry('getTime')).toBe(null);
58 expect($rootScope
.myForm
.$valid
).toBe(false);
59 expect(element
.find('.radio-now').prop('checked')).toBe(false);
60 expect(element
.find('.radio-at').prop('checked')).toBe(true);
61 expect(element
.find('.crm-form-date').datepicker('getDate')).toBe(null);
62 expect(element
.find('.crm-form-time').timeEntry('getTime')).toBe(null);
64 model
.the_date
= '2014-01-01';
66 expect($rootScope
.myForm
.$valid
).toBe(false);
67 expect(element
.find('.radio-now').prop('checked')).toBe(false);
68 expect(element
.find('.radio-at').prop('checked')).toBe(true);
69 expect(element
.find('.crm-hidden-date').val()).toEqual('2014-01-01');
70 expect(element
.find('.crm-form-date').val()).toEqual('01/01/2014');
71 expect(element
.find('.crm-form-time').timeEntry('getTime')).toBe(null);
73 model
.the_date
= '02:03:00';
75 expect($rootScope
.myForm
.$valid
).toBe(false);
76 expect(element
.find('.radio-now').prop('checked')).toBe(false);
77 expect(element
.find('.radio-at').prop('checked')).toBe(true);
78 expect(element
.find('.crm-form-date').datepicker('getDate')).toBe(null);
79 expect(element
.find('.crm-form-time').timeEntry('getTime').getMinutes()).toBe(3);
81 model
.the_date
= '2014-01-02 02:03:00';
83 expect($rootScope
.myForm
.$valid
).toBe(false);
84 expect(element
.find('.radio-now').prop('checked')).toBe(false);
85 expect(element
.find('.radio-at').prop('checked')).toBe(true);
86 expect(element
.find('.crm-form-date').datepicker('getDate').toDateString()).toEqual('Thu Jan 02 2014');
87 expect(element
.find('.crm-form-time').timeEntry('getTime').getMinutes()).toBe(3);
92 if (now
.getMonth() == 12) {
95 month
= month
+ (now
.getMonth() + 1);
97 if (now
.getDate() >= 28) {
100 day
= day
+ (now
.getDate() + 1);
102 var year
= (now
.getFullYear() + 1);
103 if (day
.length
< 2) day
= '0' + day
;
104 if (month
.length
< 2) month
= '0' + month
;
107 var datenow
= [year
, month
, day
].join('-');
108 var time
= [hours
, minutes
, "00"].join(':');
109 var currentDate
= datenow
+ ' ' + time
;
110 // Using datenow in the constructor here converts to local time. If not
111 // running on GMT (or east) then comparison to toDateString below fails.
112 // Also use month-1 because...javascript.
113 var ndate
= new Date(year
, month
-1, day
, 0, 0, 0);
114 model
.the_date
= currentDate
;
116 $rootScope
.$digest();
117 expect($rootScope
.myForm
.$valid
).toBe(true);
118 expect(element
.find('.radio-now').prop('checked')).toBe(false);
119 expect(element
.find('.radio-at').prop('checked')).toBe(true);
120 expect(element
.find('.crm-form-date').datepicker('getDate').toDateString()).toEqual(ndate
.toDateString());
121 expect(element
.find('.crm-hidden-date').val()).toEqual(currentDate
);
124 it('should update the model after changing the date and time', function() {
125 element
= $compile(standardMarkup
)($rootScope
);
128 $rootScope
.$digest();
129 expect($rootScope
.myForm
.$valid
).toBe(true);
130 expect(element
.find('.radio-now').prop('checked')).toBe(true);
131 expect(element
.find('.radio-at').prop('checked')).toBe(false);
133 element
.find('.radio-now').click().trigger('click').trigger('change');
134 element
.find('.crm-form-date').datepicker('setDate', $.datepicker
.parseDate('yy-mm-dd', '2014-01-03')).trigger('change');
135 $rootScope
.$digest();
136 expect(model
.the_date
).toBe('2014-01-03');
137 expect($rootScope
.myForm
.$valid
).toBe(false);
138 expect(element
.find('.radio-now').prop('checked')).toBe(false);
139 expect(element
.find('.radio-at').prop('checked')).toBe(true);
141 element
.find('.crm-form-time').timeEntry('setTime', '04:05').trigger('change');
142 $rootScope
.$digest();
143 expect(model
.the_date
).toBe('2014-01-03 04:05:00');
144 expect($rootScope
.myForm
.$valid
).toBe(false);
145 expect(element
.find('.radio-now').prop('checked')).toBe(false);
146 expect(element
.find('.radio-at').prop('checked')).toBe(true);
148 element
.find('.crm-form-date').datepicker('setDate', '').trigger('change');
149 $rootScope
.$digest();
150 expect(model
.the_date
).toBe('04:05:00');
151 expect($rootScope
.myForm
.$valid
).toBe(false);
152 expect(element
.find('.radio-now').prop('checked')).toBe(false);
153 expect(element
.find('.radio-at').prop('checked')).toBe(true);
155 element
.find('.radio-now').click().trigger('click').trigger('change');
156 $rootScope
.$digest();
157 expect(model
.the_date
).toBe(null);
158 expect($rootScope
.myForm
.$valid
).toBe(true);
159 expect(element
.find('.radio-now').prop('checked')).toBe(true);
160 expect(element
.find('.radio-at').prop('checked')).toBe(false);