Commit | Line | Data |
---|---|---|
8ff76a1d | 1 | 'use strict'; |
ac5009c1 | 2 | /* global $, CRM:true */ |
8ff76a1d TO |
3 | |
4 | describe('crmMailingRadioDate', function() { | |
5 | ||
6 | beforeEach(function() { | |
7 | module('crmResource'); | |
8 | module('crmUtil'); | |
9 | module('crmMailing'); | |
10 | }); | |
11 | ||
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" />' + | |
a42344f4 | 16 | ' <input crm-ui-datepicker ng-model="mySchedule.datetime" ng-required="mySchedule.mode == \'at\'"/>' + |
8ff76a1d TO |
17 | ' </div>' + |
18 | '</form>'; | |
19 | ||
20 | describe('crmMailingRadioDate directive', function() { | |
21 | var $compile, | |
22 | $rootScope, | |
23 | $interval, | |
24 | $timeout, | |
25 | model, | |
26 | element; | |
27 | ||
28 | beforeEach(inject(function(_$compile_, _$rootScope_, _$interval_, _$timeout_) { | |
29 | $compile = _$compile_; | |
30 | $rootScope = _$rootScope_; | |
31 | $interval = _$interval_; | |
32 | $timeout = _$timeout_; | |
33 | ||
a42344f4 CW |
34 | // Global settings needed for crmUiDatepicker |
35 | CRM = CRM || {}; | |
36 | CRM.config = CRM.config || {}; | |
37 | CRM.config.dateInputFormat = 'mm/dd/yy'; | |
38 | CRM.config.timeIs24Hr = true; | |
39 | ||
8ff76a1d TO |
40 | $rootScope.model = model = { |
41 | the_date: '' | |
42 | }; | |
43 | })); | |
44 | ||
45 | it('should update the UI after changing the model', function() { | |
46 | element = $compile(standardMarkup)($rootScope); | |
47 | ||
48 | model.the_date = ''; | |
49 | $rootScope.$digest(); | |
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); | |
a42344f4 CW |
53 | expect(element.find('.crm-form-date').datepicker('getDate')).toBe(null); |
54 | expect(element.find('.crm-form-time').timeEntry('getTime')).toBe(null); | |
8ff76a1d TO |
55 | |
56 | model.the_date = ' '; | |
57 | $rootScope.$digest(); | |
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); | |
a42344f4 CW |
61 | expect(element.find('.crm-form-date').datepicker('getDate')).toBe(null); |
62 | expect(element.find('.crm-form-time').timeEntry('getTime')).toBe(null); | |
8ff76a1d | 63 | |
ac5009c1 | 64 | model.the_date = '2014-01-01'; |
8ff76a1d TO |
65 | $rootScope.$digest(); |
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); | |
ac5009c1 CW |
69 | expect(element.find('.crm-hidden-date').val()).toEqual('2014-01-01'); |
70 | expect(element.find('.crm-form-date').val()).toEqual('01/01/2014'); | |
a42344f4 | 71 | expect(element.find('.crm-form-time').timeEntry('getTime')).toBe(null); |
8ff76a1d | 72 | |
ac5009c1 | 73 | model.the_date = '02:03:00'; |
8ff76a1d TO |
74 | $rootScope.$digest(); |
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); | |
a42344f4 CW |
78 | expect(element.find('.crm-form-date').datepicker('getDate')).toBe(null); |
79 | expect(element.find('.crm-form-time').timeEntry('getTime').getMinutes()).toBe(3); | |
8ff76a1d | 80 | |
ac5009c1 | 81 | model.the_date = '2014-01-02 02:03:00'; |
8ff76a1d | 82 | $rootScope.$digest(); |
3bea5218 | 83 | expect($rootScope.myForm.$valid).toBe(false); |
8ff76a1d TO |
84 | expect(element.find('.radio-now').prop('checked')).toBe(false); |
85 | expect(element.find('.radio-at').prop('checked')).toBe(true); | |
a42344f4 CW |
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); | |
3bea5218 SL |
88 | |
89 | var now = new Date(); | |
bb3476be | 90 | var month = '' + (now.getMonth() + 1); |
1f1201c0 | 91 | var year = (now.getFullYear() + 1); |
3bea5218 | 92 | if (month.length < 2) month = '0' + month; |
abcd0dab SL |
93 | var day = "01"; |
94 | var minutes = "30"; | |
95 | var hours = "09"; | |
3bea5218 SL |
96 | var datenow = [year, month, day].join('-'); |
97 | var time = [hours, minutes, "00"].join(':'); | |
98 | var currentDate = datenow + ' ' + time; | |
1f1201c0 | 99 | var ndate = new Date(datenow); |
3bea5218 | 100 | model.the_date = currentDate; |
bb3476be | 101 | |
3bea5218 SL |
102 | $rootScope.$digest(); |
103 | expect($rootScope.myForm.$valid).toBe(true); | |
104 | expect(element.find('.radio-now').prop('checked')).toBe(false); | |
105 | expect(element.find('.radio-at').prop('checked')).toBe(true); | |
bb3476be C |
106 | // expect(element.find('.crm-form-date').datepicker('getDate').toDateString()).toEqual(ndate.toDateString()); |
107 | expect(element.find('.crm-form-date').datepicker('getDate').getUTCDate()).toEqual(ndate.getUTCDate()); | |
1f1201c0 | 108 | expect(element.find('.crm-hidden-date').val()).toEqual(currentDate); |
8ff76a1d TO |
109 | }); |
110 | ||
111 | it('should update the model after changing the date and time', function() { | |
112 | element = $compile(standardMarkup)($rootScope); | |
ac5009c1 | 113 | |
8ff76a1d TO |
114 | model.the_date = ''; |
115 | $rootScope.$digest(); | |
116 | expect($rootScope.myForm.$valid).toBe(true); | |
117 | expect(element.find('.radio-now').prop('checked')).toBe(true); | |
118 | expect(element.find('.radio-at').prop('checked')).toBe(false); | |
119 | ||
ac5009c1 CW |
120 | element.find('.radio-now').click().trigger('click').trigger('change'); |
121 | element.find('.crm-form-date').datepicker('setDate', $.datepicker.parseDate('yy-mm-dd', '2014-01-03')).trigger('change'); | |
8ff76a1d | 122 | $rootScope.$digest(); |
a42344f4 | 123 | expect(model.the_date).toBe('2014-01-03'); |
8ff76a1d TO |
124 | expect($rootScope.myForm.$valid).toBe(false); |
125 | expect(element.find('.radio-now').prop('checked')).toBe(false); | |
126 | expect(element.find('.radio-at').prop('checked')).toBe(true); | |
127 | ||
a42344f4 | 128 | element.find('.crm-form-time').timeEntry('setTime', '04:05').trigger('change'); |
8ff76a1d | 129 | $rootScope.$digest(); |
ac5009c1 | 130 | expect(model.the_date).toBe('2014-01-03 04:05:00'); |
3bea5218 | 131 | expect($rootScope.myForm.$valid).toBe(false); |
8ff76a1d TO |
132 | expect(element.find('.radio-now').prop('checked')).toBe(false); |
133 | expect(element.find('.radio-at').prop('checked')).toBe(true); | |
134 | ||
a42344f4 | 135 | element.find('.crm-form-date').datepicker('setDate', '').trigger('change'); |
8ff76a1d | 136 | $rootScope.$digest(); |
ac5009c1 | 137 | expect(model.the_date).toBe('04:05:00'); |
8ff76a1d TO |
138 | expect($rootScope.myForm.$valid).toBe(false); |
139 | expect(element.find('.radio-now').prop('checked')).toBe(false); | |
140 | expect(element.find('.radio-at').prop('checked')).toBe(true); | |
141 | ||
142 | element.find('.radio-now').click().trigger('click').trigger('change'); | |
143 | $rootScope.$digest(); | |
144 | expect(model.the_date).toBe(null); | |
145 | expect($rootScope.myForm.$valid).toBe(true); | |
146 | expect(element.find('.radio-now').prop('checked')).toBe(true); | |
147 | expect(element.find('.radio-at').prop('checked')).toBe(false); | |
148 | }); | |
149 | }); | |
150 | }); |