commit | author | age
|
42cbbd
|
1 |
/* |
SE |
2 |
* Setup |
|
3 |
*/ |
|
4 |
|
0d9dc7
|
5 |
// Create the dom before requiring react |
42cbbd
|
6 |
var DOM = require('./testDOM'); |
0d9dc7
|
7 |
DOM(); |
M |
8 |
|
516b36
|
9 |
// Needs to be global to work in Travis CI |
92a2c6
|
10 |
React = require('react'); |
2d8253
|
11 |
ReactDOM = require('react-dom'); |
15436d
|
12 |
|
92a2c6
|
13 |
var Datetime = require('../DateTime'), |
0d9dc7
|
14 |
assert = require('assert'), |
92a2c6
|
15 |
moment = require('moment'), |
M |
16 |
TestUtils = require('react-addons-test-utils') |
0d9dc7
|
17 |
; |
42cbbd
|
18 |
|
SE |
19 |
/* |
|
20 |
* Utility Functions |
|
21 |
*/ |
0d9dc7
|
22 |
|
9f1b61
|
23 |
var createDatetime = function( props ) { |
2d8253
|
24 |
document.body.innerHTML = '<div id="root"></div>'; |
0d9dc7
|
25 |
|
2d8253
|
26 |
ReactDOM.render( |
516b36
|
27 |
React.createElement( Datetime, props ), |
2d8253
|
28 |
document.getElementById('root') |
0d9dc7
|
29 |
); |
M |
30 |
|
2d8253
|
31 |
return document.getElementById('root').children[0]; |
0d9dc7
|
32 |
}; |
M |
33 |
|
9f1b61
|
34 |
var trigger = function( name, element ) { |
59314a
|
35 |
var ev = document.createEvent("MouseEvents"); |
M |
36 |
ev.initEvent(name, true, true); |
|
37 |
element.dispatchEvent( ev ); |
|
38 |
}; |
|
39 |
|
92a2c6
|
40 |
var ev = TestUtils.Simulate; |
59314a
|
41 |
var dt = { |
9f1b61
|
42 |
dt: function() { |
2d8253
|
43 |
return document.getElementById('root').children[0]; |
59314a
|
44 |
}, |
9f1b61
|
45 |
view: function() { |
59314a
|
46 |
return this.dt().children[1].children[0]; |
M |
47 |
}, |
9f1b61
|
48 |
input: function() { |
59314a
|
49 |
return this.dt().children[0]; |
M |
50 |
}, |
9f1b61
|
51 |
switcher: function() { |
2d8253
|
52 |
return document.querySelector('.rdtSwitch'); |
59314a
|
53 |
}, |
9f1b61
|
54 |
timeSwitcher: function() { |
2d8253
|
55 |
return document.querySelector('.rdtTimeToggle'); |
59314a
|
56 |
}, |
9f1b61
|
57 |
year: function( n ) { |
2d8253
|
58 |
var years = document.querySelectorAll('.rdtYear'); |
59314a
|
59 |
return years[ n || 0 ]; |
M |
60 |
}, |
9f1b61
|
61 |
month: function( n ) { |
2d8253
|
62 |
var months = document.querySelectorAll('.rdtMonth'); |
59314a
|
63 |
return months[ n || 0 ]; |
M |
64 |
}, |
9f1b61
|
65 |
day: function( n ) { |
2d8253
|
66 |
return document.querySelector('.rdtDay[data-value="' + n + '"]'); |
59314a
|
67 |
}, |
9f1b61
|
68 |
next: function() { |
a8a17a
|
69 |
return document.querySelector('.rdtNext span'); |
59314a
|
70 |
}, |
9f1b61
|
71 |
prev: function() { |
a8a17a
|
72 |
return document.querySelector('.rdtPrev span'); |
59314a
|
73 |
}, |
9f1b61
|
74 |
timeUp: function( n ) { |
59314a
|
75 |
return document.querySelectorAll('.rdtCounter')[ n ].children[0]; |
M |
76 |
}, |
9f1b61
|
77 |
timeDown: function( n ) { |
59314a
|
78 |
return document.querySelectorAll('.rdtCounter')[ n ].children[2]; |
M |
79 |
}, |
9f1b61
|
80 |
hour: function() { |
59314a
|
81 |
return document.querySelectorAll('.rdtCount')[0]; |
M |
82 |
}, |
9f1b61
|
83 |
minute: function() { |
59314a
|
84 |
return document.querySelectorAll('.rdtCount')[1]; |
M |
85 |
}, |
9f1b61
|
86 |
second: function() { |
59314a
|
87 |
return document.querySelectorAll('.rdtCount')[2]; |
M |
88 |
}, |
9f1b61
|
89 |
milli: function() { |
59314a
|
90 |
return document.querySelector('.rdtMilli input'); |
963148
|
91 |
}, |
9f1b61
|
92 |
isOpen: function() { |
963148
|
93 |
return !!document.querySelector('.rdt.rdtOpen'); |
59314a
|
94 |
} |
M |
95 |
}; |
|
96 |
|
|
97 |
var date = new Date( 2000, 0, 15, 2, 2, 2, 2 ), |
0d9dc7
|
98 |
mDate = moment( date ), |
049c33
|
99 |
strDate = mDate.format('L') + ' ' + mDate.format('LT'), |
TS |
100 |
mDateUTC = moment.utc(date), |
bdad6c
|
101 |
strDateUTC = mDateUTC.format('L') + ' ' + mDateUTC.format('LT'), |
SE |
102 |
currentYear = new Date().getFullYear() |
0d9dc7
|
103 |
; |
42cbbd
|
104 |
|
SE |
105 |
/* |
|
106 |
* Tests |
|
107 |
*/ |
0d9dc7
|
108 |
|
9f1b61
|
109 |
describe( 'Datetime', function() { |
SE |
110 |
it( 'Create Datetime', function() { |
516b36
|
111 |
var component = createDatetime({}); |
0d9dc7
|
112 |
assert( component ); |
M |
113 |
assert.equal( component.children.length, 2 ); |
|
114 |
assert.equal( component.children[0].tagName , 'INPUT' ); |
|
115 |
assert.equal( component.children[1].tagName , 'DIV' ); |
|
116 |
}); |
|
117 |
|
9f1b61
|
118 |
it( 'input=false', function() { |
516b36
|
119 |
var component = createDatetime({ input: false }); |
0d9dc7
|
120 |
assert( component ); |
M |
121 |
assert.equal( component.children.length, 1 ); |
|
122 |
assert.equal( component.children[0].tagName , 'DIV' ); |
|
123 |
}); |
92a2c6
|
124 |
|
9f1b61
|
125 |
it( 'Date value', function() { |
516b36
|
126 |
var component = createDatetime({ value: date }), |
0d9dc7
|
127 |
input = component.children[0] |
M |
128 |
; |
|
129 |
assert.equal( input.value, strDate ); |
|
130 |
}); |
|
131 |
|
9f1b61
|
132 |
it( 'Moment value', function() { |
516b36
|
133 |
var component = createDatetime({ value: mDate }), |
0d9dc7
|
134 |
input = component.children[0] |
M |
135 |
; |
|
136 |
assert.equal( input.value, strDate ); |
|
137 |
}); |
|
138 |
|
9f1b61
|
139 |
it( 'String value', function() { |
516b36
|
140 |
var component = createDatetime({ value: strDate }), |
0d9dc7
|
141 |
input = component.children[0] |
M |
142 |
; |
|
143 |
assert.equal( input.value, strDate ); |
|
144 |
}); |
|
145 |
|
9f1b61
|
146 |
it( 'UTC Value from local moment', function() { |
049c33
|
147 |
var component = createDatetime({ |
TS |
148 |
value: mDate, |
|
149 |
utc: true |
|
150 |
}); |
|
151 |
var input = component.children[0]; |
|
152 |
assert.equal( input.value, strDateUTC ); |
|
153 |
}); |
|
154 |
|
9f1b61
|
155 |
it( 'UTC Value from UTC moment', function() { |
049c33
|
156 |
var component = createDatetime({ |
TS |
157 |
value: mDateUTC, |
|
158 |
utc: true |
|
159 |
}); |
|
160 |
var input = component.children[0]; |
|
161 |
assert.equal( input.value, strDateUTC ); |
|
162 |
}); |
|
163 |
|
9f1b61
|
164 |
it( 'UTC Value from utc string', function() { |
049c33
|
165 |
var component = createDatetime({ |
TS |
166 |
value: strDateUTC, |
|
167 |
utc: true |
|
168 |
}); |
|
169 |
var input = component.children[0]; |
|
170 |
assert.equal( input.value, strDateUTC ); |
|
171 |
}); |
|
172 |
|
9f1b61
|
173 |
it( 'Date defaultValue', function() { |
516b36
|
174 |
var component = createDatetime({ defaultValue: date }), |
0d9dc7
|
175 |
input = component.children[0] |
M |
176 |
; |
|
177 |
assert.equal( input.value, strDate ); |
|
178 |
}); |
|
179 |
|
9f1b61
|
180 |
it( 'Moment defaultValue', function() { |
516b36
|
181 |
var component = createDatetime({ defaultValue: mDate }), |
0d9dc7
|
182 |
input = component.children[0] |
M |
183 |
; |
|
184 |
assert.equal( input.value, strDate ); |
|
185 |
}); |
|
186 |
|
9f1b61
|
187 |
it( 'String defaultValue', function() { |
516b36
|
188 |
var component = createDatetime({ defaultValue: strDate }), |
0d9dc7
|
189 |
input = component.children[0] |
M |
190 |
; |
|
191 |
assert.equal( input.value, strDate ); |
|
192 |
}); |
|
193 |
|
9f1b61
|
194 |
it( 'dateFormat', function() { |
516b36
|
195 |
var component = createDatetime({ value: date, dateFormat: 'M&D' }), |
0d9dc7
|
196 |
input = component.children[0] |
M |
197 |
; |
|
198 |
assert.equal( input.value, mDate.format('M&D LT') ); |
|
199 |
}); |
|
200 |
|
9f1b61
|
201 |
it( 'dateFormat=false', function() { |
516b36
|
202 |
var component = createDatetime({ value: date, dateFormat: false }), |
0d9dc7
|
203 |
input = component.children[0], |
59314a
|
204 |
view = dt.view() |
0d9dc7
|
205 |
; |
M |
206 |
assert.equal( input.value, mDate.format('LT') ); |
|
207 |
// The view must be the timepicker |
|
208 |
assert.equal( view.className, 'rdtTime' ); |
|
209 |
// There must not be a date toggle |
|
210 |
assert.equal( view.querySelectorAll('thead').length, 0); |
|
211 |
}); |
98310d
|
212 |
|
9f1b61
|
213 |
it( 'timeFormat', function() { |
0d9dc7
|
214 |
var format = 'HH:mm:ss:SSS', |
516b36
|
215 |
component = createDatetime({ value: date, timeFormat: format }), |
0d9dc7
|
216 |
input = component.children[0] |
M |
217 |
; |
|
218 |
assert.equal( input.value, mDate.format('L ' + format) ); |
|
219 |
}); |
|
220 |
|
9f1b61
|
221 |
it( 'timeFormat=false', function() { |
516b36
|
222 |
var component = createDatetime({ value: date, timeFormat: false }), |
0d9dc7
|
223 |
input = component.children[0], |
59314a
|
224 |
view = dt.view() |
0d9dc7
|
225 |
; |
M |
226 |
assert.equal( input.value, mDate.format('L') ); |
|
227 |
// The view must be the daypicker |
|
228 |
assert.equal( view.className, 'rdtDays' ); |
|
229 |
// There must not be a time toggle |
|
230 |
assert.equal( view.querySelectorAll('.timeToggle').length, 0); |
|
231 |
}); |
|
232 |
|
9f1b61
|
233 |
it( 'timeFormat with lowercase am', function() { |
cf1e72
|
234 |
var format = 'HH:mm:ss:SSS a', |
SE |
235 |
component = createDatetime({ value: date, timeFormat: format }), |
|
236 |
input = component.children[0] |
|
237 |
; |
|
238 |
assert.notEqual( input.value.indexOf('am'), -1 ); |
|
239 |
}); |
|
240 |
|
9f1b61
|
241 |
it( 'timeFormat with uppercase AM', function() { |
cf1e72
|
242 |
var format = 'HH:mm:ss:SSS A', |
SE |
243 |
component = createDatetime({ value: date, timeFormat: format }), |
|
244 |
input = component.children[0] |
|
245 |
; |
|
246 |
assert.notEqual( input.value.indexOf('AM'), -1 ); |
|
247 |
}); |
|
248 |
|
9f1b61
|
249 |
it( 'viewMode=years', function() { |
516b36
|
250 |
var component = createDatetime({ viewMode: 'years' }), |
59314a
|
251 |
view = dt.view() |
0d9dc7
|
252 |
; |
M |
253 |
|
|
254 |
assert.equal( view.className, 'rdtYears' ); |
|
255 |
}); |
|
256 |
|
9f1b61
|
257 |
it( 'viewMode=months', function() { |
516b36
|
258 |
var component = createDatetime({ viewMode: 'months' }), |
59314a
|
259 |
view = dt.view() |
0d9dc7
|
260 |
; |
M |
261 |
|
|
262 |
assert.equal( view.className, 'rdtMonths' ); |
|
263 |
}); |
|
264 |
|
9f1b61
|
265 |
it( 'viewMode=time', function() { |
516b36
|
266 |
var component = createDatetime({ viewMode: 'time' }), |
59314a
|
267 |
view = dt.view() |
0d9dc7
|
268 |
; |
M |
269 |
|
|
270 |
assert.equal( view.className, 'rdtTime' ); |
|
271 |
}); |
|
272 |
|
9f1b61
|
273 |
it( 'className of type string', function() { |
516b36
|
274 |
var component = createDatetime({ className: 'custom' }); |
0d9dc7
|
275 |
assert.notEqual( component.className.indexOf('custom'), -1 ); |
M |
276 |
}); |
|
277 |
|
9f1b61
|
278 |
it( 'className of type string array', function() { |
963148
|
279 |
var component = createDatetime({ className: ['custom1', 'custom2'] }); |
SE |
280 |
assert.notEqual( component.className.indexOf('custom1'), -1 ); |
|
281 |
assert.notEqual( component.className.indexOf('custom2'), -1 ); |
|
282 |
}); |
386942
|
283 |
|
9f1b61
|
284 |
it( 'inputProps', function() { |
516b36
|
285 |
var component = createDatetime({ inputProps: { className: 'myInput', type: 'email' } }), |
0d9dc7
|
286 |
input = component.children[0] |
M |
287 |
; |
|
288 |
|
|
289 |
assert.equal( input.className, 'myInput' ); |
|
290 |
assert.equal( input.type, 'email' ); |
|
291 |
}); |
|
292 |
|
9f1b61
|
293 |
it( 'renderDay', function() { |
0d9dc7
|
294 |
var props, currentDate, selectedDate, |
9f1b61
|
295 |
component = createDatetime({ value: mDate, renderDay: function( p, current, selected ) { |
0d9dc7
|
296 |
props = p; |
M |
297 |
currentDate = current; |
|
298 |
selectedDate = selected; |
|
299 |
|
|
300 |
return React.DOM.td( props, 'day' ); |
|
301 |
}}), |
59314a
|
302 |
view = dt.view() |
0d9dc7
|
303 |
; |
M |
304 |
|
|
305 |
// Last day should be 6th of february |
|
306 |
assert.equal( currentDate.day(), 6 ); |
|
307 |
assert.equal( currentDate.month(), 1 ); |
|
308 |
|
|
309 |
// The date must be the same |
|
310 |
assert.equal( selectedDate.isSame( mDate ), true ); |
|
311 |
|
|
312 |
// There should be a onClick function in the props |
|
313 |
assert.equal( typeof props.onClick, 'function' ); |
|
314 |
|
|
315 |
// The cell text should be 'day' |
2d8253
|
316 |
assert.equal( view.querySelector('.rdtDay').innerHTML, 'day' ); |
0d9dc7
|
317 |
}); |
M |
318 |
|
9f1b61
|
319 |
it( 'renderMonth', function() { |
0d9dc7
|
320 |
var props, month, year, selectedDate, |
9f1b61
|
321 |
component = createDatetime({ value: mDate, viewMode: 'months', renderMonth: function( p, m, y, selected ) { |
0d9dc7
|
322 |
props = p; |
M |
323 |
month = m; |
|
324 |
year = y; |
|
325 |
selectedDate = selected; |
|
326 |
|
|
327 |
return React.DOM.td( props, 'month' ); |
|
328 |
}}), |
59314a
|
329 |
view = dt.view() |
0d9dc7
|
330 |
; |
M |
331 |
|
|
332 |
// The date must be the same |
|
333 |
assert.equal( selectedDate.isSame( mDate ), true ); |
|
334 |
|
|
335 |
assert.equal( month, 11 ); |
|
336 |
assert.equal( year, 2000 ); |
|
337 |
|
|
338 |
// There should be a onClick function in the props |
|
339 |
assert.equal( typeof props.onClick, 'function' ); |
|
340 |
|
|
341 |
// The cell text should be 'day' |
2d8253
|
342 |
assert.equal( view.querySelector('.rdtMonth').innerHTML, 'month' ); |
0d9dc7
|
343 |
}); |
M |
344 |
|
9f1b61
|
345 |
it( 'renderYear', function() { |
0d9dc7
|
346 |
var props, year, selectedDate, |
9f1b61
|
347 |
component = createDatetime({ value: mDate, viewMode: 'years', renderYear: function( p, y, selected ) { |
0d9dc7
|
348 |
props = p; |
M |
349 |
year = y; |
|
350 |
selectedDate = selected; |
|
351 |
|
|
352 |
return React.DOM.td( props, 'year' ); |
|
353 |
}}), |
59314a
|
354 |
view = dt.view() |
0d9dc7
|
355 |
; |
M |
356 |
|
|
357 |
// The date must be the same |
|
358 |
assert.equal( selectedDate.isSame( mDate ), true ); |
|
359 |
|
|
360 |
assert.equal( year, 2010 ); |
|
361 |
|
|
362 |
// There should be a onClick function in the props |
|
363 |
assert.equal( typeof props.onClick, 'function' ); |
|
364 |
|
|
365 |
// The cell text should be 'day' |
2d8253
|
366 |
assert.equal( view.querySelector('.rdtYear').innerHTML, 'year' ); |
0d9dc7
|
367 |
}); |
59314a
|
368 |
|
M |
369 |
it( 'Time pickers depends on the time format', function() { |
42cbbd
|
370 |
createDatetime({ viewMode: 'time', timeFormat: 'HH:mm:ss:SSS'}); |
59314a
|
371 |
assert.equal( document.querySelectorAll('.rdtCounter').length, 4 ); |
M |
372 |
|
42cbbd
|
373 |
createDatetime({ viewMode: 'time', timeFormat: 'HH:mm:ss'}); |
59314a
|
374 |
assert.equal( document.querySelectorAll('.rdtCounter').length, 3 ); |
M |
375 |
|
42cbbd
|
376 |
createDatetime({ viewMode: 'time', timeFormat: 'HH:mm'}); |
59314a
|
377 |
assert.equal( document.querySelectorAll('.rdtCounter').length, 2 ); |
M |
378 |
|
42cbbd
|
379 |
createDatetime({ viewMode: 'time', timeFormat: 'HH'}); |
59314a
|
380 |
assert.equal( document.querySelectorAll('.rdtCounter').length, 1 ); |
M |
381 |
}); |
|
382 |
|
|
383 |
it( 'viewChange', function() { |
|
384 |
createDatetime({viewMode: 'time' }); |
|
385 |
|
|
386 |
assert.equal( dt.view().className, 'rdtTime' ); |
|
387 |
ev.click( dt.switcher() ); |
|
388 |
assert.equal( dt.view().className, 'rdtDays' ); |
|
389 |
ev.click( dt.switcher() ); |
|
390 |
assert.equal( dt.view().className, 'rdtMonths' ); |
|
391 |
ev.click( dt.switcher() ); |
|
392 |
assert.equal( dt.view().className, 'rdtYears' ); |
|
393 |
}); |
|
394 |
|
9f1b61
|
395 |
it( 'switch to time', function() { |
59314a
|
396 |
createDatetime({}); |
M |
397 |
assert.equal( dt.view().className, 'rdtDays' ); |
|
398 |
ev.click( dt.timeSwitcher() ); |
|
399 |
assert.equal( dt.view().className, 'rdtTime' ); |
98310d
|
400 |
}); |
59314a
|
401 |
|
9f1b61
|
402 |
it( 'selectYear', function() { |
59314a
|
403 |
createDatetime({ viewMode: 'years', defaultValue: date }); |
M |
404 |
assert.equal( dt.view().className, 'rdtYears' ); |
|
405 |
assert.equal( dt.switcher().innerHTML, '2000-2009' ); |
|
406 |
|
|
407 |
// First year is 1999 |
|
408 |
ev.click( dt.year() ); |
|
409 |
assert.equal( dt.view().className, 'rdtMonths' ); |
|
410 |
assert.equal( dt.switcher().innerHTML, '1999' ); |
|
411 |
}); |
|
412 |
|
9f1b61
|
413 |
it( 'increase decade', function() { |
59314a
|
414 |
createDatetime({ viewMode: 'years', defaultValue: date }); |
M |
415 |
|
|
416 |
assert.equal( dt.switcher().innerHTML, '2000-2009' ); |
|
417 |
ev.click( dt.next() ); |
|
418 |
assert.equal( dt.switcher().innerHTML, '2010-2019' ); |
|
419 |
ev.click( dt.next() ); |
|
420 |
assert.equal( dt.switcher().innerHTML, '2020-2029' ); |
|
421 |
}); |
|
422 |
|
9f1b61
|
423 |
it( 'decrease decade', function() { |
59314a
|
424 |
createDatetime({ viewMode: 'years', defaultValue: date }); |
M |
425 |
|
|
426 |
assert.equal( dt.switcher().innerHTML, '2000-2009' ); |
|
427 |
ev.click( dt.prev() ); |
|
428 |
assert.equal( dt.switcher().innerHTML, '1990-1999' ); |
|
429 |
ev.click( dt.prev() ); |
|
430 |
assert.equal( dt.switcher().innerHTML, '1980-1989' ); |
|
431 |
}); |
|
432 |
|
9f1b61
|
433 |
it( 'selectMonth', function() { |
59314a
|
434 |
createDatetime({ viewMode: 'months', defaultValue: date }); |
M |
435 |
assert.equal( dt.view().className, 'rdtMonths' ); |
|
436 |
assert.equal( dt.switcher().innerHTML, '2000' ); |
|
437 |
|
|
438 |
ev.click( dt.month(1) ); |
|
439 |
assert.equal( dt.view().className, 'rdtDays' ); |
42cbbd
|
440 |
assert.equal( dt.switcher().getAttribute('data-value'), '1' ); |
59314a
|
441 |
}); |
M |
442 |
|
9f1b61
|
443 |
it( 'increase year', function() { |
59314a
|
444 |
createDatetime({ viewMode: 'months', defaultValue: date }); |
M |
445 |
|
|
446 |
assert.equal( dt.switcher().getAttribute('data-value'), '2000' ); |
|
447 |
ev.click( dt.next() ); |
|
448 |
assert.equal( dt.switcher().getAttribute('data-value'), '2001' ); |
|
449 |
ev.click( dt.next() ); |
|
450 |
assert.equal( dt.switcher().getAttribute('data-value'), '2002' ); |
|
451 |
}); |
|
452 |
|
9f1b61
|
453 |
it( 'decrease year', function() { |
59314a
|
454 |
createDatetime({ viewMode: 'months', defaultValue: date }); |
M |
455 |
|
|
456 |
assert.equal( dt.switcher().getAttribute('data-value'), '2000' ); |
|
457 |
ev.click( dt.prev() ); |
|
458 |
assert.equal( dt.switcher().getAttribute('data-value'), '1999' ); |
|
459 |
ev.click( dt.prev() ); |
|
460 |
assert.equal( dt.switcher().getAttribute('data-value'), '1998' ); |
|
461 |
}); |
|
462 |
|
9f1b61
|
463 |
it( 'increase month', function() { |
59314a
|
464 |
createDatetime({ defaultValue: date }); |
M |
465 |
|
|
466 |
assert.equal( dt.switcher().getAttribute('data-value'), '0' ); |
|
467 |
ev.click( dt.next() ); |
|
468 |
assert.equal( dt.switcher().getAttribute('data-value'), '1' ); |
|
469 |
ev.click( dt.next() ); |
|
470 |
assert.equal( dt.switcher().getAttribute('data-value'), '2' ); |
|
471 |
}); |
|
472 |
|
9f1b61
|
473 |
it( 'decrease month', function() { |
59314a
|
474 |
createDatetime({ defaultValue: date }); |
M |
475 |
|
|
476 |
assert.equal( dt.switcher().getAttribute('data-value'), '0' ); |
|
477 |
ev.click( dt.prev() ); |
|
478 |
assert.equal( dt.switcher().getAttribute('data-value'), '11' ); |
|
479 |
ev.click( dt.prev() ); |
|
480 |
assert.equal( dt.switcher().getAttribute('data-value'), '10' ); |
|
481 |
}); |
|
482 |
|
9f1b61
|
483 |
it( 'open picker', function() { |
59314a
|
484 |
createDatetime({}); |
963148
|
485 |
assert.equal( dt.isOpen(), false ); |
59314a
|
486 |
ev.focus( dt.input() ); |
963148
|
487 |
assert.equal( dt.isOpen(), true ); |
59314a
|
488 |
}); |
M |
489 |
|
42cbbd
|
490 |
it( 'onChange', function( done ) { |
9f1b61
|
491 |
createDatetime({ defaultValue: date, onChange: function( selected ) { |
59314a
|
492 |
assert.equal( selected.date(), 2 ); |
M |
493 |
assert.equal( selected.month(), mDate.month() ); |
|
494 |
assert.equal( selected.year(), mDate.year() ); |
|
495 |
done(); |
|
496 |
}}); |
|
497 |
|
|
498 |
ev.click( dt.day( 2 ) ); |
|
499 |
}); |
|
500 |
|
42cbbd
|
501 |
it( 'multiple onChange', function( done ) { |
59314a
|
502 |
var i = 0; |
9f1b61
|
503 |
createDatetime({ defaultValue: date, onChange: function( selected ) { |
59314a
|
504 |
i++; |
9f1b61
|
505 |
if( i > 2 ) { |
59314a
|
506 |
assert.equal( selected.date(), 4 ); |
M |
507 |
assert.equal( selected.month(), mDate.month() ); |
|
508 |
assert.equal( selected.year(), mDate.year() ); |
|
509 |
done(); |
|
510 |
} |
|
511 |
}}); |
|
512 |
|
|
513 |
ev.click( dt.day( 2 ) ); |
|
514 |
ev.click( dt.day( 3 ) ); |
|
515 |
ev.click( dt.day( 4 ) ); |
|
516 |
}); |
|
517 |
|
9f1b61
|
518 |
it( 'onFocus', function() { |
aca9e6
|
519 |
var focus = false; |
9f1b61
|
520 |
createDatetime({ value: date, onFocus: function( selected ) { |
aca9e6
|
521 |
focus = true; |
R |
522 |
}}); |
|
523 |
|
|
524 |
ev.focus( dt.input() ); |
|
525 |
assert.equal( focus, true ); |
|
526 |
}); |
|
527 |
|
9f1b61
|
528 |
it( 'onBlur', function() { |
42cbbd
|
529 |
let onBlurCalled = false; |
9f1b61
|
530 |
createDatetime({ value: date, onBlur: function( selected ) { |
42cbbd
|
531 |
// TODO: This is never called |
SE |
532 |
onBlurCalled = true; |
|
533 |
assert.equal( true, false ); // Just to prove that this code is not being run |
9f1b61
|
534 |
assert.equal( dt.dt().className.indexOf( 'rdtOpen' ), -1 ); |
59314a
|
535 |
assert.equal( selected.date(), mDate.date() ); |
M |
536 |
assert.equal( selected.month(), mDate.month() ); |
|
537 |
assert.equal( selected.year(), mDate.year() ); |
|
538 |
done(); |
|
539 |
}}); |
|
540 |
|
963148
|
541 |
assert.equal( dt.isOpen(), false ); |
59314a
|
542 |
ev.focus( dt.input() ); |
42cbbd
|
543 |
assert.equal( dt.isOpen(), true ); |
59314a
|
544 |
trigger( 'click', document.body ); |
42cbbd
|
545 |
// assert.equal( onBlurCalled, true ); |
59314a
|
546 |
}); |
M |
547 |
|
9f1b61
|
548 |
it( 'closeOnTab:true', function() { |
9012e8
|
549 |
createDatetime({ value: date }); |
M |
550 |
|
963148
|
551 |
assert.equal( dt.isOpen(), false ); |
9012e8
|
552 |
ev.focus( dt.input() ); |
963148
|
553 |
assert.equal(dt.isOpen(), true ); |
42cbbd
|
554 |
TestUtils.Simulate.keyDown(dt.input(), {key: 'Tab', keyCode: 9, which: 9}); |
963148
|
555 |
assert.equal( dt.isOpen(), false ); |
42cbbd
|
556 |
// trigger( 'click', document.body ); // Does nothing ?? |
9012e8
|
557 |
}); |
M |
558 |
|
9f1b61
|
559 |
it( 'closeOnTab:false', function() { |
9012e8
|
560 |
createDatetime({ value: date, closeOnTab: false }); |
M |
561 |
|
963148
|
562 |
assert.equal( dt.isOpen(), false ); |
9012e8
|
563 |
ev.focus( dt.input() ); |
963148
|
564 |
assert.equal(dt.isOpen(), true ); |
42cbbd
|
565 |
TestUtils.Simulate.keyDown(dt.input(), {key: 'Tab', keyCode: 9, which: 9}); |
963148
|
566 |
assert.equal(dt.isOpen(), true ); |
42cbbd
|
567 |
// trigger( 'click', document.body ); // Does nothing ?? |
9012e8
|
568 |
}); |
M |
569 |
|
9f1b61
|
570 |
it( 'increase time', function( done ) { |
59314a
|
571 |
var i = 0; |
42cbbd
|
572 |
createDatetime({ timeFormat: 'HH:mm:ss:SSS', viewMode: 'time', defaultValue: date, onChange: function( selected ) { |
59314a
|
573 |
i++; |
9f1b61
|
574 |
if( i > 2 ) { |
59314a
|
575 |
assert.equal( selected.hour(), 3 ); |
M |
576 |
assert.equal( selected.minute(), 3 ); |
|
577 |
assert.equal( selected.second(), 3 ); |
|
578 |
done(); |
|
579 |
} |
|
580 |
}}); |
|
581 |
|
|
582 |
trigger( 'mousedown', dt.timeUp( 0 ) ); |
|
583 |
trigger('mouseup', document.body ); |
|
584 |
assert.equal( dt.hour().innerHTML, 3 ); |
|
585 |
trigger( 'mousedown', dt.timeUp( 1 ) ); |
|
586 |
trigger( 'mouseup', dt.timeUp( 1 ) ); |
|
587 |
assert.equal( dt.minute().innerHTML, 3 ); |
|
588 |
trigger( 'mousedown', dt.timeUp( 2 ) ); |
|
589 |
trigger( 'mouseup', dt.timeUp( 2 ) ); |
|
590 |
assert.equal( dt.second().innerHTML, 3 ); |
|
591 |
}); |
|
592 |
|
9f1b61
|
593 |
it( 'decrease time', function( done ) { |
59314a
|
594 |
var i = 0; |
42cbbd
|
595 |
createDatetime({ timeFormat: 'HH:mm:ss:SSS', viewMode: 'time', defaultValue: date, onChange: function( selected ) { |
59314a
|
596 |
i++; |
9f1b61
|
597 |
if( i > 2 ) { |
59314a
|
598 |
assert.equal( selected.hour(), 1 ); |
M |
599 |
assert.equal( selected.minute(), 1 ); |
|
600 |
assert.equal( selected.second(), 1 ); |
|
601 |
done(); |
|
602 |
} |
|
603 |
}}); |
|
604 |
|
|
605 |
trigger('mousedown', dt.timeDown( 0 ) ); |
|
606 |
trigger('mouseup', dt.timeDown( 0 ) ); |
|
607 |
assert.equal( dt.hour().innerHTML, 1 ); |
|
608 |
trigger('mousedown', dt.timeDown( 1 ) ); |
|
609 |
trigger('mouseup', dt.timeDown( 1 ) ); |
|
610 |
assert.equal( dt.minute().innerHTML, 1 ); |
|
611 |
trigger('mousedown', dt.timeDown( 2 ) ); |
|
612 |
trigger('mouseup', dt.timeDown( 2 ) ); |
|
613 |
assert.equal( dt.second().innerHTML, 1 ); |
|
614 |
}); |
|
615 |
|
9f1b61
|
616 |
it( 'long increase time', function( done ) { |
42cbbd
|
617 |
createDatetime({ timeFormat: 'HH:mm:ss:SSS', viewMode: 'time', defaultValue: date}); |
59314a
|
618 |
|
M |
619 |
trigger( 'mousedown', dt.timeUp( 0 ) ); |
9f1b61
|
620 |
setTimeout( function() { |
59314a
|
621 |
trigger('mouseup', document.body ); |
M |
622 |
assert.notEqual( dt.hour().innerHTML, 2 ); |
|
623 |
assert.notEqual( dt.hour().innerHTML, 3 ); |
|
624 |
done(); |
a79e6b
|
625 |
}, 920 ); |
59314a
|
626 |
}); |
M |
627 |
|
9f1b61
|
628 |
it( 'long decrease time', function( done ) { |
42cbbd
|
629 |
createDatetime({ timeFormat: 'HH:mm:ss:SSS', viewMode: 'time', defaultValue: date}); |
59314a
|
630 |
|
M |
631 |
trigger( 'mousedown', dt.timeDown( 0 ) ); |
9f1b61
|
632 |
setTimeout( function() { |
59314a
|
633 |
trigger('mouseup', document.body ); |
M |
634 |
assert.notEqual( dt.hour().innerHTML, 1 ); |
|
635 |
assert.notEqual( dt.hour().innerHTML, 0 ); |
|
636 |
done(); |
a79e6b
|
637 |
}, 920 ); |
59314a
|
638 |
}); |
62fd2f
|
639 |
|
9f1b61
|
640 |
it( 'increase time with timeConstraints', function( done ) { |
9bb4f7
|
641 |
var i = 0; |
42cbbd
|
642 |
createDatetime({ timeFormat: 'HH:mm:ss:SSS', viewMode: 'time', defaultValue: date, onChange: function( selected ) { |
9bb4f7
|
643 |
i++; |
9f1b61
|
644 |
if( i > 2 ) { |
9bb4f7
|
645 |
assert.equal( selected.minute(), 17 ); |
LG |
646 |
assert.equal( selected.second(), 3 ); |
|
647 |
done(); |
|
648 |
} |
4ed404
|
649 |
}, timeConstraints: { hours: { max: 6, step: 8 }, minutes: { step: 15 }}}); |
9bb4f7
|
650 |
|
LG |
651 |
trigger( 'mousedown', dt.timeUp( 0 ) ); |
|
652 |
trigger('mouseup', document.body ); |
ef2929
|
653 |
assert.equal( dt.hour().innerHTML, 3 ); |
9bb4f7
|
654 |
trigger( 'mousedown', dt.timeUp( 1 ) ); |
LG |
655 |
trigger( 'mouseup', dt.timeUp( 1 ) ); |
|
656 |
assert.equal( dt.minute().innerHTML, 17 ); |
|
657 |
trigger( 'mousedown', dt.timeUp( 2 ) ); |
|
658 |
trigger( 'mouseup', dt.timeUp( 2 ) ); |
|
659 |
assert.equal( dt.second().innerHTML, 3 ); |
|
660 |
}); |
|
661 |
|
9f1b61
|
662 |
it( 'decrease time with timeConstraints', function( done ) { |
42cbbd
|
663 |
createDatetime({ timeFormat: 'HH:mm:ss:SSS', viewMode: 'time', defaultValue: date, onChange: function( selected ) { |
4ed404
|
664 |
assert.equal( selected.minute(), 47 ); |
LG |
665 |
done(); |
|
666 |
}, timeConstraints: { minutes: { step: 15 }}}); |
|
667 |
|
|
668 |
trigger( 'mousedown', dt.timeDown( 1 ) ); |
|
669 |
trigger( 'mouseup', dt.timeDown( 1 ) ); |
|
670 |
assert.equal( dt.minute().innerHTML, 47 ); |
|
671 |
}); |
|
672 |
|
9f1b61
|
673 |
it( 'invalid input value', function( done ) { |
SE |
674 |
createDatetime({ defaultValue: 'luis', onChange: function( updated ) { |
62fd2f
|
675 |
assert.equal( mDate.format('L LT'), updated.format('L LT') ); |
M |
676 |
done(); |
|
677 |
}}); |
|
678 |
|
|
679 |
assert.equal( dt.input().value, 'luis' ); |
|
680 |
dt.input().value = strDate; |
92a2c6
|
681 |
ev.change( dt.input() ); |
62fd2f
|
682 |
}); |
M |
683 |
|
9f1b61
|
684 |
it( 'invalid moment object as input value', function( done ) { |
0eb496
|
685 |
var value = moment(null); |
9f1b61
|
686 |
createDatetime({ value: value, onChange: function( updated ) { |
0eb496
|
687 |
assert.equal( mDate.format('L LT'), updated.format('L LT') ); |
SE |
688 |
done(); |
|
689 |
}}); |
|
690 |
|
|
691 |
assert.equal( dt.input().value, '' ); |
|
692 |
dt.input().value = strDate; |
|
693 |
ev.change( dt.input() ); |
|
694 |
}); |
|
695 |
|
9f1b61
|
696 |
it( 'delete input value', function( done ) { |
SE |
697 |
createDatetime({ defaultValue: date, onChange: function( date ) { |
62fd2f
|
698 |
assert.equal( date, '' ); |
M |
699 |
done(); |
|
700 |
}}); |
|
701 |
dt.input().value = ''; |
92a2c6
|
702 |
ev.change( dt.input() ); |
62fd2f
|
703 |
}); |
0eb226
|
704 |
|
9f1b61
|
705 |
it( 'strictParsing=true', function( done ) { |
0eb226
|
706 |
var invalidStrDate = strDate + 'x'; |
9f1b61
|
707 |
createDatetime({ defaultValue: '', strictParsing: true, onChange: function( updated ) { |
0eb226
|
708 |
assert.equal( updated, invalidStrDate); |
NB |
709 |
done(); |
|
710 |
}}); |
|
711 |
|
|
712 |
dt.input().value = invalidStrDate; |
92a2c6
|
713 |
ev.change( dt.input() ); |
0eb226
|
714 |
}); |
NB |
715 |
|
9f1b61
|
716 |
it( 'strictParsing=false', function( done ) { |
0eb226
|
717 |
var invalidStrDate = strDate + 'x'; |
9f1b61
|
718 |
createDatetime({ defaultValue: '', strictParsing: false, onChange: function( updated ) { |
0eb226
|
719 |
assert.equal( mDate.format('L LT'), updated.format('L LT') ); |
NB |
720 |
done(); |
|
721 |
}}); |
|
722 |
|
|
723 |
dt.input().value = invalidStrDate; |
92a2c6
|
724 |
ev.change( dt.input() ); |
0eb226
|
725 |
}); |
c306f2
|
726 |
|
9f1b61
|
727 |
it( 'disable months', function() { |
bdad6c
|
728 |
var dateBefore = currentYear + '-06-01'; |
9f1b61
|
729 |
createDatetime({ viewMode: 'months', isValidDate: function(current ) { |
3e7db8
|
730 |
return current.isBefore(moment(dateBefore, 'YYYY-MM-DD')); |
c306f2
|
731 |
}}); |
R |
732 |
assert.equal( dt.month(0).className, 'rdtMonth' ); |
|
733 |
assert.equal( dt.month(4).className, 'rdtMonth' ); |
|
734 |
assert.equal( dt.month(5).className, 'rdtMonth rdtDisabled' ); |
|
735 |
assert.equal( dt.month(11).className, 'rdtMonth rdtDisabled' ); |
|
736 |
}); |
|
737 |
|
9f1b61
|
738 |
it( 'disable years', function() { |
SE |
739 |
createDatetime({ viewMode: 'years', isValidDate: function(current ) { |
c306f2
|
740 |
return current.isBefore(moment('2016-01-01', 'YYYY-MM-DD')); |
R |
741 |
}}); |
e9db5f
|
742 |
assert.equal( dt.year(0).className, 'rdtYear' ); |
c306f2
|
743 |
assert.equal( dt.year(6).className, 'rdtYear' ); |
R |
744 |
assert.equal( dt.year(7).className, 'rdtYear rdtDisabled' ); |
|
745 |
}); |
98310d
|
746 |
|
9f1b61
|
747 |
it( 'persistent valid months going monthView->yearView->monthView', function() { |
bdad6c
|
748 |
var dateBefore = currentYear + '-06-01'; |
9f1b61
|
749 |
createDatetime({ viewMode: 'months', isValidDate: function(current ) { |
bdad6c
|
750 |
return current.isBefore(moment(dateBefore, 'YYYY-MM-DD')); |
SE |
751 |
}}); |
|
752 |
assert.equal( dt.month(4).className, 'rdtMonth' ); |
|
753 |
assert.equal( dt.month(5).className, 'rdtMonth rdtDisabled' ); |
|
754 |
// Go to year view |
|
755 |
ev.click( dt.switcher() ); |
e9db5f
|
756 |
assert.equal( dt.year(0).className, 'rdtYear' ); |
bdad6c
|
757 |
assert.equal( dt.year(9).className, 'rdtYear rdtDisabled' ); |
SE |
758 |
// Go back initial month view, nothing should be changed |
|
759 |
ev.click( dt.year(8) ); |
|
760 |
assert.equal( dt.month(4).className, 'rdtMonth' ); |
|
761 |
assert.equal( dt.month(5).className, 'rdtMonth rdtDisabled' ); |
|
762 |
}); |
|
763 |
|
9f1b61
|
764 |
it( 'locale', function() { |
963148
|
765 |
createDatetime({ locale: 'nl' }); |
SE |
766 |
view = dt.view(); |
|
767 |
var weekDays = []; |
|
768 |
var weekDaysHtmlQuery = view.querySelectorAll('.rdtDays .dow'); |
|
769 |
Array.prototype.forEach.call(weekDaysHtmlQuery, function(el) { |
|
770 |
weekDays.push(el.innerHTML); |
|
771 |
}); |
|
772 |
weekDays = weekDays.splice(0, 7); |
|
773 |
var weekDayNames = ['Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za', 'Zo']; |
|
774 |
weekDays.map(function(weekDayHtml, index) { |
|
775 |
assert.equal( weekDayHtml, weekDayNames[index] ); |
|
776 |
}); |
|
777 |
}); |
98310d
|
778 |
|
9f1b61
|
779 |
it( 'locale in viewMode=months', function() { |
963148
|
780 |
createDatetime({ viewMode: 'months', locale: 'nl' }); |
SE |
781 |
view = dt.view(); |
|
782 |
var thirdMonth = view.querySelectorAll('.rdtMonth')[2].innerHTML; |
|
783 |
var fifthMonth = view.querySelectorAll('.rdtMonth')[4].innerHTML; |
|
784 |
assert.equal( thirdMonth, 'Mrt' ); |
|
785 |
assert.equal( fifthMonth, 'Mei' ); |
|
786 |
}); |
a3c370
|
787 |
|
9f1b61
|
788 |
it( 'closeOnSelect=false', function() { |
a3c370
|
789 |
createDatetime({ closeOnSelect: false }); |
SE |
790 |
view = dt.view(); |
|
791 |
ev.focus( dt.input() ); |
|
792 |
assert.equal( dt.isOpen(), true ); |
|
793 |
ev.click( dt.day( 2 ) ); |
|
794 |
assert.equal( dt.isOpen(), true ); |
|
795 |
}); |
|
796 |
|
9f1b61
|
797 |
it( 'closeOnSelect=true', function() { |
a3c370
|
798 |
createDatetime({ closeOnSelect: true }); |
SE |
799 |
view = dt.view(); |
|
800 |
ev.focus( dt.input() ); |
|
801 |
assert.equal( dt.isOpen(), true ); |
|
802 |
ev.click( dt.day( 2 ) ); |
|
803 |
assert.equal( dt.isOpen(), false ); |
|
804 |
}); |
0d9dc7
|
805 |
}); |