| 1 | define( [ |
| 2 | "qunit", |
| 3 | "jquery", |
| 4 | "lib/helper", |
| 5 | "ui/widgets/checkboxradio" |
| 6 | ], function( QUnit, $, helper ) { |
| 7 | "use strict"; |
| 8 | |
| 9 | QUnit.module( "Checkboxradio: core", { afterEach: helper.moduleAfterEach } ); |
| 10 | |
| 11 | QUnit.test( "Checkbox - Initial class structure", function( assert ) { |
| 12 | assert.expect( 2 ); |
| 13 | var input = $( "#check" ), |
| 14 | label = $( "label[for=check]" ); |
| 15 | |
| 16 | input.checkboxradio(); |
| 17 | assert.hasClasses( input, "ui-helper-hidden-accessible ui-checkboxradio" ); |
| 18 | assert.hasClasses( label, "ui-button ui-widget ui-checkboxradio-label ui-corner-all" ); |
| 19 | } ); |
| 20 | |
| 21 | QUnit.test( "Radios - Initial class structure", function( assert ) { |
| 22 | assert.expect( 6 ); |
| 23 | var inputs = $( "#radio0 input" ), |
| 24 | labels = $( "#radio0 label" ); |
| 25 | |
| 26 | inputs.checkboxradio(); |
| 27 | inputs.each( function() { |
| 28 | assert.hasClasses( this, "ui-helper-hidden-accessible" ); |
| 29 | } ); |
| 30 | labels.each( function() { |
| 31 | assert.hasClasses( this, "ui-button" ); |
| 32 | } ); |
| 33 | } ); |
| 34 | |
| 35 | QUnit.test( "Ensure checked after single click on checkbox label button", function( assert ) { |
| 36 | assert.expect( 2 ); |
| 37 | |
| 38 | $( "#check2" ).checkboxradio().on( "change", function() { |
| 39 | var label = $( this ).checkboxradio( "widget" ); |
| 40 | assert.ok( this.checked, "checked ok" ); |
| 41 | |
| 42 | assert.hasClasses( label, "ui-state-active" ); |
| 43 | } ); |
| 44 | |
| 45 | $( "#check2" ).checkboxradio( "widget" ).simulate( "click" ); |
| 46 | } ); |
| 47 | |
| 48 | QUnit.test( "Handle form association via form attribute", function( assert ) { |
| 49 | assert.expect( 4 ); |
| 50 | |
| 51 | var radio1 = $( "#crazy-form-1" ).checkboxradio(); |
| 52 | var radio1Label = radio1.checkboxradio( "widget" ); |
| 53 | var radio2 = $( "#crazy-form-2" ).checkboxradio(); |
| 54 | var radio2Label = radio2.checkboxradio( "widget" ); |
| 55 | |
| 56 | radio2.on( "change", function() { |
| 57 | assert.ok( this.checked, "#2 checked" ); |
| 58 | assert.ok( !radio1[ 0 ].checked, "#1 not checked" ); |
| 59 | |
| 60 | assert.hasClasses( radio2Label, "ui-state-active" ); |
| 61 | assert.lacksClasses( radio1Label, "ui-state-active" ); |
| 62 | } ); |
| 63 | |
| 64 | radio2Label.simulate( "click" ); |
| 65 | } ); |
| 66 | |
| 67 | QUnit.test( "Checkbox creation requires a label, and finds it in all cases", function( assert ) { |
| 68 | assert.expect( 7 ); |
| 69 | var groups = [ |
| 70 | "<span><label for='t7092a'></label><input type='checkbox' id='t7092a'></span>", |
| 71 | "<span><input type='checkbox' id='t7092b'><label for='t7092b'></label></span>", |
| 72 | "<span><span><input type='checkbox' id='t7092c'></span><label for='t7092c'></label></span>", |
| 73 | "<span><input type='checkbox' id='t7092d'></span><span><label for='t7092d'></label></span>", |
| 74 | "<span><input type='checkbox' id='t7092e'><span><label for='t7092e'></label></span></span>", |
| 75 | "<span><label><input type='checkbox' id='t7092f'></label></span>", |
| 76 | "<span><input type='checkbox' id='check:7534'><label for='check:7534'>Label</label></span>" |
| 77 | ]; |
| 78 | |
| 79 | $.each( groups, function( index, markup ) { |
| 80 | var group = $( markup ); |
| 81 | |
| 82 | group.find( "input[type=checkbox]" ).checkboxradio(); |
| 83 | assert.hasClasses( group.find( "label" ), "ui-button" ); |
| 84 | } ); |
| 85 | } ); |
| 86 | |
| 87 | QUnit.test( "Calling checkboxradio on an unsupported element throws an error", function( assert ) { |
| 88 | assert.expect( 2 ); |
| 89 | |
| 90 | var error = new Error( |
| 91 | "Can't create checkboxradio on element.nodeName=div and element.type=undefined" |
| 92 | ); |
| 93 | assert.raises( |
| 94 | function() { |
| 95 | $( "<div>" ).checkboxradio(); |
| 96 | }, |
| 97 | error, |
| 98 | "Proper error thrown" |
| 99 | ); |
| 100 | |
| 101 | error = new Error( |
| 102 | "Can't create checkboxradio on element.nodeName=input and element.type=button" |
| 103 | ); |
| 104 | assert.raises( |
| 105 | function() { |
| 106 | $( "<input type='button'>" ).checkboxradio(); |
| 107 | }, |
| 108 | error, |
| 109 | "Proper error thrown" |
| 110 | ); |
| 111 | } ); |
| 112 | |
| 113 | QUnit.test( "Calling checkboxradio on an input with no label throws an error", function( assert ) { |
| 114 | assert.expect( 1 ); |
| 115 | |
| 116 | var error = new Error( "No label found for checkboxradio widget" ); |
| 117 | assert.raises( |
| 118 | function() { |
| 119 | $( "<input type='checkbox'>" ).checkboxradio(); |
| 120 | }, |
| 121 | error, |
| 122 | "Proper error thrown" |
| 123 | ); |
| 124 | } ); |
| 125 | |
| 126 | QUnit.test( "Inheriting label from initial HTML", function( assert ) { |
| 127 | var tests = [ |
| 128 | { |
| 129 | id: "label-with-no-for-with-html", |
| 130 | expectedLabel: "<strong>Hi</strong>, <em>I'm a label</em>" |
| 131 | }, |
| 132 | { |
| 133 | id: "label-with-no-for-with-text", |
| 134 | expectedLabel: "Hi, I'm a label" |
| 135 | }, |
| 136 | { |
| 137 | id: "label-with-no-for-with-html-like-text", |
| 138 | expectedLabel: "<em>Hi, I'm a label</em>" |
| 139 | } |
| 140 | ]; |
| 141 | |
| 142 | assert.expect( tests.length ); |
| 143 | |
| 144 | tests.forEach( function( testData ) { |
| 145 | var id = testData.id; |
| 146 | var expectedLabel = testData.expectedLabel; |
| 147 | var inputElem = $( "#" + id ); |
| 148 | var labelElem = inputElem.parent(); |
| 149 | |
| 150 | inputElem.checkboxradio( { icon: false } ); |
| 151 | |
| 152 | var labelWithoutInput = labelElem.clone(); |
| 153 | labelWithoutInput.find( "input" ).remove(); |
| 154 | |
| 155 | assert.strictEqual( |
| 156 | labelWithoutInput.html().trim(), |
| 157 | expectedLabel.trim(), |
| 158 | "Label correct [" + id + "]" |
| 159 | ); |
| 160 | } ); |
| 161 | } ); |
| 162 | |
| 163 | } ); |