forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoupleTimeFieldsSpec.coffee
More file actions
214 lines (182 loc) · 6.51 KB
/
Copy pathcoupleTimeFieldsSpec.coffee
File metadata and controls
214 lines (182 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#
# Copyright (C) 2015 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
define [
'timezone/Europe/London'
'timezone'
'compiled/util/coupleTimeFields'
'compiled/widget/DatetimeField'
'jquery'
'helpers/fakeENV'
], (london, tz, coupleTimeFields, DatetimeField, $, fakeENV) ->
# make sure this is on today date but without seconds/milliseconds, so we
# don't get screwed by dates shifting and seconds truncated during
# reinterpretation of field values, but also make sure it's the middle of the
# day so timezones don't shift the actual date from under us either
fixed = new Date()
fixed.setHours(12)
fixed.setMinutes(0)
fixed.setSeconds(0)
fixed.setMilliseconds(0)
tomorrow = new Date(fixed)
tomorrow.setDate(tomorrow.getDate() + 1)
QUnit.module 'initial coupling',
setup: ->
@$start = $('<input type="text">')
@$end = $('<input type="text">')
@start = new DatetimeField(@$start, timeOnly: true)
@end = new DatetimeField(@$end, timeOnly: true)
test 'updates start to be <= end', ->
@start.setTime(new Date(+fixed + 3600000))
@end.setTime(fixed)
coupleTimeFields(@$start, @$end)
equal +@start.datetime, +fixed
test 'leaves start < end alone', ->
earlier = new Date(+fixed - 3600000)
@start.setTime(earlier)
@end.setTime(fixed)
coupleTimeFields(@$start, @$end)
equal +@start.datetime, +earlier
test 'leaves blank start alone', ->
@start.setTime(null)
@end.setTime(fixed)
coupleTimeFields(@$start, @$end)
equal @start.blank, true
test 'leaves blank end alone', ->
@start.setTime(fixed)
@end.setTime(null)
coupleTimeFields(@$start, @$end)
equal @end.blank, true
test 'leaves invalid start alone', ->
@$start.val('invalid')
@start.setFromValue()
@end.setTime(fixed)
coupleTimeFields(@$start, @$end)
equal @$start.val(), 'invalid'
equal @start.invalid, true
test 'leaves invalid end alone', ->
@start.setTime(fixed)
@$end.val('invalid')
@end.setFromValue()
coupleTimeFields(@$start, @$end)
equal @$end.val(), 'invalid'
equal @end.invalid, true
test 'interprets time as occurring on date', ->
@$date= $('<input type="text">')
@date = new DatetimeField(@$date, dateOnly: true)
@date.setDate(tomorrow)
@start.setTime(fixed)
@end.setTime(fixed)
coupleTimeFields(@$start, @$end, @$date)
equal @start.datetime.getDate(), tomorrow.getDate()
equal @end.datetime.getDate(), tomorrow.getDate()
QUnit.module 'post coupling',
setup: ->
@$start = $('<input type="text">')
@$end = $('<input type="text">')
@start = new DatetimeField(@$start, timeOnly: true)
@end = new DatetimeField(@$end, timeOnly: true)
coupleTimeFields(@$start, @$end)
test 'changing end updates start to be <= end', ->
@start.setTime(new Date(+fixed + 3600000))
@end.setTime(fixed)
@$end.trigger('blur')
equal +@start.datetime, +fixed
test 'changing start updates end to be >= start', ->
@end.setTime(new Date(+fixed - 3600000))
@start.setTime(fixed)
@$start.trigger('blur')
equal +@end.datetime, +fixed
test 'leaves start < end alone', ->
earlier = new Date(+fixed - 3600000)
@start.setTime(earlier)
@end.setTime(fixed)
@$start.trigger('blur')
equal +@start.datetime, +earlier
equal +@end.datetime, +fixed
test 'leaves blank start alone', ->
@start.setTime(null)
@end.setTime(fixed)
@$end.trigger('blur')
equal @start.blank, true
test 'leaves blank end alone', ->
@start.setTime(fixed)
@end.setTime(null)
@$start.trigger('blur')
equal @end.blank, true
test 'leaves invalid start alone', ->
@$start.val('invalid')
@start.setFromValue()
@end.setTime(fixed)
@$end.trigger('blur')
equal @$start.val(), 'invalid'
equal @start.invalid, true
test 'leaves invalid end alone', ->
@start.setTime(fixed)
@$end.val('invalid')
@end.setFromValue()
@$start.trigger('blur')
equal @$end.val(), 'invalid'
equal @end.invalid, true
test 'does not rewrite blurred input', ->
@$start.val('7') # interpreted as 7pm, but should not be rewritten
@start.setFromValue()
@end.setTime(new Date(+@start.datetime + 3600000))
@$start.trigger('blur')
equal @$start.val(), '7'
test 'does not rewrite other input', ->
@$start.val('7') # interpreted as 7pm, but should not be rewritten
@start.setFromValue()
@end.setTime(new Date(+@start.datetime + 3600000))
@$end.trigger('blur')
equal @$start.val(), '7'
test 'does not switch time fields if in order by user profile timezone, even if out of order in local timezone', ->
snapshot = tz.snapshot()
# set local timezone to UTC
tz.changeZone(london, 'Europe/London')
# set user profile timezone to EST (UTC-4)
fakeENV.setup(TIMEZONE: 'America/Detroit')
# 1am in profile timezone, or 9pm in local timezone
@$start.val('1:00 AM')
@start.setFromValue()
# 5pm in profile timezone, or 1pm in local timezone
@$end.val('5:00 PM')
@end.setFromValue()
# store current end datetime
endTime = +@end.datetime
@$start.trigger('blur')
tz.restore(snapshot)
fakeENV.teardown()
# check that the end datetime has not been changed
equal +@end.datetime, endTime
QUnit.module 'with date field',
setup: ->
@$start = $('<input type="text">')
@$end = $('<input type="text">')
@$date= $('<input type="text">')
@start = new DatetimeField(@$start, timeOnly: true)
@end = new DatetimeField(@$end, timeOnly: true)
@date = new DatetimeField(@$date, dateOnly: true)
coupleTimeFields(@$start, @$end, @$date)
test 'interprets time as occurring on date', ->
@date.setDate(tomorrow)
@$date.trigger('blur')
@start.setTime(fixed)
@start.parseValue()
@end.setTime(fixed)
@end.parseValue()
equal @start.datetime.getDate(), tomorrow.getDate()
equal @end.datetime.getDate(), tomorrow.getDate()