jquery-ui/tests/unit/button/options.js

186 lines
7.1 KiB
JavaScript
Raw Permalink Normal View History

define( [
2016-04-03 14:43:27 +00:00
"qunit",
"jquery",
"ui/widgets/button"
2016-04-03 14:43:27 +00:00
], function( QUnit, $ ) {
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.module( "button: options" );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "disabled, explicit value", function( assert ) {
assert.expect( 8 );
var element = $( "#button" ).button( { disabled: false } );
2016-04-03 14:43:27 +00:00
assert.strictEqual( element.button( "option", "disabled" ), false, "disabled option set to false" );
assert.strictEqual( element.prop( "disabled" ), false, "Disabled property is false" );
assert.lacksClasses( element.button( "widget" ), "ui-state-disabled ui-button-disabled" );
element = $( "#button" ).button( { disabled: true } );
assert.hasClasses( element.button( "widget" ), "ui-state-disabled" );
2016-04-03 14:43:27 +00:00
assert.strictEqual( element.button( "widget" ).attr( "aria-disabled" ), undefined,
"element does not get aria-disabled" );
assert.hasClasses( element.button( "widget" ), "ui-button-disabled" );
2016-04-03 14:43:27 +00:00
assert.strictEqual( element.button( "option", "disabled" ), true, "disabled option set to true" );
assert.strictEqual( element.prop( "disabled" ), true, "Disabled property is set" );
} );
// We are testing the default here because the default null is a special value which means to check
// the DOM. We need to make sure this happens correctly. Checking the options should never return
// null, it should always be true or false.
2016-04-03 14:43:27 +00:00
QUnit.test( "disabled, null", function( assert ) {
assert.expect( 4 );
var element = $( "#button" ),
elementDisabled = $( "#button-disabled" );
element.add( elementDisabled ).button( { disabled: null } );
2016-04-03 14:43:27 +00:00
assert.strictEqual( element.button( "option", "disabled" ), false, "disabled option set to false" );
assert.strictEqual( element.prop( "disabled" ), false, "element is disabled" );
assert.strictEqual( elementDisabled.button( "option", "disabled" ), true,
"disabled option set to true" );
2016-04-03 14:43:27 +00:00
assert.strictEqual( elementDisabled.prop( "disabled" ), true, "element is disabled" );
} );
2016-04-03 14:43:27 +00:00
QUnit.test( "showLabel, false, without icon", function( assert ) {
assert.expect( 4 );
var button = $( "#button" ).button( {
showLabel: false
} );
assert.lacksClasses( button, "ui-button-icon-only" );
2016-04-03 14:43:27 +00:00
assert.strictEqual( button.button( "option", "showLabel" ), true,
"showLabel false only allowed if icon true" );
button.button( "option", "showLabel", false );
assert.lacksClasses( button, "ui-button-icon-only" );
2016-04-03 14:43:27 +00:00
assert.strictEqual( button.button( "option", "showLabel" ), true,
"showLabel false only allowed if icon true" );
} );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "showLabel, false, with icon", function( assert ) {
assert.expect( 1 );
var button = $( "#button" ).button( {
showLabel: false,
icon: "iconclass"
} );
assert.hasClasses( button, "ui-button ui-corner-all ui-widget ui-button-icon-only" );
} );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "label, default", function( assert ) {
assert.expect( 2 );
var button = $( "#button" ).button();
2016-04-03 14:43:27 +00:00
assert.deepEqual( button.text(), "Label" );
assert.deepEqual( button.button( "option", "label" ), "Label" );
} );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "label, with html markup", function( assert ) {
assert.expect( 3 );
var button = $( "#button2" ).button();
2016-04-03 14:43:27 +00:00
assert.deepEqual( button.text(), "label with span" );
assert.deepEqual( button.html().toLowerCase(), "label <span>with span</span>" );
assert.deepEqual( button.button( "option", "label" ).toLowerCase(), "label <span>with span</span>" );
} );
2016-04-03 14:43:27 +00:00
QUnit.test( "label, explicit value", function( assert ) {
assert.expect( 2 );
var button = $( "#button" ).button( {
2010-01-20 14:00:14 +00:00
label: "xxx"
} );
2016-04-03 14:43:27 +00:00
assert.deepEqual( button.text(), "xxx" );
assert.deepEqual( button.button( "option", "label" ), "xxx" );
} );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "label, default, with input type submit", function( assert ) {
assert.expect( 2 );
var button = $( "#submit" ).button();
2016-04-03 14:43:27 +00:00
assert.deepEqual( button.val(), "Label" );
assert.deepEqual( button.button( "option", "label" ), "Label" );
} );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "label, explicit value, with input type submit", function( assert ) {
assert.expect( 2 );
var button = $( "#submit" ).button( {
2010-01-20 14:00:14 +00:00
label: "xxx"
} );
2016-04-03 14:43:27 +00:00
assert.deepEqual( button.val(), "xxx" );
assert.deepEqual( button.button( "option", "label" ), "xxx" );
} );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "icon", function( assert ) {
assert.expect( 4 );
var button = $( "#button" ).button( {
showLabel: false,
icon: "iconclass"
} ),
icon = button.find( ".ui-icon" );
assert.hasClasses( icon, "iconclass" );
2016-04-03 14:43:27 +00:00
assert.equal( icon.length, 1, "button with icon option set has icon" );
button.button( "option", "icon", false );
2016-04-03 14:43:27 +00:00
assert.equal( button.find( ".ui-icon" ).length, 0, "setting icon to false removes the icon" );
button.button( "option", "icon", "iconclass" );
2016-04-03 14:43:27 +00:00
assert.ok( button.find( ".ui-icon" ).length, "setting icon to a value adds the icon" );
} );
2010-01-20 14:00:14 +00:00
2016-04-03 14:43:27 +00:00
QUnit.test( "icon position", function( assert ) {
assert.expect( 22 );
var button = $( "#button" ).button( {
icon: "ui-icon-gear"
} ),
icon = button.find( ".ui-icon" ),
space = button.find( ".ui-button-icon-space" );
2016-04-03 14:43:27 +00:00
assert.equal( icon.length, 1, "button with icon option set has icon" );
assert.equal( button.button( "option", "iconPosition" ), "beginning",
"Button has iconPosition beginning by default" );
2016-04-03 14:43:27 +00:00
assert.equal( button.contents()[ 0 ], icon[ 0 ], "icon is prepended when position is begining" );
assert.equal( icon.next()[ 0 ], space[ 0 ], "icon is followed by a space when position is begining" );
assert.equal( space.length, 1,
"ui-button-icon-space contains a breaking space iconPosition:beginning" );
assert.lacksClasses( icon, "ui-widget-icon-block" );
button.button( "option", "iconPosition", "end" );
icon = button.find( ".ui-icon" );
space = button.find( ".ui-button-icon-space" );
2016-04-03 14:43:27 +00:00
assert.equal( icon.length, 1, "Changing position to end does not re-create or duplicate icon" );
assert.equal( button.button( "option", "iconPosition" ), "end", "Button has iconPosition end" );
assert.equal( button.contents().last()[ 0 ], icon[ 0 ], "icon is appended when position is end" );
assert.equal( icon.prev()[ 0 ], space[ 0 ], "icon is preceeded by a space when position is end" );
assert.equal( space.length, 1,
"ui-button-icon-space contains a breaking space iconPosition:beginning" );
assert.lacksClasses( icon, "ui-widget-icon-block" );
button.button( "option", "iconPosition", "top" );
icon = button.find( ".ui-icon" );
2016-04-03 14:43:27 +00:00
assert.equal( icon.length, 1, "Changing position to top does not re-create or duplicate icon" );
assert.equal( button.button( "option", "iconPosition" ), "top", "Button has iconPosition top" );
assert.equal( button.contents()[ 0 ], icon[ 0 ], "icon is prepended when position is top" );
assert.ok( !button.find( "ui-button-icon-space" ).length,
"Button should not have an iconSpace with position: top" );
assert.hasClasses( icon, "ui-widget-icon-block" );
button.button( "option", "iconPosition", "bottom" );
icon = button.find( ".ui-icon" );
2016-04-03 14:43:27 +00:00
assert.equal( icon.length, 1, "Changing position to bottom does not re-create or duplicate icon" );
assert.equal( button.button( "option", "iconPosition" ), "bottom", "Button has iconPosition top" );
assert.equal( button.contents().last()[ 0 ], icon[ 0 ], "icon is prepended when position is bottom" );
assert.ok( !button.find( "ui-button-icon-space" ).length,
"Button should not have an iconSpace with position: bottom" );
assert.hasClasses( icon, "ui-widget-icon-block" );
} );
} );