aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/checkboxradio/methods.js
blob: f6c94f94fa853b688b7aa75f98a271d0716ec425 (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
define( [
	"qunit",
	"jquery",
	"lib/helper",
	"ui/widgets/checkboxradio"
], function( QUnit, $, helper ) {
"use strict";

QUnit.module( "Checkboxradio: methods", { afterEach: helper.moduleAfterEach }  );

$.each( [ "checkbox", "radio" ], function( index, value ) {
	QUnit.test( value + ": refresh", function( assert ) {
		var widget, icon,
			checkbox = value === "checkbox",
			input = $( "#" + value + "-method-refresh" );

		assert.expect( checkbox ? 11 : 8 );

		input.checkboxradio();

		widget = input.checkboxradio( "widget" );
		icon = widget.find( ".ui-icon" );
		assert.strictEqual( icon.length, 1,
			"There is initally one icon" );

		icon.remove();
		input.checkboxradio( "refresh" );
		icon = widget.find( ".ui-icon" );
		assert.strictEqual( icon.length, 1,
			"Icon is recreated on refresh if absent" );
		assert.hasClasses( icon, "ui-icon-blank" );
		if ( checkbox ) {
			assert.lacksClasses( icon, "ui-icon-check" );
		}
		assert.lacksClasses( widget, "ui-checkboxradio-checked" );

		input.prop( "checked", true );
		input.checkboxradio( "refresh" );
		if ( checkbox ) {
			assert.hasClasses( icon, "ui-icon-check" );
		}
		assert[ !checkbox ? "hasClasses" : "lacksClasses" ]( icon, "ui-icon-blank" );
		assert.hasClasses( widget, "ui-checkboxradio-checked" );

		input.prop( "checked", false );
		input.checkboxradio( "refresh" );
		assert.hasClasses( icon, "ui-icon-blank" );
		if ( checkbox ) {
			assert.lacksClasses( icon, "ui-icon-check" );
		}
		assert.lacksClasses( widget, "ui-checkboxradio-checked" );
	} );

	QUnit.test( value + ": destroy", function( assert ) {
		assert.expect( 1 );
		assert.domEqual( "#" + value + "-method-destroy", function() {
			$( "#" + value + "-method-destroy" ).checkboxradio().checkboxradio( "destroy" );
		} );
	} );

	QUnit.test( value + ": disable / enable", function( assert ) {
		assert.expect( 4 );
		var input = $( "#" + value + "-method-disable" ),
			widget = input.checkboxradio().checkboxradio( "widget" );

		input.checkboxradio( "disable" );
		assert.hasClasses( widget, "ui-state-disabled" );
		assert.strictEqual( input.is( ":disabled" ), true,
			value + " is disabled when disable is called" );

		input.checkboxradio( "enable" );
		assert.lacksClasses( widget, "ui-state-disabled" );
		assert.strictEqual( input.is( ":disabled" ), false,
			value + " has disabled prop removed when enable is called" );
	} );

	QUnit.test(  value + ": widget returns the label", function( assert ) {
		assert.expect( 1 );

		var input = $( "#" + value + "-method-refresh" ),
			label = $( "#" + value + "-method-refresh-label" );

		input.checkboxradio();
		assert.strictEqual( input.checkboxradio( "widget" )[ 0 ], label[ 0 ],
			"widget method returns label" );
	} );
} );

QUnit.test( "Input wrapped in a label preserved on refresh", function( assert ) {
	var input = $( "#label-with-no-for" ).checkboxradio(),
		element = input.checkboxradio( "widget" );

	assert.expect( 1 );

	input.checkboxradio( "refresh" );
	assert.strictEqual( input.parent()[ 0 ], element[ 0 ], "Input preserved" );
} );

QUnit.test( "Initial text label not turned to HTML on refresh", function( assert ) {
	var tests = [
		{
			id: "label-with-no-for-with-html",
			expectedLabel: "<strong>Hi</strong>, <em>I'm a label</em>"
		},
		{
			id: "label-with-no-for-with-text",
			expectedLabel: "Hi, I'm a label"
		},
		{
			id: "label-with-no-for-with-html-like-text",
			expectedLabel: "&lt;em&gt;Hi, I'm a label&lt;/em&gt;"
		}
	];

	assert.expect( tests.length );

	tests.forEach( function( testData ) {
		var id = testData.id;
		var expectedLabel = testData.expectedLabel;
		var inputElem = $( "#" + id );
		var labelElem = inputElem.parent();

		inputElem.checkboxradio( { icon: false } );
		inputElem.checkboxradio( "refresh" );

		var labelWithoutInput = labelElem.clone();
		labelWithoutInput.find( "input" ).remove();

		assert.strictEqual(
			labelWithoutInput.html().trim(),
			expectedLabel.trim(),
			"Label correct [" + id + "]"
		);
	} );
} );

} );