aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/calendar/methods.js
blob: c0948244057466685de2ed1baaafd05b6fbd9a60 (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
define( [
	"qunit",
	"jquery",
	"./helper",
	"ui/widgets/calendar"
], function( QUnit, $, testHelper ) {

QUnit.module( "calendar: methods", {
	beforeEach: function() {
		this.element = $( "#calendar" ).calendar();
		this.widget = this.element.calendar( "widget" );
	},
	afterEach: function() {
		this.element.calendar( "destroy" );
	}
} );

QUnit.test( "destroy", function( assert ) {
	assert.expect( 1 );

	var div = $( "<div>" ).appendTo( "#qunit-fixture" );

	assert.domEqual( div, function() {
		div.calendar().calendar( "destroy" );
	} );
} );

QUnit.test( "enable / disable", function( assert ) {
	assert.expect( 8 );

	this.element.calendar( "disable" );
	assert.ok( this.element.calendar( "option", "disabled" ), "disabled option is set" );
	assert.ok( this.element.hasClass( "ui-calendar-disabled" ), "has disabled widget class name" );
	assert.ok( this.element.hasClass( "ui-state-disabled" ), "has disabled state class name" );
	assert.equal( this.element.attr( "aria-disabled" ), "true", "has ARIA disabled" );

	this.element.calendar( "enable" );
	assert.ok( !this.element.calendar( "option", "disabled" ), "enabled after enable() call" );
	assert.ok( !this.element.hasClass( "ui-calendar-disabled" ), "no longer has disabled widget class name" );
	assert.ok( !this.element.hasClass( "ui-state-disabled" ), "no longer has disabled state class name" );
	assert.equal( this.element.attr( "aria-disabled" ), "false", "no longer has ARIA disabled" );
} );

QUnit.test( "widget", function( assert ) {
	assert.expect( 1 );

	assert.strictEqual( this.widget[ 0 ],  this.element[ 0 ] );
} );

QUnit.test( "value", function( assert ) {
	assert.expect( 3 );

	this.element.calendar( "value", "1/1/14" );
	assert.ok( this.element.find( "button[data-ui-calendar-timestamp]:first" )
			.hasClass( "ui-state-active" ),
		"first day marked as selected"
	);
	assert.equal( this.element.calendar( "value" ), "1/1/14", "getter" );

	this.element.calendar( "value", "abc" );
	assert.equal( this.element.calendar( "value" ), null, "Setting invalid values." );
} );

QUnit.test( "valueAsDate", function( assert ) {
	assert.expect( 11 );

	var minDate, maxDate, dateAndTimeToSet, dateAndTimeClone,
		date1 = testHelper.createDate( 2008, 6 - 1, 4 ),
		date2;

	this.element.calendar( "valueAsDate", testHelper.createDate( 2014, 0, 1 ) );
	assert.ok( this.element.find( "button[data-ui-calendar-timestamp]:first" )
			.hasClass( "ui-state-active" ),
		"First day marked as selected"
	);
	assert.dateEqual( this.element.calendar( "valueAsDate" ), testHelper.createDate( 2014, 0, 1 ), "Getter" );

	this.element.calendar( "destroy" );
	this.element.calendar();
	assert.equal( this.element.calendar( "valueAsDate" ), null, "Set date - default" );

	this.element.calendar( "valueAsDate", date1 );
	assert.dateEqual( this.element.calendar( "valueAsDate" ), date1, "Set date - 2008-06-04" );

	// With minimum / maximum
	date1 = testHelper.createDate( 2008, 1 - 1, 4 );
	date2 = testHelper.createDate( 2008, 6 - 1, 4 );
	minDate = testHelper.createDate( 2008, 2 - 1, 29 );
	maxDate = testHelper.createDate( 2008, 3 - 1, 28 );

	this.element
		.calendar( "option", { min: minDate } )
		.calendar( "valueAsDate", date2 );
	assert.dateEqual(
		this.element.calendar( "valueAsDate" ),
		date2, "Set date min/max - value > min"
	);

	this.element.calendar( "valueAsDate", date1 );
	assert.equal(
		this.element.calendar( "valueAsDate" ),
		null,
		"Set date min/max - value < min"
	);

	this.element
		.calendar( "option", { max: maxDate, min: null } )
		.calendar( "valueAsDate", date1 );
	assert.dateEqual(
		this.element.calendar( "valueAsDate" ),
		date1,
		"Set date min/max - value < max"
	);

	this.element.calendar( "valueAsDate", date2 );
	assert.equal(
		this.element.calendar( "valueAsDate" ),
		null,
		"Set date min/max - value > max"
	);

	this.element
		.calendar( "option", { min: minDate } )
		.calendar( "valueAsDate", date1 );
	assert.equal(
		this.element.calendar( "valueAsDate" ),
		null,
		"Set date min/max - value < min"
	);

	this.element.calendar( "valueAsDate", date2 );
	assert.equal(
		this.element.calendar( "valueAsDate" ),
		null, "Set date min/max - value > max"
	);

	dateAndTimeToSet = testHelper.createDate( 2008, 3 - 1, 28, 1, 11, 0 );
	dateAndTimeClone = testHelper.createDate( 2008, 3 - 1, 28, 1, 11, 0 );
	this.element.calendar( "valueAsDate", dateAndTimeToSet );
	assert.equal(
		dateAndTimeToSet.getTime(),
		dateAndTimeClone.getTime(),
		"Date object passed should not be changed by valueAsDate"
	);
} );

} );