aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/checkboxradio/core.js
blob: f218cc1cd84112d5edb14d82a8703bcc55bc7a5f (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
define( [
	"qunit",
	"jquery",
	"lib/helper",
	"ui/widgets/checkboxradio"
], function( QUnit, $, helper ) {
"use strict";

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

QUnit.test( "Checkbox - Initial class structure", function( assert ) {
	assert.expect( 2 );
	var input = $( "#check" ),
		label = $( "label[for=check]" );

	input.checkboxradio();
	assert.hasClasses( input, "ui-helper-hidden-accessible ui-checkboxradio" );
	assert.hasClasses( label, "ui-button ui-widget ui-checkboxradio-label ui-corner-all" );
} );

QUnit.test( "Radios - Initial class structure", function( assert ) {
	assert.expect( 6 );
	var inputs = $( "#radio0 input" ),
		labels = $( "#radio0 label" );

	inputs.checkboxradio();
	inputs.each( function() {
		assert.hasClasses( this, "ui-helper-hidden-accessible" );
	} );
	labels.each( function() {
		assert.hasClasses( this, "ui-button" );
	} );
} );

QUnit.test( "Ensure checked after single click on checkbox label button", function( assert ) {
	assert.expect( 2 );

	$( "#check2" ).checkboxradio().on( "change", function() {
		var label = $( this ).checkboxradio( "widget" );
		assert.ok( this.checked, "checked ok" );

		assert.hasClasses( label, "ui-state-active" );
	} );

	$( "#check2" ).checkboxradio( "widget" ).simulate( "click" );
} );

QUnit.test( "Handle form association via form attribute", function( assert ) {
	assert.expect( 4 );

	var radio1 = $( "#crazy-form-1" ).checkboxradio();
	var radio1Label = radio1.checkboxradio( "widget" );
	var radio2 = $( "#crazy-form-2" ).checkboxradio();
	var radio2Label = radio2.checkboxradio( "widget" );

	radio2.on( "change", function() {
		assert.ok( this.checked, "#2 checked" );
		assert.ok( !radio1[ 0 ].checked, "#1 not checked" );

		assert.hasClasses( radio2Label, "ui-state-active" );
		assert.lacksClasses( radio1Label, "ui-state-active" );
	} );

	radio2Label.simulate( "click" );
} );

QUnit.test( "Checkbox creation requires a label, and finds it in all cases", function( assert ) {
	assert.expect( 7 );
	var groups = [
		"<span><label for='t7092a'></label><input type='checkbox' id='t7092a'></span>",
		"<span><input type='checkbox' id='t7092b'><label for='t7092b'></label></span>",
		"<span><span><input type='checkbox' id='t7092c'></span><label for='t7092c'></label></span>",
		"<span><input type='checkbox' id='t7092d'></span><span><label for='t7092d'></label></span>",
		"<span><input type='checkbox' id='t7092e'><span><label for='t7092e'></label></span></span>",
		"<span><label><input type='checkbox' id='t7092f'></label></span>",
		"<span><input type='checkbox' id='check:7534'><label for='check:7534'>Label</label></span>"
	];

	$.each( groups, function( index, markup ) {
		var group = $( markup );

		group.find( "input[type=checkbox]" ).checkboxradio();
		assert.hasClasses( group.find( "label" ), "ui-button" );
	} );
} );

QUnit.test( "Calling checkboxradio on an unsupported element throws an error", function( assert ) {
	assert.expect( 2 );

	var error = new Error(
		"Can't create checkboxradio on element.nodeName=div and element.type=undefined"
	);
	assert.raises(
		function() {
			$( "<div>" ).checkboxradio();
		},
		error,
		"Proper error thrown"
	);

	error = new Error(
		"Can't create checkboxradio on element.nodeName=input and element.type=button"
	);
	assert.raises(
		function() {
			$( "<input type='button'>" ).checkboxradio();
		},
		error,
		"Proper error thrown"
	);
} );

QUnit.test( "Calling checkboxradio on an input with no label throws an error", function( assert ) {
	assert.expect( 1 );

	var error = new Error( "No label found for checkboxradio widget" );
	assert.raises(
		function() {
			$( "<input type='checkbox'>" ).checkboxradio();
		},
		error,
		"Proper error thrown"
	);
} );

QUnit.test( "Inheriting label from initial HTML", 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 } );

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

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

} );