Core: Clean up selector tests

This commit is contained in:
Scott González 2014-07-30 09:45:00 -04:00
parent 4ad45991e6
commit 9895cb58b4

View File

@ -1,160 +1,163 @@
/* (function( $ ) {
* selector unit tests
*/
(function($) {
module("core - selectors"); module( "core - selectors" );
function isFocusable(selector, msg) { function isFocusable( selector, msg ) {
QUnit.push($(selector).is(":focusable"), null, null, msg + " - selector " + selector + " is focusable"); QUnit.push( $( selector ).is( ":focusable" ), null, null,
msg + " - selector " + selector + " is focusable" );
} }
function isNotFocusable(selector, msg) { function isNotFocusable( selector, msg ) {
QUnit.push($(selector).length && !$(selector).is(":focusable"), null, null, msg + " - selector " + selector + " is not focusable"); QUnit.push( $( selector ).length && !$( selector ).is(":focusable"), null, null,
msg + " - selector " + selector + " is not focusable" );
} }
function isTabbable(selector, msg) { function isTabbable( selector, msg ) {
QUnit.push($(selector).is(":tabbable"), null, null, msg + " - selector " + selector + " is tabbable"); QUnit.push( $( selector ).is( ":tabbable" ), null, null,
msg + " - selector " + selector + " is tabbable" );
} }
function isNotTabbable(selector, msg) { function isNotTabbable( selector, msg ) {
QUnit.push($(selector).length && !$(selector).is(":tabbable"), null, null, msg + " - selector " + selector + " is not tabbable"); QUnit.push( $( selector ).length && !$( selector ).is( ":tabbable" ), null, null,
msg + " - selector " + selector + " is not tabbable" );
} }
test("data", function() { test( "data", function() {
expect(15); expect( 15 );
var el; var element;
function shouldHaveData(msg) {
ok(el.is(":data(test)"), msg); function shouldHaveData( msg ) {
} ok( element.is( ":data(test)" ), msg );
function shouldNotHaveData(msg) {
ok(!el.is(":data(test)"), msg);
} }
el = $("<div>"); function shouldNotHaveData( msg ) {
shouldNotHaveData("data never set"); ok( !element.is( ":data(test)" ), msg );
}
el = $("<div>").data("test", null); element = $( "<div>" );
shouldNotHaveData("data is null"); shouldNotHaveData( "data never set" );
el = $("<div>").data("test", true); element = $( "<div>" ).data( "test", null );
shouldHaveData("data set to true"); shouldNotHaveData( "data is null" );
el = $("<div>").data("test", false); element = $( "<div>" ).data( "test", true );
shouldNotHaveData("data set to false"); shouldHaveData( "data set to true" );
el = $("<div>").data("test", 0); element = $( "<div>" ).data( "test", false );
shouldNotHaveData("data set to 0"); shouldNotHaveData( "data set to false" );
el = $("<div>").data("test", 1); element = $( "<div>" ).data( "test", 0 );
shouldHaveData("data set to 1"); shouldNotHaveData( "data set to 0" );
el = $("<div>").data("test", ""); element = $( "<div>" ).data( "test", 1 );
shouldNotHaveData("data set to empty string"); shouldHaveData( "data set to 1" );
el = $("<div>").data("test", "foo"); element = $( "<div>" ).data( "test", "" );
shouldHaveData("data set to string"); shouldNotHaveData( "data set to empty string" );
el = $("<div>").data("test", []); element = $( "<div>" ).data( "test", "foo" );
shouldHaveData("data set to empty array"); shouldHaveData( "data set to string" );
el = $("<div>").data("test", [1]); element = $( "<div>" ).data( "test", [] );
shouldHaveData("data set to array"); shouldHaveData( "data set to empty array" );
el = $("<div>").data("test", {}); element = $( "<div>" ).data( "test", [ 1 ] );
shouldHaveData("data set to empty object"); shouldHaveData( "data set to array" );
el = $("<div>").data("test", {foo: "bar"}); element = $( "<div>" ).data( "test", {} );
shouldHaveData("data set to object"); shouldHaveData( "data set to empty object" );
el = $("<div>").data("test", new Date()); element = $( "<div>" ).data( "test", { foo: "bar" } );
shouldHaveData("data set to date"); shouldHaveData( "data set to object" );
el = $("<div>").data("test", /test/); element = $( "<div>" ).data( "test", new Date() );
shouldHaveData("data set to regexp"); shouldHaveData( "data set to date" );
el = $("<div>").data("test", function() {}); element = $( "<div>" ).data( "test", /test/ );
shouldHaveData("data set to function"); shouldHaveData( "data set to regexp" );
element = $( "<div>" ).data( "test", function() {} );
shouldHaveData( "data set to function" );
}); });
test("focusable - visible, enabled elements", function() { test( "focusable - visible, enabled elements", function() {
expect(18); expect( 18 );
isNotFocusable("#formNoTabindex", "form"); isNotFocusable( "#formNoTabindex", "form" );
isFocusable("#formTabindex", "form with tabindex"); isFocusable( "#formTabindex", "form with tabindex" );
isFocusable("#visibleAncestor-inputTypeNone", "input, no type"); isFocusable( "#visibleAncestor-inputTypeNone", "input, no type" );
isFocusable("#visibleAncestor-inputTypeText", "input, type text"); isFocusable( "#visibleAncestor-inputTypeText", "input, type text" );
isFocusable("#visibleAncestor-inputTypeCheckbox", "input, type checkbox"); isFocusable( "#visibleAncestor-inputTypeCheckbox", "input, type checkbox" );
isFocusable("#visibleAncestor-inputTypeRadio", "input, type radio"); isFocusable( "#visibleAncestor-inputTypeRadio", "input, type radio" );
isFocusable("#visibleAncestor-inputTypeButton", "input, type button"); isFocusable( "#visibleAncestor-inputTypeButton", "input, type button" );
isNotFocusable("#visibleAncestor-inputTypeHidden", "input, type hidden"); isNotFocusable( "#visibleAncestor-inputTypeHidden", "input, type hidden" );
isFocusable("#visibleAncestor-button", "button"); isFocusable( "#visibleAncestor-button", "button" );
isFocusable("#visibleAncestor-select", "select"); isFocusable( "#visibleAncestor-select", "select" );
isFocusable("#visibleAncestor-textarea", "textarea"); isFocusable( "#visibleAncestor-textarea", "textarea" );
isFocusable("#visibleAncestor-object", "object"); isFocusable( "#visibleAncestor-object", "object" );
isFocusable("#visibleAncestor-anchorWithHref", "anchor with href"); isFocusable( "#visibleAncestor-anchorWithHref", "anchor with href" );
isNotFocusable("#visibleAncestor-anchorWithoutHref", "anchor without href"); isNotFocusable( "#visibleAncestor-anchorWithoutHref", "anchor without href" );
isNotFocusable("#visibleAncestor-span", "span"); isNotFocusable( "#visibleAncestor-span", "span" );
isNotFocusable("#visibleAncestor-div", "div"); isNotFocusable( "#visibleAncestor-div", "div" );
isFocusable("#visibleAncestor-spanWithTabindex", "span with tabindex"); isFocusable( "#visibleAncestor-spanWithTabindex", "span with tabindex" );
isFocusable("#visibleAncestor-divWithNegativeTabindex", "div with tabindex"); isFocusable( "#visibleAncestor-divWithNegativeTabindex", "div with tabindex" );
}); });
test("focusable - disabled elements", function() { test( "focusable - disabled elements", function() {
expect(9); expect( 9 );
isNotFocusable("#disabledElement-inputTypeNone", "input, no type"); isNotFocusable( "#disabledElement-inputTypeNone", "input, no type" );
isNotFocusable("#disabledElement-inputTypeText", "input, type text"); isNotFocusable( "#disabledElement-inputTypeText", "input, type text" );
isNotFocusable("#disabledElement-inputTypeCheckbox", "input, type checkbox"); isNotFocusable( "#disabledElement-inputTypeCheckbox", "input, type checkbox" );
isNotFocusable("#disabledElement-inputTypeRadio", "input, type radio"); isNotFocusable( "#disabledElement-inputTypeRadio", "input, type radio" );
isNotFocusable("#disabledElement-inputTypeButton", "input, type button"); isNotFocusable( "#disabledElement-inputTypeButton", "input, type button" );
isNotFocusable("#disabledElement-inputTypeHidden", "input, type hidden"); isNotFocusable( "#disabledElement-inputTypeHidden", "input, type hidden" );
isNotFocusable("#disabledElement-button", "button"); isNotFocusable( "#disabledElement-button", "button" );
isNotFocusable("#disabledElement-select", "select"); isNotFocusable( "#disabledElement-select", "select" );
isNotFocusable("#disabledElement-textarea", "textarea"); isNotFocusable( "#disabledElement-textarea", "textarea" );
}); });
test("focusable - hidden styles", function() { test( "focusable - hidden styles", function() {
expect(8); expect( 8 );
isNotFocusable("#displayNoneAncestor-input", "input, display: none parent"); isNotFocusable( "#displayNoneAncestor-input", "input, display: none parent" );
isNotFocusable("#displayNoneAncestor-span", "span with tabindex, display: none parent"); isNotFocusable( "#displayNoneAncestor-span", "span with tabindex, display: none parent" );
isNotFocusable("#visibilityHiddenAncestor-input", "input, visibility: hidden parent"); isNotFocusable( "#visibilityHiddenAncestor-input", "input, visibility: hidden parent" );
isNotFocusable("#visibilityHiddenAncestor-span", "span with tabindex, visibility: hidden parent"); isNotFocusable( "#visibilityHiddenAncestor-span", "span with tabindex, visibility: hidden parent" );
isNotFocusable("#displayNone-input", "input, display: none"); isNotFocusable( "#displayNone-input", "input, display: none" );
isNotFocusable("#visibilityHidden-input", "input, visibility: hidden"); isNotFocusable( "#visibilityHidden-input", "input, visibility: hidden" );
isNotFocusable("#displayNone-span", "span with tabindex, display: none"); isNotFocusable( "#displayNone-span", "span with tabindex, display: none" );
isNotFocusable("#visibilityHidden-span", "span with tabindex, visibility: hidden"); isNotFocusable( "#visibilityHidden-span", "span with tabindex, visibility: hidden" );
}); });
test("focusable - natively focusable with various tabindex", function() { test( "focusable - natively focusable with various tabindex", function() {
expect(4); expect( 4 );
isFocusable("#inputTabindex0", "input, tabindex 0"); isFocusable( "#inputTabindex0", "input, tabindex 0" );
isFocusable("#inputTabindex10", "input, tabindex 10"); isFocusable( "#inputTabindex10", "input, tabindex 10" );
isFocusable("#inputTabindex-1", "input, tabindex -1"); isFocusable( "#inputTabindex-1", "input, tabindex -1" );
isFocusable("#inputTabindex-50", "input, tabindex -50"); isFocusable( "#inputTabindex-50", "input, tabindex -50" );
}); });
test("focusable - not natively focusable with various tabindex", function() { test( "focusable - not natively focusable with various tabindex", function() {
expect(4); expect( 4 );
isFocusable("#spanTabindex0", "span, tabindex 0"); isFocusable( "#spanTabindex0", "span, tabindex 0" );
isFocusable("#spanTabindex10", "span, tabindex 10"); isFocusable( "#spanTabindex10", "span, tabindex 10" );
isFocusable("#spanTabindex-1", "span, tabindex -1"); isFocusable( "#spanTabindex-1", "span, tabindex -1" );
isFocusable("#spanTabindex-50", "span, tabindex -50"); isFocusable( "#spanTabindex-50", "span, tabindex -50" );
}); });
test("focusable - area elements", function() { test( "focusable - area elements", function() {
expect( 3 ); expect( 3 );
isFocusable("#areaCoordsHref", "coords and href"); isFocusable( "#areaCoordsHref", "coords and href" );
isFocusable("#areaNoCoordsHref", "href but no coords"); isFocusable( "#areaNoCoordsHref", "href but no coords" );
isNotFocusable("#areaNoImg", "not associated with an image"); isNotFocusable( "#areaNoImg", "not associated with an image" );
}); });
test( "focusable - dimensionless parent with overflow", function() { test( "focusable - dimensionless parent with overflow", function() {
@ -163,83 +166,83 @@ test( "focusable - dimensionless parent with overflow", function() {
isFocusable( "#dimensionlessParent", "input" ); isFocusable( "#dimensionlessParent", "input" );
}); });
test("tabbable - visible, enabled elements", function() { test( "tabbable - visible, enabled elements", function() {
expect(18); expect( 18 );
isNotTabbable("#formNoTabindex", "form"); isNotTabbable( "#formNoTabindex", "form" );
isTabbable("#formTabindex", "form with tabindex"); isTabbable( "#formTabindex", "form with tabindex" );
isTabbable("#visibleAncestor-inputTypeNone", "input, no type"); isTabbable( "#visibleAncestor-inputTypeNone", "input, no type" );
isTabbable("#visibleAncestor-inputTypeText", "input, type text"); isTabbable( "#visibleAncestor-inputTypeText", "input, type text" );
isTabbable("#visibleAncestor-inputTypeCheckbox", "input, type checkbox"); isTabbable( "#visibleAncestor-inputTypeCheckbox", "input, type checkbox" );
isTabbable("#visibleAncestor-inputTypeRadio", "input, type radio"); isTabbable( "#visibleAncestor-inputTypeRadio", "input, type radio" );
isTabbable("#visibleAncestor-inputTypeButton", "input, type button"); isTabbable( "#visibleAncestor-inputTypeButton", "input, type button" );
isNotTabbable("#visibleAncestor-inputTypeHidden", "input, type hidden"); isNotTabbable( "#visibleAncestor-inputTypeHidden", "input, type hidden" );
isTabbable("#visibleAncestor-button", "button"); isTabbable( "#visibleAncestor-button", "button" );
isTabbable("#visibleAncestor-select", "select"); isTabbable( "#visibleAncestor-select", "select" );
isTabbable("#visibleAncestor-textarea", "textarea"); isTabbable( "#visibleAncestor-textarea", "textarea" );
isTabbable("#visibleAncestor-object", "object"); isTabbable( "#visibleAncestor-object", "object" );
isTabbable("#visibleAncestor-anchorWithHref", "anchor with href"); isTabbable( "#visibleAncestor-anchorWithHref", "anchor with href" );
isNotTabbable("#visibleAncestor-anchorWithoutHref", "anchor without href"); isNotTabbable( "#visibleAncestor-anchorWithoutHref", "anchor without href" );
isNotTabbable("#visibleAncestor-span", "span"); isNotTabbable( "#visibleAncestor-span", "span" );
isNotTabbable("#visibleAncestor-div", "div"); isNotTabbable( "#visibleAncestor-div", "div" );
isTabbable("#visibleAncestor-spanWithTabindex", "span with tabindex"); isTabbable( "#visibleAncestor-spanWithTabindex", "span with tabindex" );
isNotTabbable("#visibleAncestor-divWithNegativeTabindex", "div with tabindex"); isNotTabbable( "#visibleAncestor-divWithNegativeTabindex", "div with tabindex" );
}); });
test("tabbable - disabled elements", function() { test( "tabbable - disabled elements", function() {
expect(9); expect( 9 );
isNotTabbable("#disabledElement-inputTypeNone", "input, no type"); isNotTabbable( "#disabledElement-inputTypeNone", "input, no type" );
isNotTabbable("#disabledElement-inputTypeText", "input, type text"); isNotTabbable( "#disabledElement-inputTypeText", "input, type text" );
isNotTabbable("#disabledElement-inputTypeCheckbox", "input, type checkbox"); isNotTabbable( "#disabledElement-inputTypeCheckbox", "input, type checkbox" );
isNotTabbable("#disabledElement-inputTypeRadio", "input, type radio"); isNotTabbable( "#disabledElement-inputTypeRadio", "input, type radio" );
isNotTabbable("#disabledElement-inputTypeButton", "input, type button"); isNotTabbable( "#disabledElement-inputTypeButton", "input, type button" );
isNotTabbable("#disabledElement-inputTypeHidden", "input, type hidden"); isNotTabbable( "#disabledElement-inputTypeHidden", "input, type hidden" );
isNotTabbable("#disabledElement-button", "button"); isNotTabbable( "#disabledElement-button", "button" );
isNotTabbable("#disabledElement-select", "select"); isNotTabbable( "#disabledElement-select", "select" );
isNotTabbable("#disabledElement-textarea", "textarea"); isNotTabbable( "#disabledElement-textarea", "textarea" );
}); });
test("tabbable - hidden styles", function() { test( "tabbable - hidden styles", function() {
expect(8); expect( 8 );
isNotTabbable("#displayNoneAncestor-input", "input, display: none parent"); isNotTabbable( "#displayNoneAncestor-input", "input, display: none parent" );
isNotTabbable("#displayNoneAncestor-span", "span with tabindex, display: none parent"); isNotTabbable( "#displayNoneAncestor-span", "span with tabindex, display: none parent" );
isNotTabbable("#visibilityHiddenAncestor-input", "input, visibility: hidden parent"); isNotTabbable( "#visibilityHiddenAncestor-input", "input, visibility: hidden parent" );
isNotTabbable("#visibilityHiddenAncestor-span", "span with tabindex, visibility: hidden parent"); isNotTabbable( "#visibilityHiddenAncestor-span", "span with tabindex, visibility: hidden parent" );
isNotTabbable("#displayNone-input", "input, display: none"); isNotTabbable( "#displayNone-input", "input, display: none" );
isNotTabbable("#visibilityHidden-input", "input, visibility: hidden"); isNotTabbable( "#visibilityHidden-input", "input, visibility: hidden" );
isNotTabbable("#displayNone-span", "span with tabindex, display: none"); isNotTabbable( "#displayNone-span", "span with tabindex, display: none" );
isNotTabbable("#visibilityHidden-span", "span with tabindex, visibility: hidden"); isNotTabbable( "#visibilityHidden-span", "span with tabindex, visibility: hidden" );
}); });
test("tabbable - natively tabbable with various tabindex", function() { test( "tabbable - natively tabbable with various tabindex", function() {
expect(4); expect( 4 );
isTabbable("#inputTabindex0", "input, tabindex 0"); isTabbable( "#inputTabindex0", "input, tabindex 0" );
isTabbable("#inputTabindex10", "input, tabindex 10"); isTabbable( "#inputTabindex10", "input, tabindex 10" );
isNotTabbable("#inputTabindex-1", "input, tabindex -1"); isNotTabbable( "#inputTabindex-1", "input, tabindex -1" );
isNotTabbable("#inputTabindex-50", "input, tabindex -50"); isNotTabbable( "#inputTabindex-50", "input, tabindex -50" );
}); });
test("tabbable - not natively tabbable with various tabindex", function() { test( "tabbable - not natively tabbable with various tabindex", function() {
expect(4); expect( 4 );
isTabbable("#spanTabindex0", "span, tabindex 0"); isTabbable( "#spanTabindex0", "span, tabindex 0" );
isTabbable("#spanTabindex10", "span, tabindex 10"); isTabbable( "#spanTabindex10", "span, tabindex 10" );
isNotTabbable("#spanTabindex-1", "span, tabindex -1"); isNotTabbable( "#spanTabindex-1", "span, tabindex -1" );
isNotTabbable("#spanTabindex-50", "span, tabindex -50"); isNotTabbable( "#spanTabindex-50", "span, tabindex -50" );
}); });
test("tabbable - area elements", function() { test( "tabbable - area elements", function() {
expect( 3 ); expect( 3 );
isTabbable("#areaCoordsHref", "coords and href"); isTabbable( "#areaCoordsHref", "coords and href" );
isTabbable("#areaNoCoordsHref", "href but no coords"); isTabbable( "#areaNoCoordsHref", "href but no coords" );
isNotTabbable("#areaNoImg", "not associated with an image"); isNotTabbable( "#areaNoImg", "not associated with an image" );
}); });
test( "tabbable - dimensionless parent with overflow", function() { test( "tabbable - dimensionless parent with overflow", function() {
@ -248,4 +251,4 @@ test( "tabbable - dimensionless parent with overflow", function() {
isTabbable( "#dimensionlessParent", "input" ); isTabbable( "#dimensionlessParent", "input" );
}); });
})(jQuery); })( jQuery );