summaryrefslogtreecommitdiff
path: root/tests/unit-tests/datetimepicker/datetimepicker-tests.js
blob: c35e064e81737bbb06d579d1a25bec1178a01ad9 (plain)
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
$(document).ready( function () {

	module( "Date Time Picker");

	var datetime = $("#datetime")[0];
	var date = $("#date")[0];
	var time = $("#time")[0];
	var custom = $("#custom")[0];

	// trigger pagecreate
	$( "#page-1" ).page();

	var objDatetime = $(datetime).data( "datetimepicker" );
	var objDate = $(date).data( "datetimepicker" );
	var objTime = $(time).data( "datetimepicker" );
	var objCustom = $(custom).data( "datetimepicker" );

	asyncTest( "Auto-initialization", function () {
		ok( objDatetime, "should Date/Time instace created" );
		ok( objDate, "should Date instance created" );
		ok( objTime, "should Time instance created" );
		ok( objCustom, "should Custom format instance created" );

		start();
	});

	asyncTest( "Options", function () {
		equal( objDatetime.options.type, "datetime", "should 'datetime' type created." );
		equal( objDate.options.type, "date", "should 'date' type created." );
		equal( objTime.options.type, "time", "should 'time' type created." );
		equal( objCustom.options.type, "datetime", "should custom format created as 'datetime' type." );
		equal( objCustom.options.format, "MMM dd yyyy hh:mm tt", "should accept custom format string." );
		equal( objCustom.options.date.toString(), new Date("Jun 30 00:00:00 UTC+0000 2012").toString(), "should accept preset date." );

		start();
	});

	asyncTest( "Private Methods", function () {
		ok( ( function () {
			var year = 0,
				expect = false,
				actual = false;

			try {
				for ( year = 1; year < 2100; year++ ) {
					expect = new Date( year, 1, 29 ).getDate() == 29;
					actual = objDatetime._isLeapYear( year );
					if ( expect != actual ) {
						throw "" + year + " is wrong";
					}
				};
			} catch ( exception ) {
				console.log( exception );
				return false;
			}
			return true;
		}()), "should be able to check leap year" );

		var updateFieldTest = function ( format, value, expect ) {
			var target = $('<div data-pat=' + format + '></div>');
			objTime._updateField( target, value );
			return target.text();
		};

		deepEqual( [
			updateFieldTest( "h", 0 ),
			updateFieldTest( "hh", 1 ),
			updateFieldTest( "H", 13 ),
			updateFieldTest( "HH", 9 ),
			updateFieldTest( "m", 9 ),
			updateFieldTest( "mm", 9 ),
			updateFieldTest( "MMM", 3 ),
			updateFieldTest( "MMMM", 3 ),
			updateFieldTest( "yy", 1995 ),
			updateFieldTest( "yyyy", 1995 )
		],
		[
			"12", "01", "13", "09", "9", "09", Globalize.culture().calendar.months.namesAbbr[2], Globalize.culture().calendar.months.names[2], "95", "1995"
		], "should update field to given value with format" );

		ok( ( function () {
			var beforeNoon = objTime.options.date.getHours() < 12;
			objTime._switchAmPm();
			return beforeNoon != objTime.options.date.getHours() < 12;
		}()), "should change AM/PM by AMPM button" );

		deepEqual( [ "MMMM", " ", "dd", " ", "yyyy", " ", "hh", ":", "mm", " ", "dummy space" ],
			objTime._parsePattern( "MMMM dd yyyy hh:mm 'dummy space'" ), "should parse DTF string as array" );

		objDatetime.options.date = new Date( "May 2 18:30:00 2012" );

		var months = Globalize.culture().calendar.months.namesAbbr.slice();
		if ( months.length > 12 ) {
			months.length = 12;
		}

		deepEqual( [
			{ // hour h 6
				values : [ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" ],
				data : [ 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 12 ],
				numItems : 12,
				current : 5
			},
			{ // hour H 6
				values : range( 0, 23 ),
				data : range( 0, 23 ),
				numItems : 24,
				current : 18
			},
			{
				values : months,
				data : range( 1, 12 ),
				numItems : 12,
				current : 4
			}
		],
		[
			objDatetime._populateDataSelector( "hour", "hh", objDatetime ),
			objDatetime._populateDataSelector( "hour", "H", objDatetime ),
			objDatetime._populateDataSelector( "month", "MMM", objDatetime )
		], "should populate data selector by given field and pattern" );

		start();
	});

	asyncTest( "Public Methods", function () {
		objDatetime.value.call( objDatetime, "Jan 1 09:00:00 2012" );
		equal( "2012-01-01T09:00:00", objDatetime.value(), "should set and get value by API" );
		var format = "yyyy MM dd hh mm";
		objDatetime._setFormat( format );
		equal( objDatetime.option("format"), format, "should set type and format" );
		start();
	});

	asyncTest( "Events", function () {
		var str = "May 2 18:00:00 2012";

		$(datetime).bind("date-changed", function(e, date) {
			equal( date, "2012-05-02T18:00:00", "Should invoke event when date changed" );
			start();
		});

		objDatetime.value( str );
	});


});