From 5ed1046a4ac271a1f20cc1f19aa50bec15cd9704 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Scott=20Gonz=C3=A1lez?= Date: Mon, 24 Jan 2011 19:20:09 -0500 Subject: [PATCH] Accordion tests: Cleanup. --- tests/unit/accordion/accordio.html | 43 ++-- tests/unit/accordion/accordion.html | 43 ++-- tests/unit/accordion/accordion_core.js | 2 +- tests/unit/accordion/accordion_defaults.js | 29 ++- .../accordion_defaults_deprecated.js | 43 ++-- tests/unit/accordion/accordion_deprecated.js | 52 +++- tests/unit/accordion/accordion_events.js | 2 +- tests/unit/accordion/accordion_methods.js | 105 ++++---- tests/unit/accordion/accordion_options.js | 228 +++++++++++------- tests/unit/accordion/accordion_tickets.js | 9 +- tests/unit/testsuite.js | 74 ++++-- 11 files changed, 369 insertions(+), 261 deletions(-) diff --git a/tests/unit/accordion/accordio.html b/tests/unit/accordion/accordio.html index c104df4d5..db202fe43 100644 --- a/tests/unit/accordion/accordio.html +++ b/tests/unit/accordion/accordio.html @@ -17,30 +17,37 @@ - + diff --git a/tests/unit/accordion/accordion.html b/tests/unit/accordion/accordion.html index 88226be31..23380000e 100644 --- a/tests/unit/accordion/accordion.html +++ b/tests/unit/accordion/accordion.html @@ -20,30 +20,37 @@ - + diff --git a/tests/unit/accordion/accordion_core.js b/tests/unit/accordion/accordion_core.js index fa0facc80..601d2ed44 100644 --- a/tests/unit/accordion/accordion_core.js +++ b/tests/unit/accordion/accordion_core.js @@ -1,6 +1,6 @@ (function( $ ) { -module( "accordion: core" ); +module( "accordion: core", accordionSetupTeardown() ); test( "handle click on header-descendant", function() { var ac = $( "#navigation" ).accordion(); diff --git a/tests/unit/accordion/accordion_defaults.js b/tests/unit/accordion/accordion_defaults.js index c2904d39a..95a478057 100644 --- a/tests/unit/accordion/accordion_defaults.js +++ b/tests/unit/accordion/accordion_defaults.js @@ -1,16 +1,15 @@ - -var accordion_defaults = { - active: 0, - animated: false, - collapsible: false, - disabled: false, - event: "click", - header: "> li > :first-child,> :not(li):even", - heightStyle: "auto", - icons: { - "activeHeader": "ui-icon-triangle-1-s", - "header": "ui-icon-triangle-1-e" +commonWidgetTests( "accordion", { + defaults: { + active: 0, + animated: "slide", + collapsible: false, + disabled: false, + event: "click", + header: "> li > :first-child,> :not(li):even", + heightStyle: "auto", + icons: { + "activeHeader": "ui-icon-triangle-1-s", + "header": "ui-icon-triangle-1-e" + } } -}; - -commonWidgetTests( "accordion", { defaults: accordion_defaults } ); +}); diff --git a/tests/unit/accordion/accordion_defaults_deprecated.js b/tests/unit/accordion/accordion_defaults_deprecated.js index 386354e83..b90b472e2 100644 --- a/tests/unit/accordion/accordion_defaults_deprecated.js +++ b/tests/unit/accordion/accordion_defaults_deprecated.js @@ -1,22 +1,21 @@ - -var accordion_defaults = { - active: 0, - animated: false, - autoHeight: true, - clearStyle: false, - collapsible: false, - disabled: false, - event: "click", - fillSpace: false, - header: "> li > :first-child,> :not(li):even", - heightStyle: null, - icons: { - "activeHeader": null, - "header": "ui-icon-triangle-1-e", - "headerSelected": "ui-icon-triangle-1-s" - }, - navigation: false, - navigationFilter: function() {} -}; - -commonWidgetTests( "accordion", { defaults: accordion_defaults } ); +commonWidgetTests( "accordion", { + defaults: { + active: 0, + animated: "slide", + autoHeight: true, + clearStyle: false, + collapsible: false, + disabled: false, + event: "click", + fillSpace: false, + header: "> li > :first-child,> :not(li):even", + heightStyle: null, + icons: { + "activeHeader": null, + "header": "ui-icon-triangle-1-e", + "headerSelected": "ui-icon-triangle-1-s" + }, + navigation: false, + navigationFilter: function() {} + } +}); diff --git a/tests/unit/accordion/accordion_deprecated.js b/tests/unit/accordion/accordion_deprecated.js index b219cf8c3..2fa3ca41a 100644 --- a/tests/unit/accordion/accordion_deprecated.js +++ b/tests/unit/accordion/accordion_deprecated.js @@ -5,7 +5,7 @@ (function($) { -module("accordion (deprecated): expanded active option, activate method"); +module("accordion (deprecated): expanded active option, activate method", accordionSetupTeardown() ); test("activate", function() { var expected = $('#list1').accordion(), @@ -96,7 +96,7 @@ test("{ active: jQuery Object }", function() { -module("accordion (deprecated) - height options"); +module("accordion (deprecated) - height options", accordionSetupTeardown() ); test("{ autoHeight: true }, default", function() { equalHeights($('#navigation').accordion({ autoHeight: true }), 95, 130); @@ -141,7 +141,7 @@ test("{ fillSpace: true } with multiple siblings", function() { -module("accordion (deprecated) - icons"); +module("accordion (deprecated) - icons", accordionSetupTeardown() ); test("change headerSelected option after creation", function() { var list = $("#list1"); @@ -151,4 +151,50 @@ test("change headerSelected option after creation", function() { equals( $( "#list1 span.deprecated" ).length, 1); }); + + + + +module( "accordion (deprecated) - resize", accordionSetupTeardown() ); + +test( "resize", function() { + var expected = $( "#navigation" ) + .parent() + .height( 300 ) + .end() + .accordion({ + heightStyle: "fill" + }); + equalHeights( expected, 246, 258 ); + + expected.parent().height( 500 ); + expected.accordion( "resize" ); + equalHeights( expected, 446, 458 ); +}); + + + + +module( "accordion (deprecated) - navigation", accordionSetupTeardown() ); + +test("{ navigation: true, navigationFilter: header }", function() { + $("#navigation").accordion({ + navigation: true, + navigationFilter: function() { + return /\?p=1\.1\.3$/.test(this.href); + } + }); + equals( $("#navigation .ui-accordion-content:eq(2)").size(), 1, "third content active" ); +}); + +test("{ navigation: true, navigationFilter: content }", function() { + $("#navigation").accordion({ + navigation: true, + navigationFilter: function() { + return /\?p=1\.1\.3\.2$/.test(this.href); + } + }); + equals( $("#navigation .ui-accordion-content:eq(2)").size(), 1, "third content active" ); +}); + })(jQuery); diff --git a/tests/unit/accordion/accordion_events.js b/tests/unit/accordion/accordion_events.js index ff9590eac..e0dbe82eb 100644 --- a/tests/unit/accordion/accordion_events.js +++ b/tests/unit/accordion/accordion_events.js @@ -1,6 +1,6 @@ (function( $ ) { -module( "accordion: events" ); +module( "accordion: events", accordionSetupTeardown() ); // TODO: verify correct elements in ui properties // TODO: add tests for switching between active panels (not collapsed) diff --git a/tests/unit/accordion/accordion_methods.js b/tests/unit/accordion/accordion_methods.js index c6754d277..e0ed734f6 100644 --- a/tests/unit/accordion/accordion_methods.js +++ b/tests/unit/accordion/accordion_methods.js @@ -1,71 +1,48 @@ -/* - * accordion_methods.js - */ -(function($) { - -module("accordion: methods"); - -test("init", function() { - $("
").appendTo('body').accordion().remove(); - ok(true, '.accordion() called on element'); - - $([]).accordion().remove(); - ok(true, '.accordion() called on empty collection'); - - $('
').accordion().remove(); - ok(true, '.accordion() called on disconnected DOMElement - never connected'); - - $('
').appendTo('body').remove().accordion().remove(); - ok(true, '.accordion() called on disconnected DOMElement - removed'); - - var el = $('
').accordion(); - var foo = el.accordion("option", "foo"); - el.remove(); - ok(true, 'arbitrary option getter after init'); - - $('
').accordion().accordion("option", "foo", "bar").remove(); - ok(true, 'arbitrary option setter after init'); -}); - -test("destroy", function() { - var beforeHtml = $("#list1").find("div").css("font-style", "normal").end().parent().html(); - var afterHtml = $("#list1").accordion().accordion("destroy").parent().html(); - // Opera 9 outputs role="" instead of removing the attribute like everyone else - if ($.browser.opera) { - afterHtml = afterHtml.replace(/ role=""/g, ""); - } +(function( $ ) { + +module( "accordion: methods", accordionSetupTeardown() ); + +test( "destroy", function() { + var beforeHtml = $( "#list1" ) + .find( "div" ) + .css( "font-style", "normal" ) + .end() + .parent() + .html(); + var afterHtml = $( "#list1" ) + .accordion() + .accordion( "destroy" ) + .parent() + .html() + // Opera 9 outputs role="" instead of removing the attribute like everyone else + .replace( / role=""/g, "" ); equal( afterHtml, beforeHtml ); }); -test("enable", function() { - var expected = $('#list1').accordion(), - actual = expected.accordion('enable'); - equals(actual, expected, 'enable is chainable'); - state(expected, 1, 0, 0) -}); - -test("disable", function() { - var expected = $('#list1').accordion(), - actual = expected.accordion('disable'); - equals(actual, expected, 'disable is chainable'); - - state(expected, 1, 0, 0) - expected.accordion("option", "active", 1); - state(expected, 1, 0, 0) - expected.accordion("enable"); - expected.accordion("option", "active", 1); - state(expected, 0, 1, 0) +test( "enable/disable", function() { + var accordion = $('#list1').accordion(); + state( accordion, 1, 0, 0 ); + accordion.accordion( "disable" ); + accordion.accordion( "option", "active", 1 ); + state( accordion, 1, 0, 0 ); + accordion.accordion( "enable" ); + accordion.accordion( "option", "active", 1 ); + state( accordion, 0, 1, 0 ); }); -test("refresh", function() { - var expected = $('#navigation').parent().height(300).end().accordion({ - heightStyle: "fill" - }); - equalHeights(expected, 246, 258); - - expected.parent().height(500); - expected.accordion("refresh"); - equalHeights(expected, 446, 458); +test( "refresh", function() { + var expected = $( "#navigation" ) + .parent() + .height( 300 ) + .end() + .accordion({ + heightStyle: "fill" + }); + equalHeights( expected, 246, 258 ); + + expected.parent().height( 500 ); + expected.accordion( "refresh" ); + equalHeights( expected, 446, 458 ); }); -})(jQuery); +}( jQuery ) ); diff --git a/tests/unit/accordion/accordion_options.js b/tests/unit/accordion/accordion_options.js index c7b4038ef..0e02f0372 100644 --- a/tests/unit/accordion/accordion_options.js +++ b/tests/unit/accordion/accordion_options.js @@ -1,128 +1,176 @@ -/* - * accordion_options.js - */ -(function($) { +(function( $ ) { -module("accordion: options"); +module( "accordion: options", accordionSetupTeardown() ); -test("{ active: first child }, default", function() { - var ac = $("#list1").accordion(); - equals( ac.accordion('option', 'active'), 0); - state(ac, 1, 0, 0) +test( "{ active: default }", function() { + var ac = $( "#list1" ).accordion(); + equals( ac.accordion( "option", "active" ), 0 ); + state( ac, 1, 0, 0 ); }); -test("{ active: false }", function() { - var ac = $("#list1").accordion({ +test( "{ active: false }", function() { + var ac = $( "#list1" ).accordion({ active: false, collapsible: true }); - state(ac, 0, 0, 0); - equals( $("#list1 .ui-accordion-header.ui-state-active").size(), 0, "no headers selected" ); - equals( $("#list1").accordion('option', 'active'), false); + state( ac, 0, 0, 0 ); + equals( ac.find( ".ui-accordion-header.ui-state-active" ).size(), 0, "no headers selected" ); + equals( ac.accordion( "option", "active" ), false ); + + // TODO: fix active: false when not collapsible +// ac.accordion( "option", "collapsible", false ); +// state( ac, 1, 0, 0 ); +// equals( ac.accordion( "option", "active" ), 0 ); +// +// ac.accordion( "destroy" ); +// ac.accordion({ +// active: false +// }); +// state( ac, 1, 0, 0 ); +// strictEqual( ac.accordion( "option", "active" ), 0 ); }); -test("{ active: Number }", function() { - expect(4); - $("#list1").accordion({ - active: 0 +test( "{ active: Number }", function() { + var ac = $( "#list1" ).accordion({ + active: 2 }); - equals( $("#list1").accordion('option', 'active'), 0); + equals( ac.accordion( "option", "active" ), 2 ); + state( ac, 0, 0, 1 ); - $("#list1").accordion('option', 'active', 1); - equals( $("#list1").accordion('option', 'active'), 1); + ac.accordion( "option", "active", 0 ); + equals( ac.accordion( "option", "active" ), 0 ); + state( ac, 1, 0, 0 ); - $('.ui-accordion-header:eq(2)', '#list1').click(); - equals( $("#list1").accordion('option', 'active'), 2); + ac.find( ".ui-accordion-header" ).eq( 1 ).click(); + equals( ac.accordion( "option", "active" ), 1 ); + state( ac, 0, 1, 0 ); - $("#list1").accordion('option', 'active', 0); - equals( $("#list1").accordion('option', 'active'), 0); + ac.accordion( "option", "active", 10 ); + equals( ac.accordion( "option", "active" ), 1 ); + state( ac, 0, 1, 0 ); }); -test("{ heightStyle: 'auto' }, default", function() { - equalHeights($('#navigation').accordion({ heightStyle: 'auto' }), 95, 130); -}); +if ( $.uiBackCompat === false ) { + test( "{ active: -Number }", function() { + // TODO: fix initializing with negative value + var ac = $( "#list1" ).accordion({ +// active: -1 + }); +// equals( ac.accordion( "option", "active" ), 2 ); +// state( ac, 0, 0, 1 ); + + ac.accordion( "option", "active", -2 ); + equals( ac.accordion( "option", "active" ), 1 ); + state( ac, 0, 1, 0 ); + + ac.accordion( "option", "active", -10 ); + equals( ac.accordion( "option", "active" ), 1 ); + state( ac, 0, 1, 0 ); + + ac.accordion( "option", "active", -3 ); + equals( ac.accordion( "option", "active" ), 0 ); + state( ac, 1, 0, 0 ); + }); +} + +// TODO: add animation tests -test("{ heightStyle: 'content' }", function() { - var accordion = $('#navigation').accordion({ heightStyle: 'content' }); - var sizes = []; - accordion.find(".ui-accordion-content").each(function() { - sizes.push($(this).height()); +test( "{ collapsible: false }", function() { + var ac = $( "#list1" ).accordion({ + active: 1 }); - ok( sizes[0] >= 70 && sizes[0] <= 90, "was " + sizes[0] ); - ok( sizes[1] >= 98 && sizes[1] <= 126, "was " + sizes[1] ); - ok( sizes[2] >= 42 && sizes[2] <= 54, "was " + sizes[2] ); -}); -test("{ collapsible: false }, default", function() { - var ac = $("#list1").accordion(); - ac.accordion("option", "active", false); - state(ac, 1, 0, 0); + ac.accordion( "option", "active", false ); + equal( ac.accordion( "option", "active" ), 1 ); + state( ac, 0, 1, 0 ); + + ac.find( ".ui-accordion-header" ).eq( 1 ).click(); + equal( ac.accordion( "option", "active" ), 1 ); + state( ac, 0, 1, 0 ); }); -test("{ collapsible: true }", function() { - var ac = $("#list1").accordion({ +test( "{ collapsible: true }", function() { + var ac = $( "#list1" ).accordion({ active: 1, collapsible: true }); - var header = $('#list1 .ui-accordion-header:eq(1)').click(); - equals( $("#list1").accordion('option', 'active'), false); - state(ac, 0, 0, 0); + + // TODO: fix setting active to false +// ac.accordion( "option", "active", false ); +// equal( ac.accordion( "option", "active" ), false ); +// state( ac, 0, 0, 0 ); + + ac.accordion( "option", "active", 1 ); + equal( ac.accordion( "option", "active" ), 1 ); + state( ac, 0, 1, 0 ); + + ac.find( ".ui-accordion-header" ).eq( 1 ).click(); + equals( ac.accordion( "option", "active" ), false ); + state( ac, 0, 0, 0 ); }); -test("{ heightStyle: 'fill' }", function() { - $("#navigationWrapper").height(500); - equalHeights($('#navigation').accordion({ heightStyle: 'fill' }), 446, 458); +// TODO: add event tests + +// TODO: add more header tests +test( "{ header: default }", function() { + // default: > li > :first-child,> :not(li):even + // > :not(li):even + state( $( "#list1" ).accordion(), 1, 0, 0); + // > li > :first-child + state( $( "#navigation" ).accordion(), 1, 0, 0); }); -test("{ header: '> li > :first-child,> :not(li):even' }, default", function() { - state($("#list1").accordion(), 1, 0, 0); - state($("#navigation").accordion(), 1, 0, 0); +test( "{ heightStyle: 'auto' }", function() { + var ac = $( "#navigation" ).accordion({ heightStyle: "auto" }); + equalHeights( ac, 95, 130 ); }); -test("{ icons: false }", function() { - var list = $("#list1"); - function icons(on) { - same($("span.ui-icon", list).length, on ? 3 : 0); - same( list.hasClass("ui-accordion-icons"), on ); - } - list.accordion(); - icons(true); - list.accordion("destroy").accordion({ - icons: false - }); - icons(false); - list.accordion("option", "icons", $.ui.accordion.prototype.options.icons); - icons(true); - list.accordion("option", "icons", false); - icons(false); +test( "{ heightStyle: 'content' }", function() { + var ac = $( "#navigation" ).accordion({ heightStyle: "content" }); + var sizes = ac.find( ".ui-accordion-content" ).map(function() { + return $( this ).height(); + }).get(); + ok( sizes[ 0 ] >= 70 && sizes[ 0 ] <= 90, "was " + sizes[ 0 ] ); + ok( sizes[ 1 ] >= 98 && sizes[ 1 ] <= 126, "was " + sizes[ 1 ] ); + ok( sizes[ 2 ] >= 42 && sizes[ 2 ] <= 54, "was " + sizes[ 2 ] ); }); -test("{ icons: { activeHeader : 'test' } }", function() { - var list = $("#list1"); - list.accordion( { icons: { "activeHeader": "test" } } ); - equals( $( "#list1 span.test" ).length, 1); - list.accordion("option", "icons", { "activeHeader": "news" } ); - equals( $( "#list1 span.test" ).length, 0); - equals( $( "#list1 span.news" ).length, 1); +test( "{ heightStyle: 'fill' }", function() { + $( "#navigationWrapper" ).height( 500 ); + var ac = $( "#navigation" ).accordion({ heightStyle: "fill" }); + equalHeights( ac, 446, 458); + ac.accordion( "destroy" ); + + $( "
" ).height( 100 ).appendTo( "#navigationWrapper" ); + ac.accordion({ heightStyle: "fill" }); + equalHeights( ac, 346, 358 ); }); -test("{ navigation: true, navigationFilter: header }", function() { - $("#navigation").accordion({ - navigation: true, - navigationFilter: function() { - return /\?p=1\.1\.3$/.test(this.href); - } +test( "{ icons: false }", function() { + var list = $( "#list1" ); + function icons( on ) { + same( list.find( "span.ui-icon").length, on ? 3 : 0 ); + same( list.hasClass( "ui-accordion-icons" ), on ); + } + list.accordion(); + icons( true ); + list.accordion( "destroy" ).accordion({ + icons: false }); - equals( $("#navigation .ui-accordion-content:eq(2)").size(), 1, "third content active" ); + icons( false ); + list.accordion( "option", "icons", $.ui.accordion.prototype.options.icons ); + icons( true ); + list.accordion( "option", "icons", false ); + icons( false ); }); -test("{ navigation: true, navigationFilter: content }", function() { - $("#navigation").accordion({ - navigation: true, - navigationFilter: function() { - return /\?p=1\.1\.3\.2$/.test(this.href); - } +test( "{ icons: hash }", function() { + var list = $( "#list1" ).accordion({ + icons: { activeHeader: "a1", header: "h1" } }); - equals( $("#navigation .ui-accordion-content:eq(2)").size(), 1, "third content active" ); + ok( list.find( ".ui-accordion-header.ui-state-active span.ui-icon" ).hasClass( "a1" ) ); + list.accordion( "option", "icons", { activeHeader: "a2", header: "h2" } ); + ok( !list.find( ".ui-accordion-header.ui-state-active span.ui-icon" ).hasClass( "a1" ) ); + ok( list.find( ".ui-accordion-header.ui-state-active span.ui-icon" ).hasClass( "a2" ) ); }); -})(jQuery); +}( jQuery ) ); diff --git a/tests/unit/accordion/accordion_tickets.js b/tests/unit/accordion/accordion_tickets.js index 98d8fbe4c..b301051da 100644 --- a/tests/unit/accordion/accordion_tickets.js +++ b/tests/unit/accordion/accordion_tickets.js @@ -1,8 +1,5 @@ -/* - * accordion_tickets.js - */ -(function($) { +(function( $ ) { -module("accordion: tickets"); +module( "accordion: tickets", accordionSetupTeardown() ); -})(jQuery); +}( jQuery ) ); diff --git a/tests/unit/testsuite.js b/tests/unit/testsuite.js index d152f84cd..23819d0e4 100644 --- a/tests/unit/testsuite.js +++ b/tests/unit/testsuite.js @@ -1,39 +1,67 @@ -function testWidgetDefaults(widget, defaults) { - var pluginDefaults = $.extend({}, - $.ui[widget].prototype.options - ); - +(function() { + +function testWidgetDefaults( widget, defaults ) { + var pluginDefaults = $.ui[ widget ].prototype.options; + // ensure that all defaults have the correct value - test('defined defaults', function() { - $.each(defaults, function(key, val) { - if ($.isFunction(val)) { - ok(val !== undefined, key); + test( "defined defaults", function() { + $.each( defaults, function( key, val ) { + if ( $.isFunction( val ) ) { + ok( $.isFunction( pluginDefaults[ key ] ), key ); return; } - same(pluginDefaults[key], val, key); + same( pluginDefaults[ key ], val, key ); }); }); - + // ensure that all defaults were tested - test('tested defaults', function() { - $.each(pluginDefaults, function(key, val) { - ok(key in defaults, key); + test( "tested defaults", function() { + $.each( pluginDefaults, function( key, val ) { + ok( key in defaults, key ); }); }); } -function testWidgetOverrides(widget) { - test('$.widget overrides', function() { - $.each(['_widgetInit', 'option', '_trigger'], function(i, method) { - ok($.Widget.prototype[method] == $.ui[widget].prototype[method], - 'should not override ' + method); +var privateMethods = [ + "_createWidget", + "_super", + "_superApply", + "destroy", + "option", + "enable", + "disable", + "_trigger" +]; + +function testWidgetOverrides( widget ) { + test( "$.widget overrides", function() { + $.each( privateMethods, function( i, method ) { + strictEqual( $.ui[ widget ].prototype[ method ], + $.Widget.prototype[ method ], "should not override " + method ); }); }); } -function commonWidgetTests(widget, settings) { - module(widget + ": common widget"); +function testBasicUsage( widget ) { + test( "basic usage", function() { + var defaultElement = $.ui[ widget ].prototype.defaultElement; + $( defaultElement ).appendTo( "body" )[ widget ]().remove(); + ok( true, "initialized on element" ); + + $( defaultElement ).accordion().remove(); + ok( true, "initialized on disconnected DOMElement - never connected" ); - testWidgetDefaults(widget, settings.defaults); - testWidgetOverrides(widget); + $( defaultElement ).appendTo( "body" ).remove().accordion().remove(); + ok( true, "initialized on disconnected DOMElement - removed" ); + }); } + +window.commonWidgetTests = function( widget, settings ) { + module( widget + ": common widget" ); + + testWidgetDefaults( widget, settings.defaults ); + testWidgetOverrides( widget ); + testBasicUsage( widget ); +}; + +}()); -- 2.39.5