Skip to content

Commit 7af808c

Browse files
committed
Add tests for pick events, remove unnecessary date manip
1 parent 87e98c6 commit 7af808c

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

js/bootstrap-datepicker.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@
415415
this.viewDate.setDate(1);
416416
if (target.is('.month')) {
417417
var month = target.parent().find('span').index(target);
418-
this.viewDate.setDate(1);
419418
this.viewDate.setMonth(month);
420419
this.element.trigger({
421420
type: 'pickMonth',
@@ -424,8 +423,6 @@
424423
} else {
425424
var year = parseInt(target.text(), 10)||0;
426425
this.viewDate.setFullYear(year);
427-
this.viewDate.setDate(1);
428-
this.viewDate.setMonth(0);
429426
this.element.trigger({
430427
type: 'pickYear',
431428
date: this.viewDate

tests/suites/events.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
module('Events', {
2+
setup: function(){
3+
this.input = $('<input type="text" value="31-03-2011">')
4+
.appendTo('#qunit-fixture')
5+
.datepicker({format: "dd-mm-yyyy"})
6+
.focus(); // Activate for visibility checks
7+
this.dp = this.input.data('datepicker')
8+
this.picker = this.dp.picker;
9+
},
10+
teardown: function(){
11+
this.picker.remove();
12+
}
13+
});
14+
15+
test('Selecting a year from decade view triggers pickYear', function(){
16+
var target,
17+
triggered = 0;
18+
19+
this.input.on('pickYear', function(){
20+
triggered++;
21+
});
22+
23+
equal(this.dp.viewMode, 0);
24+
target = this.picker.find('.datepicker-days thead th.switch');
25+
ok(target.is(':visible'), 'View switcher is visible');
26+
27+
target.click();
28+
ok(this.picker.find('.datepicker-months').is(':visible'), 'Month picker is visible');
29+
equal(this.dp.viewMode, 1);
30+
// Not modified when switching modes
31+
datesEqual(this.dp.viewDate, new Date(2011, 2, 31));
32+
datesEqual(this.dp.date, new Date(2011, 2, 31));
33+
34+
target = this.picker.find('.datepicker-months thead th.switch');
35+
ok(target.is(':visible'), 'View switcher is visible');
36+
37+
target.click();
38+
ok(this.picker.find('.datepicker-years').is(':visible'), 'Year picker is visible');
39+
equal(this.dp.viewMode, 2);
40+
// Not modified when switching modes
41+
datesEqual(this.dp.viewDate, new Date(2011, 2, 31));
42+
datesEqual(this.dp.date, new Date(2011, 2, 31));
43+
44+
// Change years to test internal state changes
45+
target = this.picker.find('.datepicker-years tbody span:contains(2010)');
46+
target.click();
47+
equal(this.dp.viewMode, 1);
48+
// Only viewDate modified
49+
datesEqual(this.dp.viewDate, new Date(2010, 2, 1));
50+
datesEqual(this.dp.date, new Date(2011, 2, 31));
51+
equal(triggered, 1);
52+
});
53+
54+
test('Selecting a month from year view triggers pickMonth', function(){
55+
var target,
56+
triggered = 0;
57+
58+
this.input.on('pickMonth', function(){
59+
triggered++;
60+
});
61+
62+
equal(this.dp.viewMode, 0);
63+
target = this.picker.find('.datepicker-days thead th.switch');
64+
ok(target.is(':visible'), 'View switcher is visible');
65+
66+
target.click();
67+
ok(this.picker.find('.datepicker-months').is(':visible'), 'Month picker is visible');
68+
equal(this.dp.viewMode, 1);
69+
// Not modified when switching modes
70+
datesEqual(this.dp.viewDate, new Date(2011, 2, 31));
71+
datesEqual(this.dp.date, new Date(2011, 2, 31));
72+
73+
target = this.picker.find('.datepicker-months tbody span:contains(Apr)');
74+
target.click();
75+
equal(this.dp.viewMode, 0);
76+
// Only viewDate modified
77+
datesEqual(this.dp.viewDate, new Date(2011, 3, 1));
78+
datesEqual(this.dp.date, new Date(2011, 2, 31));
79+
equal(triggered, 1);
80+
});

tests/tests.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<script src="suites/keyboard_navigation/2012.js"></script>
3030
<script src="suites/keyboard_navigation/2011.js"></script>
3131
<script src="suites/component.js"></script>
32+
<script src="suites/events.js"></script>
3233
<script src="suites/options.js"></script>
3334
</head>
3435
<body>

0 commit comments

Comments
 (0)