OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <!-- |
| 3 @license |
| 4 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 5 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 6 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 7 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 8 Code distributed by Google as part of the polymer project is also |
| 9 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 10 --> |
| 11 <html> |
| 12 <head> |
| 13 <title>iron-form</title> |
| 14 |
| 15 <script src="../../webcomponentsjs/webcomponents-lite.js"></script> |
| 16 <script src="../../web-component-tester/browser.js"></script> |
| 17 |
| 18 <link rel="import" href="../../polymer/polymer.html"> |
| 19 <link rel="import" href="../../paper-input/paper-input.html"> |
| 20 <link rel="import" href="../../paper-input/paper-textarea.html"> |
| 21 <link rel="import" href="../iron-form.html"> |
| 22 <link rel="import" href="simple-element.html"> |
| 23 <link rel="import" href="element-with-nested-form-element.html"> |
| 24 <link rel="import" href="element-with-nested-input.html"> |
| 25 <link rel="import" href="validatable-element-with-nested-elements.html"> |
| 26 |
| 27 </head> |
| 28 <body> |
| 29 |
| 30 <test-fixture id="Basic"> |
| 31 <template> |
| 32 <form is="iron-form"> |
| 33 <simple-element name="zig" value="zag"></simple-element> |
| 34 <input name="foo" value="bar"> |
| 35 </form> |
| 36 </template> |
| 37 </test-fixture> |
| 38 |
| 39 <test-fixture id="Dupes"> |
| 40 <template> |
| 41 <form is="iron-form"> |
| 42 <input name="foo" value="bar"> |
| 43 <input name="foo" value="barbar"> |
| 44 <simple-element name="zig" value="zig"></simple-element> |
| 45 <simple-element name="zig" value="zag"></simple-element> |
| 46 <simple-element name="zig" value="zug"></simple-element> |
| 47 </form> |
| 48 </template> |
| 49 </test-fixture> |
| 50 |
| 51 <test-fixture id="NestedDupes"> |
| 52 <template> |
| 53 <form is="iron-form"> |
| 54 <element-with-nested-form-element name="foo" value="bar"></element-with-
nested-form-element> |
| 55 <element-with-nested-input name="zig" value="zag"></element-with-nested-
input> |
| 56 </form> |
| 57 </template> |
| 58 </test-fixture> |
| 59 |
| 60 <test-fixture id="NestedSubmittable"> |
| 61 <template> |
| 62 <form is="iron-form"> |
| 63 <element-with-nested-form-element> |
| 64 <simple-element name="foo" value="bar"></simple-element> |
| 65 </element-with-nested-form-element> |
| 66 </form> |
| 67 </template> |
| 68 </test-fixture> |
| 69 |
| 70 <test-fixture id="CheckedStates"> |
| 71 <template> |
| 72 <form is="iron-form"> |
| 73 <input type="checkbox" name="foo" value="bar1" checked> |
| 74 <input type="checkbox" name="foo" value="bar2"> |
| 75 <input type="checkbox" name="foo" value="bar3" checked> |
| 76 <input type="checkbox" name="foo" value="bar4"> |
| 77 </form> |
| 78 </template> |
| 79 </test-fixture> |
| 80 |
| 81 <test-fixture id="Disabled"> |
| 82 <template> |
| 83 <form is="iron-form"> |
| 84 <input name="foo" value="bar1"> |
| 85 <input name="foo" value="bar2" disabled> |
| 86 <input type="checkbox" name="zig" value="zag" disabled checked> |
| 87 </form> |
| 88 </template> |
| 89 </test-fixture> |
| 90 |
| 91 <test-fixture id="FormGet"> |
| 92 <template> |
| 93 <form is="iron-form" action="/responds_with_json" method="get"> |
| 94 <simple-element name="zig" value="zag"></simple-element> |
| 95 </form> |
| 96 </template> |
| 97 </test-fixture> |
| 98 |
| 99 <test-fixture id="FormPost"> |
| 100 <template> |
| 101 <form is="iron-form" action="/responds_with_json" method="post"> |
| 102 <simple-element name="zig" value="zag"></simple-element> |
| 103 </form> |
| 104 </template> |
| 105 </test-fixture> |
| 106 |
| 107 <test-fixture id="InvalidForm"> |
| 108 <template> |
| 109 <form is="iron-form" action="/responds_with_json" method="post"> |
| 110 <simple-element name="zig"></simple-element> |
| 111 <input name="foo" required> |
| 112 </form> |
| 113 </template> |
| 114 </test-fixture> |
| 115 |
| 116 <test-fixture id="FormWithRequiredCustomElements"> |
| 117 <template> |
| 118 <form is="iron-form" action="/responds_with_json" method="post"> |
| 119 <simple-element required></simple-element> |
| 120 </form> |
| 121 </template> |
| 122 </test-fixture> |
| 123 |
| 124 <test-fixture id="FormWithRequiredElements"> |
| 125 <template> |
| 126 <form is="iron-form" action="/responds_with_json" method="post"> |
| 127 <input required> |
| 128 </form> |
| 129 </template> |
| 130 </test-fixture> |
| 131 |
| 132 <test-fixture id="FormValidateNonRequiredCustomElements"> |
| 133 <template> |
| 134 <form is="iron-form" action="/responds_with_json" method="post"> |
| 135 <paper-input label="numbers" pattern="^[0-9]$" ></paper-input> |
| 136 </form> |
| 137 </template> |
| 138 </test-fixture> |
| 139 |
| 140 <test-fixture id="FormValidateNonRequiredElements"> |
| 141 <template> |
| 142 <form is="iron-form" action="/responds_with_json" method="post"> |
| 143 <input type="text" name="letters" pattern="^[a-z]$" > |
| 144 </form> |
| 145 </template> |
| 146 </test-fixture> |
| 147 |
| 148 <test-fixture id="FormValidateNestedElements"> |
| 149 <template> |
| 150 <form is="iron-form"> |
| 151 <validatable-element-with-nested-elements> |
| 152 <simple-element required></simple-element> |
| 153 <input required> |
| 154 </validatable-element-with-nested-elements> |
| 155 </form> |
| 156 </template> |
| 157 </test-fixture> |
| 158 |
| 159 <test-fixture id="FormForResetting"> |
| 160 <template> |
| 161 <form is="iron-form"> |
| 162 <simple-element name="zig" value="zag"></simple-element> |
| 163 <paper-input name="zig" value="zug"></paper-input> |
| 164 <paper-input name="blank" value=""></paper-input> |
| 165 <paper-textarea name="zig" value="zog"></paper-textarea> |
| 166 <paper-textarea name="undef"></paper-textarea> |
| 167 <paper-textarea name="blank" value=""></paper-textarea> |
| 168 <input name="blank" value=""> |
| 169 <input name="foo" value="bar"> |
| 170 <input type="checkbox" name="foo" value="bar1" checked> |
| 171 <input type="checkbox" name="foo" value="bar2"> |
| 172 </form> |
| 173 </template> |
| 174 </test-fixture> |
| 175 |
| 176 <test-fixture id="NativeSelect"> |
| 177 <template> |
| 178 <form is="iron-form"> |
| 179 <select name="numbers" multiple> |
| 180 <option value="one" selected>one</option> |
| 181 <option value="two">two</option> |
| 182 <option value="three" selected>three</option> |
| 183 <option value="four">four</option> |
| 184 </select> |
| 185 |
| 186 <select name="cheese"> |
| 187 <option value="yes" selected>yes</option> |
| 188 <option value="no">no</option> |
| 189 </select> |
| 190 </form> |
| 191 </template> |
| 192 </test-fixture> |
| 193 |
| 194 <script> |
| 195 suite('registration', function() { |
| 196 var f; |
| 197 test('elements can be registered', function() { |
| 198 f = fixture('Basic'); |
| 199 |
| 200 assert.equal(f._customElements.length, 1); |
| 201 assert.equal(f.elements.length, 1); |
| 202 }); |
| 203 |
| 204 test('elements can be unregistered', function(done) { |
| 205 f = fixture('Basic'); |
| 206 var element = f.querySelector('simple-element'); |
| 207 |
| 208 assert.equal(f._customElements.length, 1); |
| 209 assert.equal(f.elements.length, 1); |
| 210 |
| 211 f.removeChild(element); |
| 212 |
| 213 setTimeout(function() { |
| 214 assert.equal(f._customElements.length, 0); |
| 215 assert.equal(f.elements.length, 1); |
| 216 done(); |
| 217 }, 200); |
| 218 }); |
| 219 }); |
| 220 |
| 221 suite('validation', function() { |
| 222 test('custom elements are validated if they don\'t have a name', function()
{ |
| 223 var f = fixture('FormWithRequiredCustomElements'); |
| 224 assert.equal(f._customElements.length, 1); |
| 225 |
| 226 var simpleElement = f._customElements[0]; |
| 227 assert.isFalse(!!simpleElement.name) |
| 228 |
| 229 // custom elements |
| 230 assert.isFalse(f.validate()); |
| 231 assert.isTrue(simpleElement.invalid); |
| 232 |
| 233 simpleElement.value = 'batman'; |
| 234 |
| 235 assert.isTrue(f.validate()); |
| 236 assert.isFalse(simpleElement.invalid); |
| 237 |
| 238 // Since the elements don't have names, they don't get serialized. |
| 239 var json = f.serialize(); |
| 240 assert.equal(Object.keys(json).length, 0); |
| 241 }); |
| 242 |
| 243 test('native elements are validated if they don\'t have a name', function()
{ |
| 244 var f = fixture('FormWithRequiredElements'); |
| 245 assert.equal(f.elements.length, 1); |
| 246 |
| 247 var input = f.elements[0]; |
| 248 |
| 249 assert.isFalse(!!input.name) |
| 250 |
| 251 assert.isFalse(f.validate()); |
| 252 assert.isFalse(input.validity.valid); |
| 253 |
| 254 input.value = 'robin'; |
| 255 |
| 256 assert.isTrue(f.validate()); |
| 257 assert.isTrue(input.validity.valid); |
| 258 |
| 259 // Since the elements don't have names, they don't get serialized. |
| 260 var json = f.serialize(); |
| 261 assert.equal(Object.keys(json).length, 0); |
| 262 }); |
| 263 |
| 264 test('custom elements are validated if they have a name', function() { |
| 265 var f = fixture('FormWithRequiredCustomElements'); |
| 266 assert.equal(f._customElements.length, 1); |
| 267 |
| 268 var simpleElement = f._customElements[0]; |
| 269 simpleElement.name = 'zig'; |
| 270 |
| 271 assert.isFalse(f.validate()); |
| 272 assert.isTrue(simpleElement.invalid); |
| 273 |
| 274 simpleElement.value = 'batman'; |
| 275 |
| 276 assert.isTrue(f.validate()); |
| 277 assert.isFalse(simpleElement.invalid); |
| 278 |
| 279 // The elements have names, so they're serialized. |
| 280 var json = f.serialize(); |
| 281 assert.equal(Object.keys(json).length, 1); |
| 282 }); |
| 283 |
| 284 test('native elements are validated if they have a name', function() { |
| 285 var f = fixture('FormWithRequiredElements'); |
| 286 assert.equal(f.elements.length, 1); |
| 287 |
| 288 var input = f.elements[0]; |
| 289 input.name = 'zag'; |
| 290 |
| 291 assert.isFalse(f.validate()); |
| 292 assert.isFalse(input.validity.valid); |
| 293 |
| 294 input.value = 'robin'; |
| 295 |
| 296 assert.isTrue(f.validate()); |
| 297 assert.isTrue(input.validity.valid); |
| 298 |
| 299 // The elements have names, so they're serialized. |
| 300 var json = f.serialize(); |
| 301 assert.equal(Object.keys(json).length, 1); |
| 302 }); |
| 303 |
| 304 test('non-required custom elements are validated', function() { |
| 305 var |
| 306 f = fixture('FormValidateNonRequiredCustomElements'), |
| 307 elements = f.getEffectiveChildren(), |
| 308 input = elements && elements[0] |
| 309 ; |
| 310 |
| 311 assert.equal(elements.length, 1); |
| 312 |
| 313 assert.isTrue(f.validate()); |
| 314 assert.isTrue(input.validate()); |
| 315 |
| 316 input.value = "abcdefg"; |
| 317 |
| 318 assert.isFalse(f.validate()); |
| 319 assert.isFalse(input.validate()); |
| 320 }); |
| 321 |
| 322 test('non-required native elements are validated', function() { |
| 323 var f = fixture('FormValidateNonRequiredElements'); |
| 324 assert.equal(f.elements.length, 1); |
| 325 |
| 326 var input = f.elements[0]; |
| 327 |
| 328 assert.isTrue(f.validate()); |
| 329 assert.isTrue(input.validity.valid); |
| 330 |
| 331 input.value = "012345"; |
| 332 |
| 333 assert.isFalse(f.validate()); |
| 334 assert.isFalse(input.validity.valid); |
| 335 }); |
| 336 |
| 337 test('nested elements are not validated', function() { |
| 338 var f = fixture('FormValidateNestedElements'); |
| 339 |
| 340 // <validatable-element-with-nested-elements> returns true. |
| 341 assert.isTrue(f.validate()); |
| 342 |
| 343 // The form also contains an invalid custom and native element, but those |
| 344 // shouldn't affect the form's validity. |
| 345 assert.equal(f._customElements.length, 2); |
| 346 assert.equal(f.elements.length, 1); |
| 347 assert.isFalse(f.$$('simple-element').validate()); |
| 348 assert.isFalse(f.$$('input').checkValidity()); |
| 349 }); |
| 350 }); |
| 351 |
| 352 suite('serializing', function() { |
| 353 var f; |
| 354 test('serializes both custom and native elements', function() { |
| 355 f = fixture('Basic'); |
| 356 |
| 357 assert.equal(f._customElements.length, 1); |
| 358 assert.equal(f.elements.length, 1); |
| 359 |
| 360 var json = f.serialize(); |
| 361 assert.equal(Object.keys(json).length, 2); |
| 362 assert.equal(json['zig'], 'zag'); |
| 363 assert.equal(json['foo'], 'bar'); |
| 364 }); |
| 365 |
| 366 test('serializes elements with duplicate names', function() { |
| 367 f = fixture('Dupes'); |
| 368 |
| 369 assert.equal(f._customElements.length, 3); |
| 370 assert.equal(f.elements.length, 2); |
| 371 |
| 372 var json = f.serialize(); |
| 373 assert.equal(Object.keys(json).length, 2); |
| 374 assert.equal(json['foo'].length, 2); |
| 375 assert.equal(json['foo'][0], 'bar'); |
| 376 assert.equal(json['foo'][1], 'barbar'); |
| 377 assert.equal(json['zig'].length, 3); |
| 378 assert.equal(json['zig'][0], 'zig'); |
| 379 assert.equal(json['zig'][1], 'zag'); |
| 380 assert.equal(json['zig'][2], 'zug'); |
| 381 }); |
| 382 |
| 383 test('serializes elements with checked states', function() { |
| 384 f = fixture('CheckedStates'); |
| 385 |
| 386 assert.equal(f._customElements.length, 0); |
| 387 assert.equal(f.elements.length, 4); |
| 388 |
| 389 var json = f.serialize(); |
| 390 assert.equal(Object.keys(json).length, 1); |
| 391 assert.equal(json['foo'].length, 2); |
| 392 assert.equal(json['foo'][0], 'bar1'); |
| 393 assert.equal(json['foo'][1], 'bar3'); |
| 394 }); |
| 395 |
| 396 test('serializes a native select element with or without multiple selection'
, function() { |
| 397 f = fixture('NativeSelect'); |
| 398 |
| 399 assert.equal(f._customElements.length, 0); |
| 400 assert.equal(f.elements.length, 2); |
| 401 |
| 402 var json = f.serialize(); |
| 403 assert.equal(Object.keys(json).length, 2); |
| 404 |
| 405 // Single selection. |
| 406 assert.equal(json['cheese'], 'yes'); |
| 407 |
| 408 // Multiple selection. |
| 409 assert.equal(json['numbers'].length, 2); |
| 410 assert.equal(json['numbers'][0], 'one'); |
| 411 assert.equal(json['numbers'][1], 'three'); |
| 412 }); |
| 413 |
| 414 test('does not serialize disabled elements', function() { |
| 415 f = fixture('Disabled'); |
| 416 |
| 417 assert.equal(f._customElements.length, 0); |
| 418 assert.equal(f.elements.length, 3); |
| 419 |
| 420 var json = f.serialize(); |
| 421 assert.equal(Object.keys(json).length, 1); |
| 422 assert.equal(json['foo'], 'bar1'); |
| 423 }); |
| 424 |
| 425 test('nested elements are only serialized once', function() { |
| 426 f = fixture('NestedDupes'); |
| 427 |
| 428 assert.equal(f._customElements.length, 3); |
| 429 |
| 430 var json = f.serialize(); |
| 431 assert.equal(Object.keys(json).length, 2); |
| 432 assert.equal(json['foo'], 'bar'); |
| 433 assert.equal(json['zig'], 'zag'); |
| 434 }); |
| 435 |
| 436 |
| 437 test('nested elements can be submitted if parents aren\'t submittable', func
tion() { |
| 438 f = fixture('NestedSubmittable'); |
| 439 |
| 440 var json = f.serialize(); |
| 441 assert.equal(Object.keys(json).length, 1); |
| 442 assert.equal(json['foo'], 'bar'); |
| 443 }); |
| 444 |
| 445 }); |
| 446 |
| 447 suite('resetting', function () { |
| 448 test('form restores the default values if changes were made', function(done)
{ |
| 449 var form = fixture('FormForResetting'); |
| 450 |
| 451 // Initial values. |
| 452 var customElement = form.querySelector('simple-element'); |
| 453 var input = form.querySelector('input[name="foo"]'); |
| 454 var inputBlank = form.querySelector('input[name="blank"]'); |
| 455 var checkbox1 = form.querySelectorAll('input[type="checkbox"]')[0]; |
| 456 var checkbox2 = form.querySelectorAll('input[type="checkbox"]')[1]; |
| 457 var paperInput = form.querySelector('paper-input[name="zig"]'); |
| 458 var paperInputBlank = form.querySelector('paper-input[name="blank"]'); |
| 459 var paperTextarea = form.querySelector('paper-textarea[name="zig"]'); |
| 460 var paperTextareaBlank = form.querySelector('paper-textarea[name="blank"]'
); |
| 461 |
| 462 assert.equal(customElement.value, 'zag'); |
| 463 assert.equal(input.value, 'bar'); |
| 464 assert.equal(inputBlank.value, ''); |
| 465 assert.isTrue(checkbox1.checked); |
| 466 assert.isFalse(checkbox2.checked); |
| 467 assert.equal(paperInput.value, 'zug'); |
| 468 assert.equal(paperInput.inputElement.value, 'zug'); |
| 469 assert.equal(paperInputBlank.value, ''); |
| 470 assert.equal(paperInputBlank.inputElement.value, ''); |
| 471 assert.equal(paperTextarea.value, 'zog'); |
| 472 assert.equal(paperTextarea.inputElement.textarea.value, 'zog'); |
| 473 assert.equal(paperTextareaBlank.value, ''); |
| 474 assert.equal(paperTextareaBlank.inputElement.textarea.value, ''); |
| 475 |
| 476 // Change the values. |
| 477 customElement.value = 'not zag'; |
| 478 input.value = 'not bar'; |
| 479 inputBlank.value = 'not blank'; |
| 480 checkbox1.checked = false; |
| 481 checkbox2.checked = true; |
| 482 paperInput.value = 'not zug'; |
| 483 paperInputBlank.value = 'not blank'; |
| 484 paperTextarea.value = 'not zog'; |
| 485 paperTextareaBlank.value = 'not blank'; |
| 486 |
| 487 assert.equal(customElement.value, 'not zag'); |
| 488 assert.equal(input.value, 'not bar'); |
| 489 assert.equal(inputBlank.value, 'not blank'); |
| 490 assert.isFalse(checkbox1.checked); |
| 491 assert.isTrue(checkbox2.checked); |
| 492 assert.equal(paperInput.value, 'not zug'); |
| 493 assert.equal(paperInput.inputElement.value, 'not zug'); |
| 494 assert.equal(paperInputBlank.value, 'not blank'); |
| 495 assert.equal(paperInputBlank.inputElement.value, 'not blank'); |
| 496 assert.equal(paperTextarea.value, 'not zog'); |
| 497 assert.equal(paperTextarea.inputElement.textarea.value, 'not zog'); |
| 498 assert.equal(paperTextareaBlank.value, 'not blank'); |
| 499 assert.equal(paperTextareaBlank.inputElement.textarea.value, 'not blank'); |
| 500 |
| 501 form.addEventListener('iron-form-reset', function(event) { |
| 502 // Restored initial values. |
| 503 assert.equal(customElement.value, 'zag'); |
| 504 assert.equal(input.value, 'bar'); |
| 505 assert.equal(inputBlank.value, ''); |
| 506 assert.isTrue(checkbox1.checked); |
| 507 assert.isFalse(checkbox2.checked); |
| 508 assert.equal(paperInput.value, 'zug'); |
| 509 assert.equal(paperInput.inputElement.value, 'zug'); |
| 510 assert.equal(paperInputBlank.value, ''); |
| 511 assert.equal(paperInputBlank.inputElement.value, ''); |
| 512 assert.equal(paperTextarea.value, 'zog'); |
| 513 assert.equal(paperTextarea.inputElement.textarea.value, 'zog'); |
| 514 assert.equal(paperTextareaBlank.value, ''); |
| 515 assert.equal(paperTextareaBlank.inputElement.textarea.value, ''); |
| 516 done(); |
| 517 }); |
| 518 |
| 519 form.reset(); |
| 520 }); |
| 521 |
| 522 test('form restores the default values if no changes were made', function(do
ne) { |
| 523 var form = fixture('FormForResetting'); |
| 524 |
| 525 // Initial values. |
| 526 var customElement = form.querySelector('simple-element'); |
| 527 var input = form.querySelector('input[name="foo"]'); |
| 528 var inputBlank = form.querySelector('input[name="blank"]'); |
| 529 var checkbox1 = form.querySelectorAll('input[type="checkbox"]')[0]; |
| 530 var checkbox2 = form.querySelectorAll('input[type="checkbox"]')[1]; |
| 531 var paperInput = form.querySelector('paper-input[name="zig"]'); |
| 532 var paperInputBlank = form.querySelector('paper-input[name="blank"]'); |
| 533 var paperTextarea = form.querySelector('paper-textarea[name="zig"]'); |
| 534 var paperTextareaBlank = form.querySelector('paper-textarea[name="blank"]'
); |
| 535 |
| 536 assert.equal(customElement.value, 'zag'); |
| 537 assert.equal(input.value, 'bar'); |
| 538 assert.equal(inputBlank.value, ''); |
| 539 assert.isTrue(checkbox1.checked); |
| 540 assert.isFalse(checkbox2.checked); |
| 541 assert.equal(paperInput.value, 'zug'); |
| 542 assert.equal(paperInput.inputElement.value, 'zug'); |
| 543 assert.equal(paperInputBlank.value, ''); |
| 544 assert.equal(paperInputBlank.inputElement.value, ''); |
| 545 assert.equal(paperTextarea.value, 'zog'); |
| 546 assert.equal(paperTextarea.inputElement.textarea.value, 'zog'); |
| 547 assert.equal(paperTextareaBlank.value, ''); |
| 548 assert.equal(paperTextareaBlank.inputElement.textarea.value, ''); |
| 549 |
| 550 form.addEventListener('iron-form-reset', function(event) { |
| 551 // Restored initial values. |
| 552 assert.equal(customElement.value, 'zag'); |
| 553 assert.equal(input.value, 'bar'); |
| 554 assert.equal(inputBlank.value, ''); |
| 555 assert.isTrue(checkbox1.checked); |
| 556 assert.isFalse(checkbox2.checked); |
| 557 assert.equal(paperInput.value, 'zug'); |
| 558 assert.equal(paperInput.inputElement.value, 'zug'); |
| 559 assert.equal(paperInputBlank.value, ''); |
| 560 assert.equal(paperInputBlank.inputElement.value, ''); |
| 561 assert.equal(paperTextarea.value, 'zog'); |
| 562 assert.equal(paperTextarea.inputElement.textarea.value, 'zog'); |
| 563 assert.equal(paperTextareaBlank.value, ''); |
| 564 assert.equal(paperTextareaBlank.inputElement.textarea.value, ''); |
| 565 done(); |
| 566 }); |
| 567 |
| 568 form.reset(); |
| 569 }); |
| 570 |
| 571 test('form restores null-value paper-textarea if initially undefined', funct
ion(done) { |
| 572 var form = fixture('FormForResetting'); |
| 573 var paperTextarea = form.querySelector('paper-textarea[name="undef"]'); |
| 574 |
| 575 form.addEventListener('iron-form-reset', function(event) { |
| 576 // Setting the native textarea's value to undefined causes it to |
| 577 // display literal "undefined", and paper-textarea.value is indirectly |
| 578 // bound to the textarea's value, so iron-form resets any undefined |
| 579 // initial values to null in order to avoid this problem. |
| 580 assert.equal(paperTextarea.value, null); |
| 581 assert.equal(paperTextarea.inputElement.textarea.value, ''); |
| 582 done(); |
| 583 }); |
| 584 |
| 585 form.reset(); |
| 586 }); |
| 587 |
| 588 test('validation messages are cleared', function(done) { |
| 589 var form = fixture('FormWithRequiredCustomElements'); |
| 590 assert.equal(form._customElements.length, 1); |
| 591 |
| 592 var customElement = form.querySelector('simple-element'); |
| 593 |
| 594 assert.isFalse(form.validate()); |
| 595 assert.isTrue(customElement.invalid); |
| 596 |
| 597 form.addEventListener('iron-form-reset', function(event) { |
| 598 // Cleared validation messages. |
| 599 assert.isFalse(customElement.invalid); |
| 600 done(); |
| 601 }); |
| 602 |
| 603 form.reset(); |
| 604 |
| 605 }); |
| 606 |
| 607 }); |
| 608 |
| 609 suite('submitting', function () { |
| 610 var server; |
| 611 var form; |
| 612 |
| 613 setup(function() { |
| 614 server = sinon.fakeServer.create(); |
| 615 server.respondWith( |
| 616 'GET', |
| 617 /\/responds_with_json.*/, |
| 618 [ |
| 619 200, |
| 620 '{"Content-Type":"application/json"}', |
| 621 '{"success":true}' |
| 622 ] |
| 623 ); |
| 624 |
| 625 server.respondWith( |
| 626 'POST', |
| 627 /\/responds_with_json.*/, |
| 628 [ |
| 629 200, |
| 630 '{"Content-Type":"application/json"}', |
| 631 '{"success":true}' |
| 632 ] |
| 633 ); |
| 634 |
| 635 server.respondWith( |
| 636 'GET', |
| 637 /\/responds_with_error.*/, |
| 638 [ |
| 639 404, |
| 640 '{"Content-Type":"application/text"}', |
| 641 '{"success":false}' |
| 642 ] |
| 643 ); |
| 644 }); |
| 645 |
| 646 teardown(function() { |
| 647 server.restore(); |
| 648 }); |
| 649 |
| 650 test('does not submit forms with invalid native elements', function(done) { |
| 651 form = fixture('InvalidForm'); |
| 652 var nativeElement = form.querySelector('input'); |
| 653 var customElement = form.querySelector('simple-element'); |
| 654 customElement.value = "foo"; |
| 655 |
| 656 var submitted = false; |
| 657 form.addEventListener('iron-form-submit', function() { |
| 658 submitted = true; |
| 659 }); |
| 660 |
| 661 form.addEventListener('iron-form-invalid', function() { |
| 662 expect(submitted).to.be.equal(false); |
| 663 expect(nativeElement.validity.valid).to.be.equal(false); |
| 664 expect(customElement.invalid).to.be.equal(false); |
| 665 done(); |
| 666 }); |
| 667 |
| 668 form.submit(); |
| 669 server.respond(); |
| 670 }); |
| 671 |
| 672 test('can modify the request in the presubmit', function(done) { |
| 673 form = fixture('FormGet'); |
| 674 |
| 675 var submitted = false; |
| 676 var presubmitted = false; |
| 677 |
| 678 form.addEventListener('iron-form-submit', function() { |
| 679 submitted = true; |
| 680 }); |
| 681 |
| 682 form.addEventListener('iron-form-presubmit', function() { |
| 683 presubmitted = true; |
| 684 this.request.params = {batman: true}; |
| 685 }); |
| 686 |
| 687 form.addEventListener('iron-form-response', function(event) { |
| 688 expect(submitted).to.be.equal(true); |
| 689 expect(presubmitted).to.be.equal(true); |
| 690 |
| 691 // We have changed the json parameters |
| 692 expect(event.detail.url).to.contain('batman=true'); |
| 693 |
| 694 var response = event.detail.response; |
| 695 expect(response).to.be.ok; |
| 696 expect(response).to.be.an('object'); |
| 697 expect(response.success).to.be.equal(true); |
| 698 done(); |
| 699 }); |
| 700 |
| 701 form.submit(); |
| 702 server.respond(); |
| 703 }); |
| 704 |
| 705 |
| 706 test('can do a custom submission in the presubmit', function(done) { |
| 707 form = fixture('FormGet'); |
| 708 |
| 709 var presubmitted = false; |
| 710 |
| 711 // Since we are not using the normal form submission, these events should |
| 712 // never be called. |
| 713 var formResponseHandler = sinon.spy(); |
| 714 form.addEventListener('iron-form-response', formResponseHandler); |
| 715 var formSubmitHandler = sinon.spy(); |
| 716 form.addEventListener('iron-form-submit', formSubmitHandler); |
| 717 |
| 718 form.addEventListener('iron-form-presubmit', function(event) { |
| 719 presubmitted = true; |
| 720 event.preventDefault(); |
| 721 |
| 722 // Your custom submission logic could go here (like using Firebase). |
| 723 // In this case, fire a custom event as a an example. |
| 724 this.fire('custom-form-submit'); |
| 725 }); |
| 726 |
| 727 form.addEventListener('custom-form-submit', function(event) { |
| 728 expect(presubmitted).to.be.equal(true); |
| 729 expect(formResponseHandler.callCount).to.be.equal(0); |
| 730 expect(formSubmitHandler.callCount).to.be.equal(0); |
| 731 done(); |
| 732 }); |
| 733 |
| 734 form.submit(); |
| 735 }); |
| 736 |
| 737 test('can submit with method=get', function(done) { |
| 738 form = fixture('FormGet'); |
| 739 |
| 740 var submitted = false; |
| 741 form.addEventListener('iron-form-submit', function() { |
| 742 submitted = true; |
| 743 }); |
| 744 |
| 745 form.addEventListener('iron-form-response', function(event) { |
| 746 expect(submitted).to.be.equal(true); |
| 747 expect(event.detail.url).to.contain('zig=zag'); |
| 748 |
| 749 var response = event.detail.response; |
| 750 expect(response).to.be.ok; |
| 751 expect(response).to.be.an('object'); |
| 752 expect(response.success).to.be.equal(true); |
| 753 done(); |
| 754 }); |
| 755 |
| 756 form.submit(); |
| 757 server.respond(); |
| 758 }); |
| 759 |
| 760 test('can submit with method=post', function(done) { |
| 761 form = fixture('FormPost'); |
| 762 |
| 763 var submitted = false; |
| 764 form.addEventListener('iron-form-submit', function() { |
| 765 submitted = true; |
| 766 }); |
| 767 |
| 768 form.addEventListener('iron-form-response', function(event) { |
| 769 expect(submitted).to.be.equal(true); |
| 770 |
| 771 var response = event.detail.response; |
| 772 expect(response).to.be.ok; |
| 773 expect(response).to.be.an('object'); |
| 774 expect(response.success).to.be.equal(true); |
| 775 done(); |
| 776 }); |
| 777 |
| 778 form.submit(); |
| 779 server.respond(); |
| 780 }); |
| 781 |
| 782 test('can relay errors', function(done) { |
| 783 form = fixture('FormPost'); |
| 784 form.action = "/responds_with_error"; |
| 785 |
| 786 form.addEventListener('iron-form-error', function(event) { |
| 787 var error = event.detail; |
| 788 |
| 789 expect(error).to.be.ok; |
| 790 expect(error).to.be.an('object'); |
| 791 expect(error.error).to.be.ok; |
| 792 done(); |
| 793 }); |
| 794 |
| 795 form.submit(); |
| 796 server.respond(); |
| 797 }); |
| 798 |
| 799 }); |
| 800 |
| 801 </script> |
| 802 |
| 803 </body> |
| 804 </html> |
OLD | NEW |