2 * mobile checkboxradio unit tests
5 module( 'jquery.mobile.forms.checkboxradio.js' );
7 test( "widget can be disabled and enabled", function(){
8 var input = $( "#checkbox-1" ),
9 button = input.parent().find( ".ui-btn" );
11 input.checkboxradio( "disable" );
12 input.checkboxradio( "enable" );
13 ok( !input.attr( "disabled" ), "start input as enabled" );
14 ok( !input.parent().hasClass( "ui-disabled" ), "no disabled styles" );
15 ok( !input.attr( "checked" ), "not checked before click" );
16 button.trigger( "click" );
17 ok( input.attr( "checked" ), "checked after click" );
18 ok( button.hasClass( "ui-checkbox-on" ), "active styles after click" );
19 button.trigger( "click" );
21 input.checkboxradio( "disable" );
22 ok( input.attr( "disabled" ), "input disabled" );
23 ok( input.parent().hasClass( "ui-disabled" ), "disabled styles" );
24 ok( !input.attr( "checked" ), "not checked before click" );
25 button.trigger( "click" );
26 ok( !input.attr( "checked" ), "not checked after click" );
27 ok( !button.hasClass( "ui-checkbox-on" ), "no active styles after click" );
30 test( "clicking a checkbox within a controlgroup does not affect checkboxes with the same name in the same controlgroup", function(){
31 var input1 = $("#checkbox-31");
32 var button1 = input1.parent().find(".ui-btn");
33 var input2 = $("#checkbox-32");
34 var button2 = input2.parent().find(".ui-btn");
36 ok(!input1.attr("checked"), "input1 not checked before click");
37 ok(!input2.attr("checked"), "input2 not checked before click");
39 button1.trigger("click");
40 ok(input1.attr("checked"), "input1 checked after click on input1");
41 ok(!input2.attr("checked"), "input2 not checked after click on input1");
43 button2.trigger("click");
44 ok(input1.attr("checked"), "input1 not changed after click on input2");
45 ok(input2.attr("checked"), "input2 checked after click on input2");
48 asyncTest( "change events fired on checkbox for both check and uncheck", function(){
49 var $checkbox = $( "#checkbox-2" ),
50 $checkboxLabel = $checkbox.parent().find( ".ui-btn" );
52 $checkbox.unbind( "change" );
56 $checkbox.one('change', function(){
57 ok( true, "change fired on click to check the box" );
60 $checkboxLabel.trigger( "click" );
62 //test above will be triggered twice, and the start here once
63 $checkbox.one('change', function(){
67 $checkboxLabel.trigger( "click" );
70 asyncTest( "radio button labels should update the active button class to last clicked and clear checked", function(){
71 var $radioBtns = $( '#radio-active-btn-test input' ),
72 singleActiveAndChecked = function(){
73 same( $( "#radio-active-btn-test .ui-radio-on" ).length, 1, "there should be only one active button" );
74 // Use the .checked property, not the checked attribute which is not dynamic
76 $( "#radio-active-btn-test input" ).each(function(i, e) {
81 same( numChecked, 1, "there should be only one checked" );
84 $.testHelper.sequence([
86 $radioBtns.last().siblings( 'label' ).click();
90 ok( $radioBtns.last().prop( 'checked' ), "last input is checked" );
91 ok( $radioBtns.last().siblings( 'label' ).hasClass( 'ui-radio-on' ),
92 "last input label is an active button" );
94 ok( !$radioBtns.first().prop( 'checked' ), "first input label is not active" );
95 ok( !$radioBtns.first().siblings( 'label' ).hasClass( 'ui-radio-on' ),
96 "first input label is not active" );
98 singleActiveAndChecked();
100 $radioBtns.first().siblings( 'label' ).click();
104 ok( $radioBtns.first().prop( 'checked' ));
105 ok( $radioBtns.first().siblings( 'label' ).hasClass( 'ui-radio-on' ),
106 "first input label is an active button" );
108 ok( !$radioBtns.last().prop( 'checked' ));
109 ok( !$radioBtns.last().siblings( 'label' ).hasClass( 'ui-radio-on' ),
110 "last input label is not active" );
112 singleActiveAndChecked();
120 test( "checkboxradio controls will create when inside a container that receives a 'create' event", function(){
121 ok( !$("#enhancetest").appendTo(".ui-page-active").find(".ui-checkbox").length, "did not have enhancements applied" );
122 ok( $("#enhancetest").trigger("create").find(".ui-checkbox").length, "enhancements applied" );
125 $.mobile.page.prototype.options.keepNative = "input.should-be-native";
127 // not testing the positive case here since's it's obviously tested elsewhere
128 test( "checkboxradio elements in the keepNative set shouldn't be enhanced", function() {
129 ok( !$("input.should-be-native").parent().is("div.ui-checkbox") );
132 test( "Elements with \u201cdata-mini='true'\u201d should have \u201cui-mini\u201d class attached to enhanced element.", function(){
133 var full = document.getElementById("radio-full"),
134 $fulllbl = $('[for="radio-full"]'),
135 mini = document.getElementById("radio-mini"),
136 $minilbl = $('[for="radio-mini"]'),
137 minictrl = $("#mini-control");
139 ok( !full.getAttribute('data-nstest-mini') && !$fulllbl.hasClass('ui-mini'), "Original element does not have data attribute, enhanced version does not recieve .ui-mini.");
140 ok( mini.getAttribute('data-nstest-mini'), "Original element has data attribute, enhanced version recieves .ui-mini." );
143 asyncTest( "clicking the label triggers a click on the element", function() {
148 $( "#checkbox-click-triggered" ).one('click', function() {
152 $.testHelper.sequence([
154 $( "[for='checkbox-click-triggered']" ).click();
158 ok(clicked, "click was fired on input");
164 asyncTest( "clicking the label triggers a change on the element", function() {
169 $( "#checkbox-change-triggered" ).one('change', function() {
173 $.testHelper.sequence([
175 $( "[for='checkbox-change-triggered']" ).click();
179 ok(changed, "change was fired on input");
186 test( "theme should be inherited", function() {
187 var $inherited = $( "#checkbox-inherit-theme" ),
188 $explicit = $( "#checkbox-explicit-theme" );
190 ok( $inherited.siblings("label").hasClass( "ui-btn-up-a" ), "should inherit from page" );
191 ok( $explicit.siblings("label").hasClass( "ui-btn-up-b" ), "should not inherit" );
194 asyncTest( "form submission should include radio button values", function() {
195 var $form = $( "#radio-form" ), $input = $form.find("input").first();
197 $.testHelper.pageSequence([
204 ok( location.search.indexOf("radio1=1") >= 0, "the radio was checked" );
206 // if the changepage in the previous function failed don't go back
208 window.history.back();
218 asyncTest( "form submission should include checkbox button values", function() {
219 var $form = $( "#check-form" ), $inputs = $form.find("input");
221 $.testHelper.pageSequence([
228 ok( location.search.indexOf("checkbox-form=on") >= 0, "the first checkbox was checked" );
229 ok( location.search.indexOf("checkbox-form-2=on") >= 0, "the second checkbox was checked" );
230 // if the changepage in the previous function failed don't go back
232 window.history.back();
242 test( "nested label checkbox still renders", function() {
243 var $checkbox = $( "#checkbox-nested-label" );
246 $checkbox.checkboxradio();
248 ok( false, "checkboxradio exception raised: " + e.toString());
251 ok( $checkbox.parent().hasClass("ui-checkbox"), "enhancement has occured");
254 test( "nested label (no [for]) checkbox still renders", function() {
255 var $checkbox = $( "#checkbox-nested-label-no-for" );
258 $checkbox.checkboxradio();
260 ok( false, "checkboxradio exception raised: " + e.toString());
263 ok( $checkbox.parent().hasClass("ui-checkbox"), "enhancement has occured");
266 test( "Icon positioning", function() {
267 var bottomicon = $("[for='bottomicon']")
268 topicon = $("[for='topicon']");
270 ok( bottomicon.hasClass("ui-btn-icon-bottom"), "Icon position set on label adds the appropriate class." );
271 ok( topicon.hasClass("ui-btn-icon-top"), "Icon position set on input adds the appropriate class to the label." );