| 1 |
describe('__collectFormData unit tests', function() { |
| 2 |
|
| 3 |
before(function() { |
| 4 |
|
| 5 |
if (!customElements.get('test-input')) { |
| 6 |
class TestInput extends HTMLElement { |
| 7 |
static formAssociated = true; |
| 8 |
|
| 9 |
constructor() { |
| 10 |
super(); |
| 11 |
this._internals = this.attachInternals(); |
| 12 |
this._value = ''; |
| 13 |
} |
| 14 |
|
| 15 |
connectedCallback() { |
| 16 |
this._value = this.getAttribute('value') || ''; |
| 17 |
this._internals.setFormValue(this._value); |
| 18 |
} |
| 19 |
|
| 20 |
get value() { |
| 21 |
return this._value; |
| 22 |
} |
| 23 |
|
| 24 |
set value(val) { |
| 25 |
this._value = val; |
| 26 |
this._internals.setFormValue(val); |
| 27 |
} |
| 28 |
} |
| 29 |
customElements.define('test-input', TestInput); |
| 30 |
} |
| 31 |
}); |
| 32 |
|
| 33 |
it('collects text input from form', function () { |
| 34 |
let form = createProcessedHTML('<form><input type="text" name="foo" value="bar"></form>') |
| 35 |
let formData = htmx.__collectFormData(form, form, null) |
| 36 |
assert.equal(formData.get('foo'), 'bar') |
| 37 |
}) |
| 38 |
|
| 39 |
it('collects multiple inputs from form', function () { |
| 40 |
let form = createProcessedHTML('<form><input name="a" value="1"><input name="b" value="2"></form>') |
| 41 |
let formData = htmx.__collectFormData(form, form, null) |
| 42 |
assert.equal(formData.get('a'), '1') |
| 43 |
assert.equal(formData.get('b'), '2') |
| 44 |
}) |
| 45 |
|
| 46 |
it('collects textarea from form', function () { |
| 47 |
let form = createProcessedHTML('<form><textarea name="msg">hello</textarea></form>') |
| 48 |
let formData = htmx.__collectFormData(form, form, null) |
| 49 |
assert.equal(formData.get('msg'), 'hello') |
| 50 |
}) |
| 51 |
|
| 52 |
it('collects select from form', function () { |
| 53 |
let form = createProcessedHTML('<form><select name="choice"><option value="a">A</option><option value="b" selected>B</option></select></form>') |
| 54 |
let formData = htmx.__collectFormData(form, form, null) |
| 55 |
assert.equal(formData.get('choice'), 'b') |
| 56 |
}) |
| 57 |
|
| 58 |
it('collects checked checkbox', function () { |
| 59 |
let form = createProcessedHTML('<form><input type="checkbox" name="agree" value="yes" checked></form>') |
| 60 |
let formData = htmx.__collectFormData(form, form, null) |
| 61 |
assert.equal(formData.get('agree'), 'yes') |
| 62 |
}) |
| 63 |
|
| 64 |
it('excludes unchecked checkbox', function () { |
| 65 |
let form = createProcessedHTML('<form><input type="checkbox" name="agree" value="yes"></form>') |
| 66 |
let formData = htmx.__collectFormData(form, form, null) |
| 67 |
assert.equal(formData.get('agree'), null) |
| 68 |
}) |
| 69 |
|
| 70 |
it('collects checked radio button', function () { |
| 71 |
let form = createProcessedHTML('<form><input type="radio" name="color" value="red" checked><input type="radio" name="color" value="blue"></form>') |
| 72 |
let formData = htmx.__collectFormData(form, form, null) |
| 73 |
assert.equal(formData.get('color'), 'red') |
| 74 |
}) |
| 75 |
|
| 76 |
it('excludes unchecked radio buttons', function () { |
| 77 |
let form = createProcessedHTML('<form><input type="radio" name="color" value="red"><input type="radio" name="color" value="blue"></form>') |
| 78 |
let formData = htmx.__collectFormData(form, form, null) |
| 79 |
assert.equal(formData.get('color'), null) |
| 80 |
}) |
| 81 |
|
| 82 |
it('collects multiple select values', function () { |
| 83 |
let form = createProcessedHTML('<form><select name="items" multiple><option value="a" selected>A</option><option value="b" selected>B</option><option value="c">C</option></select></form>') |
| 84 |
let formData = htmx.__collectFormData(form, form, null) |
| 85 |
assert.deepEqual(formData.getAll('items'), ['a', 'b']) |
| 86 |
}) |
| 87 |
|
| 88 |
it('excludes disabled inputs', function () { |
| 89 |
let form = createProcessedHTML('<form><input name="a" value="1"><input name="b" value="2" disabled></form>') |
| 90 |
let formData = htmx.__collectFormData(form, form, null) |
| 91 |
assert.equal(formData.get('a'), '1') |
| 92 |
assert.equal(formData.get('b'), null) |
| 93 |
}) |
| 94 |
|
| 95 |
it('excludes inputs without name attribute', function () { |
| 96 |
let form = createProcessedHTML('<form><input value="1"><input name="b" value="2"></form>') |
| 97 |
let formData = htmx.__collectFormData(form, form, null) |
| 98 |
assert.equal(formData.get('b'), '2') |
| 99 |
}) |
| 100 |
|
| 101 |
it('collects element value when no form provided', function () { |
| 102 |
let input = createProcessedHTML('<input name="foo" value="bar">') |
| 103 |
let formData = htmx.__collectFormData(input, null, null) |
| 104 |
assert.equal(formData.get('foo'), 'bar') |
| 105 |
}) |
| 106 |
|
| 107 |
it('excludes unchecked checkbox trigger with no form', function () { |
| 108 |
let input = createProcessedHTML('<input type="checkbox" name="expires">') |
| 109 |
let formData = htmx.__collectFormData(input, null, null) |
| 110 |
assert.equal(formData.get('expires'), null) |
| 111 |
}) |
| 112 |
|
| 113 |
it('includes checked checkbox trigger with no form', function () { |
| 114 |
let input = createProcessedHTML('<input type="checkbox" name="expires" checked>') |
| 115 |
let formData = htmx.__collectFormData(input, null, null) |
| 116 |
assert.equal(formData.get('expires'), 'on') |
| 117 |
}) |
| 118 |
|
| 119 |
it('excludes unchecked radio trigger with no form', function () { |
| 120 |
let input = createProcessedHTML('<input type="radio" name="size" value="large">') |
| 121 |
let formData = htmx.__collectFormData(input, null, null) |
| 122 |
assert.equal(formData.get('size'), null) |
| 123 |
}) |
| 124 |
|
| 125 |
it('includes checked radio trigger with no form', function () { |
| 126 |
let input = createProcessedHTML('<input type="radio" name="size" value="large" checked>') |
| 127 |
let formData = htmx.__collectFormData(input, null, null) |
| 128 |
assert.equal(formData.get('size'), 'large') |
| 129 |
}) |
| 130 |
|
| 131 |
it('collects all selected options from select-multiple trigger with no form', function () { |
| 132 |
let container = createProcessedHTML('<div><select name="color" multiple><option value="red">Red</option><option value="blue">Blue</option><option value="green">Green</option></select></div>') |
| 133 |
let select = container.querySelector('select') |
| 134 |
select.options[1].selected = true |
| 135 |
select.options[2].selected = true |
| 136 |
let formData = htmx.__collectFormData(select, null, null) |
| 137 |
assert.deepEqual(formData.getAll('color'), ['blue', 'green']) |
| 138 |
}) |
| 139 |
|
| 140 |
it('collects button trigger value with no form', function () { |
| 141 |
let container = createProcessedHTML('<div><button name="intent" value="preview"></button></div>') |
| 142 |
let btn = container.querySelector('button') |
| 143 |
let formData = htmx.__collectFormData(btn, null, null) |
| 144 |
assert.equal(formData.get('intent'), 'preview') |
| 145 |
}) |
| 146 |
|
| 147 |
it('excludes disabled trigger element with no form', function () { |
| 148 |
let container = createProcessedHTML('<div><input name="skip" value="nope" disabled></div>') |
| 149 |
let input = container.querySelector('input') |
| 150 |
let formData = htmx.__collectFormData(input, null, null) |
| 151 |
assert.equal(formData.get('skip'), null) |
| 152 |
}) |
| 153 |
|
| 154 |
it('excludes fieldset-disabled input via hx-include', function () { |
| 155 |
let container = createProcessedHTML('<div><fieldset disabled><input id="blocked" name="blocked" value="nope"></fieldset><input id="active" name="active" value="yes"></div>') |
| 156 |
let blocked = container.querySelector('#blocked') |
| 157 |
let active = container.querySelector('#active') |
| 158 |
let included = new Set() |
| 159 |
let formData = new FormData() |
| 160 |
htmx.__addInputValues(blocked, included, formData) |
| 161 |
htmx.__addInputValues(active, included, formData) |
| 162 |
assert.equal(formData.get('blocked'), null) |
| 163 |
assert.equal(formData.get('active'), 'yes') |
| 164 |
}) |
| 165 |
|
| 166 |
it('collects submitter value', function () { |
| 167 |
let form = createProcessedHTML('<form><input name="a" value="1"><button name="submit" value="go">Go</button></form>') |
| 168 |
let submitter = form.querySelector('button') |
| 169 |
let formData = htmx.__collectFormData(form, form, submitter) |
| 170 |
assert.equal(formData.get('a'), '1') |
| 171 |
assert.equal(formData.get('submit'), 'go') |
| 172 |
}) |
| 173 |
|
| 174 |
it('collects hx-include elements', function () { |
| 175 |
createProcessedHTML('<input id="extra" name="extra" value="included"><form hx-include="#extra"><input name="a" value="1"></form>') |
| 176 |
let form = document.querySelector('form') |
| 177 |
let formData = htmx.__collectFormData(form, form, null) |
| 178 |
assert.equal(formData.get('a'), '1') |
| 179 |
assert.equal(formData.get('extra'), 'included') |
| 180 |
}) |
| 181 |
|
| 182 |
it('does not duplicate elements in hx-include', function () { |
| 183 |
let container = createProcessedHTML('<div><form><input name="a" value="1"></form></div>') |
| 184 |
let form = container.querySelector('form') |
| 185 |
form.setAttribute('hx-include', 'input[name="a"]') |
| 186 |
let formData = htmx.__collectFormData(form, form, null) |
| 187 |
assert.deepEqual(formData.getAll('a'), ['1']) |
| 188 |
}) |
| 189 |
|
| 190 |
it('handles empty form', function () { |
| 191 |
let form = createProcessedHTML('<form></form>') |
| 192 |
let formData = htmx.__collectFormData(form, form, null) |
| 193 |
assert.equal([...formData.keys()].length, 0) |
| 194 |
}) |
| 195 |
|
| 196 |
it('handles element without name or form', function () { |
| 197 |
let div = createProcessedHTML('<div></div>') |
| 198 |
let formData = htmx.__collectFormData(div, null, null) |
| 199 |
assert.equal([...formData.keys()].length, 0) |
| 200 |
}) |
| 201 |
|
| 202 |
it('collects form-associated custom element value', function () { |
| 203 |
let form = createProcessedHTML('<form><test-input name="custom" value="test-value"></test-input></form>'); |
| 204 |
let formData = htmx.__collectFormData(form, form, null); |
| 205 |
assert.equal(formData.get('custom'), 'test-value'); |
| 206 |
}); |
| 207 |
|
| 208 |
it('collects both regular and custom element values', function () { |
| 209 |
let form = createProcessedHTML('<form><input name="regular" value="reg-val"><test-input name="custom" value="cust-val"></test-input></form>'); |
| 210 |
let formData = htmx.__collectFormData(form, form, null); |
| 211 |
assert.equal(formData.get('regular'), 'reg-val'); |
| 212 |
assert.equal(formData.get('custom'), 'cust-val'); |
| 213 |
}); |
| 214 |
|
| 215 |
it('collects custom element with form attribute outside form', function () { |
| 216 |
createProcessedHTML('<form id="myform"><input name="inside" value="in"></form><test-input form="myform" name="outside" value="out"></test-input>'); |
| 217 |
let form = document.getElementById('myform'); |
| 218 |
let formData = htmx.__collectFormData(form, form, null); |
| 219 |
assert.equal(formData.get('inside'), 'in'); |
| 220 |
assert.equal(formData.get('outside'), 'out'); |
| 221 |
}); |
| 222 |
|
| 223 |
it('does not duplicate custom element in hx-include', function () { |
| 224 |
let container = createProcessedHTML('<div><form><test-input name="custom" value="val"></test-input></form></div>'); |
| 225 |
let form = container.querySelector('form'); |
| 226 |
form.setAttribute('hx-include', 'test-input[name="custom"]'); |
| 227 |
let formData = htmx.__collectFormData(form, form, null); |
| 228 |
assert.deepEqual(formData.getAll('custom'), ['val']); |
| 229 |
}); |
| 230 |
|
| 231 |
it('collects multiple custom elements with same name', function () { |
| 232 |
let form = createProcessedHTML('<form><test-input name="items" value="a"></test-input><test-input name="items" value="b"></test-input></form>'); |
| 233 |
let formData = htmx.__collectFormData(form, form, null); |
| 234 |
assert.deepEqual(formData.getAll('items'), ['a', 'b']); |
| 235 |
}); |
| 236 |
}); |