| 1 |
define( [ |
| 2 |
"qunit", |
| 3 |
"jquery", |
| 4 |
"lib/helper", |
| 5 |
"ui/widgets/checkboxradio" |
| 6 |
], function( QUnit, $, helper ) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
QUnit.module( "Checkboxradio: options", { afterEach: helper.moduleAfterEach } ); |
| 10 |
|
| 11 |
function assertDisabled( checkbox, assert ) { |
| 12 |
assert.hasClasses( checkbox.checkboxradio( "widget" ), "ui-state-disabled", |
| 13 |
"label gets ui-state-disabled" ); |
| 14 |
assert.strictEqual( checkbox.is( ":disabled" ), true, "checkbox is disabled" ); |
| 15 |
} |
| 16 |
|
| 17 |
function assertEnabled( checkbox, assert ) { |
| 18 |
assert.lacksClasses( checkbox.checkboxradio( "widget" ), "ui-state-disabled", |
| 19 |
"label has ui-state-disabled removed when disabled set to false" ); |
| 20 |
assert.strictEqual( checkbox.is( ":disabled" ), false, |
| 21 |
"checkbox has disabled prop removed when disabled set to false" ); |
| 22 |
} |
| 23 |
|
| 24 |
QUnit.test( "disabled", function( assert ) { |
| 25 |
assert.expect( 6 ); |
| 26 |
|
| 27 |
var checkbox = $( "#checkbox-option-disabled" ); |
| 28 |
checkbox.checkboxradio( { |
| 29 |
disabled: true |
| 30 |
} ); |
| 31 |
|
| 32 |
assertDisabled( checkbox, assert ); |
| 33 |
|
| 34 |
checkbox.checkboxradio( "option", "disabled", false ); |
| 35 |
assertEnabled( checkbox, assert ); |
| 36 |
|
| 37 |
checkbox.checkboxradio( "option", "disabled", true ); |
| 38 |
assertDisabled( checkbox, assert ); |
| 39 |
} ); |
| 40 |
|
| 41 |
QUnit.test( "disabled - prop true on init", function( assert ) { |
| 42 |
assert.expect( 2 ); |
| 43 |
var checkbox = $( "#checkbox-option-disabled" ); |
| 44 |
|
| 45 |
checkbox.prop( "disabled", true ); |
| 46 |
checkbox.checkboxradio(); |
| 47 |
|
| 48 |
assertDisabled( checkbox, assert ); |
| 49 |
} ); |
| 50 |
|
| 51 |
QUnit.test( "disabled - explicit null value, checks the DOM", function( assert ) { |
| 52 |
assert.expect( 2 ); |
| 53 |
var checkbox = $( "#checkbox-option-disabled" ); |
| 54 |
|
| 55 |
checkbox.prop( "disabled", true ); |
| 56 |
checkbox.checkboxradio( { |
| 57 |
disabled: null |
| 58 |
} ); |
| 59 |
assertDisabled( checkbox, assert ); |
| 60 |
} ); |
| 61 |
|
| 62 |
function assertNoIcon( assert, checkbox ) { |
| 63 |
assert.strictEqual( checkbox.checkboxradio( "widget" ).find( "span.ui-icon" ).length, 0, |
| 64 |
"Label does not contain an icon" ); |
| 65 |
} |
| 66 |
|
| 67 |
function assertIcon( checkbox, icon, assert ) { |
| 68 |
var iconElement = checkbox.checkboxradio( "widget" ).find( ".ui-icon" ); |
| 69 |
|
| 70 |
icon = icon || "blank"; |
| 71 |
assert.strictEqual( iconElement.length, 1, |
| 72 |
"Label contains icon" ); |
| 73 |
assert.hasClasses( iconElement, "ui-checkboxradio-icon ui-corner-all ui-icon " + |
| 74 |
"ui-icon-background ui-icon-" + icon, |
| 75 |
"Icon has proper classes" ); |
| 76 |
if ( icon === "blank" ) { |
| 77 |
assert.lacksClasses( iconElement, "ui-icon-check ui-state-checked" ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
QUnit.test( "icon - false on init", function( assert ) { |
| 82 |
var checkbox = $( "#checkbox-option-icon" ); |
| 83 |
|
| 84 |
assert.expect( 1 ); |
| 85 |
|
| 86 |
checkbox.checkboxradio( { icon: false } ); |
| 87 |
assertNoIcon( assert, checkbox ); |
| 88 |
} ); |
| 89 |
|
| 90 |
QUnit.test( "icon - default unchecked", function( assert ) { |
| 91 |
var checkbox = $( "#checkbox-option-icon" ); |
| 92 |
|
| 93 |
assert.expect( 3 ); |
| 94 |
|
| 95 |
checkbox.checkboxradio(); |
| 96 |
assertIcon( checkbox, false, assert ); |
| 97 |
} ); |
| 98 |
|
| 99 |
QUnit.test( "icon - default checked", function( assert ) { |
| 100 |
var checkbox = $( "#checkbox-option-icon" ).attr( "checked", "checked" ); |
| 101 |
|
| 102 |
assert.expect( 2 ); |
| 103 |
|
| 104 |
checkbox.checkboxradio(); |
| 105 |
assertIcon( checkbox, "check ui-state-checked", assert ); |
| 106 |
} ); |
| 107 |
|
| 108 |
QUnit.test( "icon", function( assert ) { |
| 109 |
var checkbox = $( "#checkbox-option-icon" ); |
| 110 |
|
| 111 |
assert.expect( 9 ); |
| 112 |
|
| 113 |
checkbox.prop( "checked", true ); |
| 114 |
|
| 115 |
checkbox.checkboxradio(); |
| 116 |
assertIcon( checkbox, "check ui-state-checked", assert ); |
| 117 |
|
| 118 |
checkbox.checkboxradio( "option", "icon", false ); |
| 119 |
assertNoIcon( assert, checkbox ); |
| 120 |
|
| 121 |
checkbox.checkboxradio( "option", "icon", true ); |
| 122 |
assertIcon( checkbox, "check ui-state-checked", assert ); |
| 123 |
|
| 124 |
checkbox.checkboxradio( "option", "icon", false ); |
| 125 |
assertNoIcon( assert, checkbox ); |
| 126 |
|
| 127 |
checkbox.checkboxradio( "option", "icon", true ); |
| 128 |
checkbox.prop( "checked", false ).checkboxradio( "refresh" ); |
| 129 |
assertIcon( checkbox, false, assert ); |
| 130 |
} ); |
| 131 |
|
| 132 |
QUnit.test( "label - default", function( assert ) { |
| 133 |
var checkbox = $( "#checkbox-option-label" ), |
| 134 |
widget; |
| 135 |
|
| 136 |
assert.expect( 2 ); |
| 137 |
|
| 138 |
checkbox.checkboxradio(); |
| 139 |
widget = checkbox.checkboxradio( "widget" ); |
| 140 |
assert.strictEqual( checkbox.checkboxradio( "option", "label" ), |
| 141 |
"checkbox label", "When no value passed on create text from dom is used for option" ); |
| 142 |
assert.strictEqual( String.prototype.trim.call( widget.text() ), |
| 143 |
"checkbox label", "When no value passed on create text from dom is used in dom" ); |
| 144 |
} ); |
| 145 |
|
| 146 |
QUnit.test( "label - explicit value", function( assert ) { |
| 147 |
assert.expect( 5 ); |
| 148 |
var checkbox = $( "#checkbox-option-label" ).checkboxradio( { |
| 149 |
label: "foo" |
| 150 |
} ), |
| 151 |
widget = checkbox.checkboxradio( "widget" ), |
| 152 |
icon = widget.find( ".ui-icon" ), |
| 153 |
iconSpace = widget.find( ".ui-checkboxradio-icon-space" ); |
| 154 |
|
| 155 |
assert.strictEqual( checkbox.checkboxradio( "option", "label" ), |
| 156 |
"foo", "When value is passed on create value is used for option" ); |
| 157 |
assert.strictEqual( String.prototype.trim.call( widget.text() ), |
| 158 |
"foo", "When value is passed on create value is used in dom" ); |
| 159 |
assert.strictEqual( icon.length, 1, |
| 160 |
"Icon is preserved when label is set on init when wrapped in label" ); |
| 161 |
assert.strictEqual( iconSpace.length, 1, |
| 162 |
"Icon space is preserved when label is set on init when wrapped in label" ); |
| 163 |
assert.strictEqual( $( "#checkbox-option-label" ).length, 1, |
| 164 |
"Element is preserved when label is set on init when wrapped in label" ); |
| 165 |
} ); |
| 166 |
|
| 167 |
QUnit.test( "label - explicit null value", function( assert ) { |
| 168 |
var checkbox = $( "#checkbox-option-label" ), |
| 169 |
widget; |
| 170 |
|
| 171 |
assert.expect( 2 ); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
checkbox.checkboxradio( { |
| 177 |
label: null |
| 178 |
} ); |
| 179 |
widget = checkbox.checkboxradio( "widget" ); |
| 180 |
assert.strictEqual( checkbox.checkboxradio( "option", "label" ), |
| 181 |
"checkbox label", "When null is passed on create text from dom is used for option" ); |
| 182 |
assert.strictEqual( String.prototype.trim.call( widget.text() ), |
| 183 |
"checkbox label", "When null is passed on create text from dom is used in dom" ); |
| 184 |
|
| 185 |
} ); |
| 186 |
|
| 187 |
QUnit.test( "label", function( assert ) { |
| 188 |
assert.expect( 4 ); |
| 189 |
|
| 190 |
var checkbox = $( "#checkbox-option-label" ), |
| 191 |
widget; |
| 192 |
|
| 193 |
checkbox.checkboxradio(); |
| 194 |
widget = checkbox.checkboxradio( "widget" ); |
| 195 |
checkbox.checkboxradio( "option", "label", "bar" ); |
| 196 |
assert.strictEqual( checkbox.checkboxradio( "option", "label" ), |
| 197 |
"bar", "When value is passed value is used for option" ); |
| 198 |
assert.strictEqual( String.prototype.trim.call( widget.text() ), |
| 199 |
"bar", "When value is passed value is used in dom" ); |
| 200 |
|
| 201 |
checkbox.checkboxradio( "option", "label", null ); |
| 202 |
assert.strictEqual( checkbox.checkboxradio( "option", "label" ), |
| 203 |
"bar", "When null is passed text from dom is used for option" ); |
| 204 |
assert.strictEqual( String.prototype.trim.call( widget.text() ), |
| 205 |
"bar", "When null is passed text from dom is used in dom" ); |
| 206 |
} ); |
| 207 |
|
| 208 |
} ); |