2014-08-28 19:16:51 +00:00
|
|
|
define( [
|
2016-04-03 14:53:34 +00:00
|
|
|
"qunit",
|
2014-08-28 19:16:51 +00:00
|
|
|
"jquery",
|
2020-05-16 07:16:24 +00:00
|
|
|
"lib/helper",
|
2015-01-08 02:51:38 +00:00
|
|
|
"ui/widgets/checkboxradio"
|
2020-05-16 07:16:24 +00:00
|
|
|
], function( QUnit, $, helper ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2020-05-16 07:16:24 +00:00
|
|
|
QUnit.module( "Checkboxradio: core", { afterEach: helper.moduleAfterEach } );
|
2015-10-27 20:05:52 +00:00
|
|
|
|
2016-04-03 14:53:34 +00:00
|
|
|
QUnit.test( "Checkbox - Initial class structure", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2015-09-11 14:48:20 +00:00
|
|
|
var input = $( "#check" ),
|
|
|
|
label = $( "label[for=check]" );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
|
|
|
input.checkboxradio();
|
|
|
|
assert.hasClasses( input, "ui-helper-hidden-accessible ui-checkboxradio" );
|
|
|
|
assert.hasClasses( label, "ui-button ui-widget ui-checkboxradio-label ui-corner-all" );
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2016-04-03 14:53:34 +00:00
|
|
|
QUnit.test( "Radios - Initial class structure", function( assert ) {
|
|
|
|
assert.expect( 6 );
|
2014-08-28 19:16:51 +00:00
|
|
|
var inputs = $( "#radio0 input" ),
|
|
|
|
labels = $( "#radio0 label" );
|
|
|
|
|
|
|
|
inputs.checkboxradio();
|
|
|
|
inputs.each( function() {
|
|
|
|
assert.hasClasses( this, "ui-helper-hidden-accessible" );
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
2014-08-28 19:16:51 +00:00
|
|
|
labels.each( function() {
|
|
|
|
assert.hasClasses( this, "ui-button" );
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
|
|
|
} );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2016-04-03 14:53:34 +00:00
|
|
|
QUnit.test( "Ensure checked after single click on checkbox label button", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2020-05-16 06:36:55 +00:00
|
|
|
$( "#check2" ).checkboxradio().on( "change", function() {
|
2014-08-28 19:16:51 +00:00
|
|
|
var label = $( this ).checkboxradio( "widget" );
|
2016-04-03 14:53:34 +00:00
|
|
|
assert.ok( this.checked, "checked ok" );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
|
|
|
assert.hasClasses( label, "ui-state-active" );
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2024-04-30 22:54:19 +00:00
|
|
|
$( "#check2" ).checkboxradio( "widget" ).simulate( "click" );
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2016-04-03 14:53:34 +00:00
|
|
|
QUnit.test( "Handle form association via form attribute", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2015-10-27 18:35:27 +00:00
|
|
|
|
|
|
|
var radio1 = $( "#crazy-form-1" ).checkboxradio();
|
|
|
|
var radio1Label = radio1.checkboxradio( "widget" );
|
|
|
|
var radio2 = $( "#crazy-form-2" ).checkboxradio();
|
|
|
|
var radio2Label = radio2.checkboxradio( "widget" );
|
|
|
|
|
2020-05-16 06:36:55 +00:00
|
|
|
radio2.on( "change", function() {
|
2016-04-03 14:53:34 +00:00
|
|
|
assert.ok( this.checked, "#2 checked" );
|
|
|
|
assert.ok( !radio1[ 0 ].checked, "#1 not checked" );
|
2015-10-27 18:35:27 +00:00
|
|
|
|
|
|
|
assert.hasClasses( radio2Label, "ui-state-active" );
|
|
|
|
assert.lacksClasses( radio1Label, "ui-state-active" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
radio2Label.simulate( "click" );
|
|
|
|
} );
|
|
|
|
|
2016-04-03 14:53:34 +00:00
|
|
|
QUnit.test( "Checkbox creation requires a label, and finds it in all cases", function( assert ) {
|
|
|
|
assert.expect( 7 );
|
2014-08-28 19:16:51 +00:00
|
|
|
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>",
|
2015-10-06 13:38:32 +00:00
|
|
|
"<span><input type='checkbox' id='t7092e'><span><label for='t7092e'></label></span></span>",
|
2014-08-28 19:16:51 +00:00
|
|
|
"<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" );
|
|
|
|
} );
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2016-04-03 14:53:34 +00:00
|
|
|
QUnit.test( "Calling checkboxradio on an unsupported element throws an error", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2015-10-27 20:05:52 +00:00
|
|
|
|
2020-07-22 14:04:47 +00:00
|
|
|
var error = new Error(
|
|
|
|
"Can't create checkboxradio on element.nodeName=div and element.type=undefined"
|
|
|
|
);
|
2014-08-28 19:16:51 +00:00
|
|
|
assert.raises(
|
|
|
|
function() {
|
|
|
|
$( "<div>" ).checkboxradio();
|
|
|
|
},
|
2020-07-22 14:04:47 +00:00
|
|
|
error,
|
2014-08-28 19:16:51 +00:00
|
|
|
"Proper error thrown"
|
|
|
|
);
|
2015-10-27 20:05:52 +00:00
|
|
|
|
2020-07-22 14:04:47 +00:00
|
|
|
error = new Error(
|
|
|
|
"Can't create checkboxradio on element.nodeName=input and element.type=button"
|
|
|
|
);
|
2014-08-28 19:16:51 +00:00
|
|
|
assert.raises(
|
|
|
|
function() {
|
|
|
|
$( "<input type='button'>" ).checkboxradio();
|
|
|
|
},
|
2020-07-22 14:04:47 +00:00
|
|
|
error,
|
2014-08-28 19:16:51 +00:00
|
|
|
"Proper error thrown"
|
|
|
|
);
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
2015-10-27 20:05:52 +00:00
|
|
|
|
2016-04-03 14:53:34 +00:00
|
|
|
QUnit.test( "Calling checkboxradio on an input with no label throws an error", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2015-10-27 20:05:52 +00:00
|
|
|
|
2020-07-22 14:04:47 +00:00
|
|
|
var error = new Error( "No label found for checkboxradio widget" );
|
2014-08-28 19:16:51 +00:00
|
|
|
assert.raises(
|
|
|
|
function() {
|
|
|
|
$( "<input type='checkbox'>" ).checkboxradio();
|
|
|
|
},
|
2020-07-22 14:04:47 +00:00
|
|
|
error,
|
2014-08-28 19:16:51 +00:00
|
|
|
"Proper error thrown"
|
|
|
|
);
|
2015-09-11 14:48:20 +00:00
|
|
|
} );
|
2014-08-28 19:16:51 +00:00
|
|
|
|
2022-07-14 18:52:02 +00:00
|
|
|
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: "<em>Hi, I'm a label</em>"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
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 + "]"
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2014-08-28 19:16:51 +00:00
|
|
|
} );
|