| 1 | define( [ |
| 2 | "qunit", |
| 3 | "jquery", |
| 4 | "./helper", |
| 5 | "ui/widgets/accordion" |
| 6 | ], function( QUnit, $, testHelper ) { |
| 7 | "use strict"; |
| 8 | |
| 9 | var beforeAfterEach = testHelper.beforeAfterEach, |
| 10 | state = testHelper.state; |
| 11 | |
| 12 | QUnit.module( "accordion: core", beforeAfterEach() ); |
| 13 | |
| 14 | $.each( { div: "#list1", ul: "#navigation", dl: "#accordion-dl" }, function( type, selector ) { |
| 15 | QUnit.test( "markup structure: " + type, function( assert ) { |
| 16 | assert.expect( 10 ); |
| 17 | var element = $( selector ).accordion(), |
| 18 | headers = element.find( ".ui-accordion-header" ), |
| 19 | content = headers.next(); |
| 20 | |
| 21 | assert.hasClasses( element, "ui-accordion ui-widget" ); |
| 22 | assert.equal( headers.length, 3, ".ui-accordion-header elements exist, correct number" ); |
| 23 | assert.hasClasses( headers[ 0 ], |
| 24 | "ui-accordion-header ui-accordion-header-active ui-accordion-icons" ); |
| 25 | assert.hasClasses( headers[ 1 ], |
| 26 | "ui-accordion-header ui-accordion-header-collapsed ui-accordion-icons" ); |
| 27 | assert.hasClasses( headers[ 2 ], |
| 28 | "ui-accordion-header ui-accordion-header-collapsed ui-accordion-icons" ); |
| 29 | assert.equal( content.length, 3, ".ui-accordion-content elements exist, correct number" ); |
| 30 | assert.hasClasses( content[ 0 ], |
| 31 | "ui-accordion-content ui-widget-content ui-accordion-content-active" ); |
| 32 | assert.hasClasses( content[ 1 ], "ui-accordion-content ui-widget-content" ); |
| 33 | assert.hasClasses( content[ 2 ], "ui-accordion-content ui-widget-content" ); |
| 34 | assert.deepEqual( element.find( ".ui-accordion-header" ).next().get(), |
| 35 | element.find( ".ui-accordion-content" ).get(), |
| 36 | "content panels come immediately after headers" ); |
| 37 | } ); |
| 38 | } ); |
| 39 | |
| 40 | QUnit.test( "handle click on header-descendant", function( assert ) { |
| 41 | assert.expect( 1 ); |
| 42 | var element = $( "#navigation" ).accordion(); |
| 43 | $( "#navigation h2" ).eq( 1 ).find( "a" ).trigger( "click" ); |
| 44 | state( assert, element, 0, 1, 0 ); |
| 45 | } ); |
| 46 | |
| 47 | QUnit.test( "accessibility", function( assert ) { |
| 48 | assert.expect( 61 ); |
| 49 | var element = $( "#list1" ).accordion( { |
| 50 | active: 1, |
| 51 | collapsible: true |
| 52 | } ), |
| 53 | headers = element.find( ".ui-accordion-header" ); |
| 54 | |
| 55 | assert.equal( element.attr( "role" ), "tablist", "element role" ); |
| 56 | headers.each( function( i ) { |
| 57 | var header = headers.eq( i ), |
| 58 | panel = header.next(); |
| 59 | assert.equal( header.attr( "role" ), "tab", "header " + i + " role" ); |
| 60 | assert.equal( header.attr( "aria-controls" ), panel.attr( "id" ), "header " + i + " aria-controls" ); |
| 61 | assert.equal( panel.attr( "role" ), "tabpanel", "panel " + i + " role" ); |
| 62 | assert.equal( panel.attr( "aria-labelledby" ), header.attr( "id" ), "panel " + i + " aria-labelledby" ); |
| 63 | } ); |
| 64 | |
| 65 | assert.equal( headers.eq( 1 ).attr( "tabindex" ), 0, "active header has tabindex=0" ); |
| 66 | assert.equal( headers.eq( 1 ).attr( "aria-selected" ), "true", "active tab (1) has aria-selected=true" ); |
| 67 | assert.equal( headers.eq( 1 ).attr( "aria-expanded" ), "true", "active tab (1) has aria-expanded=true" ); |
| 68 | assert.equal( headers.eq( 1 ).next().attr( "aria-hidden" ), "false", "active tabpanel (1) has aria-hidden=false" ); |
| 69 | assert.equal( headers.eq( 0 ).attr( "tabindex" ), -1, "inactive header (0) has tabindex=-1" ); |
| 70 | assert.equal( headers.eq( 0 ).attr( "aria-selected" ), "false", "inactive tab (0) has aria-selected=false" ); |
| 71 | assert.equal( headers.eq( 0 ).attr( "aria-expanded" ), "false", "inactive tab (0) has aria-expanded=false" ); |
| 72 | assert.equal( headers.eq( 0 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (0) has aria-hidden=true" ); |
| 73 | assert.equal( headers.eq( 2 ).attr( "tabindex" ), -1, "inactive header (2) has tabindex=-1" ); |
| 74 | assert.equal( headers.eq( 2 ).attr( "aria-selected" ), "false", "inactive tab (2) has aria-selected=false" ); |
| 75 | assert.equal( headers.eq( 2 ).attr( "aria-expanded" ), "false", "inactive tab (2) has aria-expanded=false" ); |
| 76 | assert.equal( headers.eq( 2 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (2) has aria-hidden=true" ); |
| 77 | |
| 78 | element.accordion( "option", "active", 0 ); |
| 79 | assert.equal( headers.eq( 0 ).attr( "tabindex" ), 0, "active header (0) has tabindex=0" ); |
| 80 | assert.equal( headers.eq( 0 ).attr( "aria-selected" ), "true", "active tab (0) has aria-selected=true" ); |
| 81 | assert.equal( headers.eq( 0 ).attr( "aria-expanded" ), "true", "active tab (0) has aria-expanded=true" ); |
| 82 | assert.equal( headers.eq( 0 ).next().attr( "aria-hidden" ), "false", "active tabpanel (0) has aria-hidden=false" ); |
| 83 | assert.equal( headers.eq( 1 ).attr( "tabindex" ), -1, "inactive header (1) has tabindex=-1" ); |
| 84 | assert.equal( headers.eq( 1 ).attr( "aria-selected" ), "false", "inactive tab (1) has aria-selected=false" ); |
| 85 | assert.equal( headers.eq( 1 ).attr( "aria-expanded" ), "false", "inactive tab (1) has aria-expanded=false" ); |
| 86 | assert.equal( headers.eq( 1 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (1) has aria-hidden=true" ); |
| 87 | assert.equal( headers.eq( 2 ).attr( "tabindex" ), -1, "inactive header (2) has tabindex=-1" ); |
| 88 | assert.equal( headers.eq( 2 ).attr( "aria-selected" ), "false", "inactive tab (2) has aria-selected=false" ); |
| 89 | assert.equal( headers.eq( 2 ).attr( "aria-expanded" ), "false", "inactive tab (2) has aria-expanded=false" ); |
| 90 | assert.equal( headers.eq( 2 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (2) has aria-hidden=true" ); |
| 91 | |
| 92 | element.accordion( "option", "active", false ); |
| 93 | assert.equal( headers.eq( 0 ).attr( "tabindex" ), 0, "previously active header (0) has tabindex=0" ); |
| 94 | assert.equal( headers.eq( 0 ).attr( "aria-selected" ), "false", "inactive tab (0) has aria-selected=false" ); |
| 95 | assert.equal( headers.eq( 0 ).attr( "aria-expanded" ), "false", "inactive tab (0) has aria-expanded=false" ); |
| 96 | assert.equal( headers.eq( 0 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (0) has aria-hidden=true" ); |
| 97 | assert.equal( headers.eq( 1 ).attr( "tabindex" ), -1, "inactive header (1) has tabindex=-1" ); |
| 98 | assert.equal( headers.eq( 1 ).attr( "aria-selected" ), "false", "inactive tab (1) has aria-selected=false" ); |
| 99 | assert.equal( headers.eq( 1 ).attr( "aria-expanded" ), "false", "inactive tab (1) has aria-expanded=false" ); |
| 100 | assert.equal( headers.eq( 1 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (1) has aria-hidden=true" ); |
| 101 | assert.equal( headers.eq( 2 ).attr( "tabindex" ), -1, "inactive header (2) has tabindex=-1" ); |
| 102 | assert.equal( headers.eq( 2 ).attr( "aria-selected" ), "false", "inactive tab (2) has aria-selected=false" ); |
| 103 | assert.equal( headers.eq( 2 ).attr( "aria-expanded" ), "false", "inactive tab (2) has aria-expanded=false" ); |
| 104 | assert.equal( headers.eq( 2 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (2) has aria-hidden=true" ); |
| 105 | |
| 106 | element.accordion( "option", "active", 1 ); |
| 107 | assert.equal( headers.eq( 1 ).attr( "tabindex" ), 0, "active header has tabindex=0" ); |
| 108 | assert.equal( headers.eq( 1 ).attr( "aria-selected" ), "true", "active tab (1) has aria-selected=true" ); |
| 109 | assert.equal( headers.eq( 1 ).attr( "aria-expanded" ), "true", "active tab (1) has aria-expanded=true" ); |
| 110 | assert.equal( headers.eq( 1 ).next().attr( "aria-hidden" ), "false", "active tabpanel (1) has aria-hidden=false" ); |
| 111 | assert.equal( headers.eq( 0 ).attr( "tabindex" ), -1, "inactive header (0) has tabindex=-1" ); |
| 112 | assert.equal( headers.eq( 0 ).attr( "aria-selected" ), "false", "inactive tab (0) has aria-selected=false" ); |
| 113 | assert.equal( headers.eq( 0 ).attr( "aria-expanded" ), "false", "inactive tab (0) has aria-expanded=false" ); |
| 114 | assert.equal( headers.eq( 0 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (0) has aria-hidden=true" ); |
| 115 | assert.equal( headers.eq( 2 ).attr( "tabindex" ), -1, "inactive header (2) has tabindex=-1" ); |
| 116 | assert.equal( headers.eq( 2 ).attr( "aria-selected" ), "false", "inactive tab (2) has aria-selected=false" ); |
| 117 | assert.equal( headers.eq( 2 ).attr( "aria-expanded" ), "false", "inactive tab (2) has aria-expanded=false" ); |
| 118 | assert.equal( headers.eq( 2 ).next().attr( "aria-hidden" ), "true", "inactive tabpanel (2) has aria-hidden=true" ); |
| 119 | |
| 120 | } ); |
| 121 | |
| 122 | QUnit.test( "keyboard support", function( assert ) { |
| 123 | var ready = assert.async(); |
| 124 | assert.expect( 13 ); |
| 125 | var element = $( "#list1" ).accordion(), |
| 126 | headers = element.find( ".ui-accordion-header" ), |
| 127 | anchor = headers.eq( 1 ).next().find( "a" ).eq( 0 ), |
| 128 | keyCode = $.ui.keyCode; |
| 129 | assert.equal( headers.filter( ".ui-state-focus" ).length, 0, "no headers focused on init" ); |
| 130 | headers.eq( 0 ).simulate( "focus" ); |
| 131 | setTimeout( step1 ); |
| 132 | |
| 133 | function step1() { |
| 134 | assert.hasClasses( headers.eq( 0 ), "ui-state-focus", "first header has focus" ); |
| 135 | headers.eq( 0 ).simulate( "keydown", { keyCode: keyCode.DOWN } ); |
| 136 | assert.hasClasses( headers.eq( 1 ), "ui-state-focus", "DOWN moves focus to next header" ); |
| 137 | headers.eq( 1 ).simulate( "keydown", { keyCode: keyCode.RIGHT } ); |
| 138 | assert.hasClasses( headers.eq( 2 ), "ui-state-focus", "RIGHT moves focus to next header" ); |
| 139 | headers.eq( 2 ).simulate( "keydown", { keyCode: keyCode.DOWN } ); |
| 140 | assert.hasClasses( headers.eq( 0 ), "ui-state-focus", "DOWN wraps focus to first header" ); |
| 141 | |
| 142 | headers.eq( 0 ).simulate( "keydown", { keyCode: keyCode.UP } ); |
| 143 | assert.hasClasses( headers.eq( 2 ), "ui-state-focus", "UP wraps focus to last header" ); |
| 144 | headers.eq( 2 ).simulate( "keydown", { keyCode: keyCode.LEFT } ); |
| 145 | assert.hasClasses( headers.eq( 1 ), |
| 146 | "ui-state-focus", "LEFT moves focus to previous header" ); |
| 147 | |
| 148 | headers.eq( 1 ).simulate( "keydown", { keyCode: keyCode.HOME } ); |
| 149 | assert.hasClasses( headers.eq( 0 ), "ui-state-focus", "HOME moves focus to first header" ); |
| 150 | headers.eq( 0 ).simulate( "keydown", { keyCode: keyCode.END } ); |
| 151 | assert.hasClasses( headers.eq( 2 ), "ui-state-focus", "END moves focus to last header" ); |
| 152 | |
| 153 | headers.eq( 2 ).simulate( "keydown", { keyCode: keyCode.ENTER } ); |
| 154 | assert.equal( element.accordion( "option", "active" ), 2, "ENTER activates panel" ); |
| 155 | headers.eq( 1 ).simulate( "keydown", { keyCode: keyCode.SPACE } ); |
| 156 | assert.equal( element.accordion( "option", "active" ), 1, "SPACE activates panel" ); |
| 157 | |
| 158 | anchor.simulate( "focus" ); |
| 159 | setTimeout( step2 ); |
| 160 | } |
| 161 | |
| 162 | function step2() { |
| 163 | assert.lacksClasses( headers.eq( 1 ), "ui-state-focus", |
| 164 | "header loses focus when focusing inside the panel" ); |
| 165 | anchor.simulate( "keydown", { keyCode: keyCode.UP, ctrlKey: true } ); |
| 166 | assert.hasClasses( headers.eq( 1 ), "ui-state-focus", "CTRL+UP moves focus to header" ); |
| 167 | ready(); |
| 168 | } |
| 169 | } ); |
| 170 | |
| 171 | } ); |