| 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 <script src="../../test-fixture/test-fixture-mocha.js"></script> |
| 18 |
| 19 <link rel="import" href="../../polymer/polymer.html"> |
| 20 <link rel="import" href="../../test-fixture/test-fixture.html"> |
| 21 <link rel="import" href="../iron-form.html"> |
| 22 <link rel="import" href="simple-element.html"> |
| 23 |
| 24 </head> |
| 25 <body> |
| 26 |
| 27 <test-fixture id="Basic"> |
| 28 <template> |
| 29 <form is="iron-form"> |
| 30 <simple-element name="zig" value="zag"></simple-element> |
| 31 <input name="foo" value="bar"> |
| 32 </form> |
| 33 </template> |
| 34 </test-fixture> |
| 35 |
| 36 <test-fixture id="Dupes"> |
| 37 <template> |
| 38 <form is="iron-form"> |
| 39 <input name="foo" value="bar"> |
| 40 <input name="foo" value="barbar"> |
| 41 <simple-element name="zig" value="zig"></simple-element> |
| 42 <simple-element name="zig" value="zag"></simple-element> |
| 43 <simple-element name="zig" value="zug"></simple-element> |
| 44 </form> |
| 45 </template> |
| 46 </test-fixture> |
| 47 |
| 48 <test-fixture id="CheckedStates"> |
| 49 <template> |
| 50 <form is="iron-form"> |
| 51 <input type="checkbox" name="foo" value="bar1" checked> |
| 52 <input type="checkbox" name="foo" value="bar2"> |
| 53 <input type="checkbox" name="foo" value="bar3" checked> |
| 54 <input type="checkbox" name="foo" value="bar4"> |
| 55 </form> |
| 56 </template> |
| 57 </test-fixture> |
| 58 |
| 59 <test-fixture id="FormGet"> |
| 60 <template> |
| 61 <form is="iron-form" action="/responds_with_json" method="get"> |
| 62 <simple-element name="zig" value="zag"></simple-element> |
| 63 </form> |
| 64 </template> |
| 65 </test-fixture> |
| 66 |
| 67 <test-fixture id="FormPost"> |
| 68 <template> |
| 69 <form is="iron-form" action="/responds_with_json" method="post"> |
| 70 <simple-element name="zig" value="zag"></simple-element> |
| 71 </form> |
| 72 </template> |
| 73 </test-fixture> |
| 74 |
| 75 <test-fixture id="InvalidForm"> |
| 76 <template> |
| 77 <form is="iron-form" action="/responds_with_json" method="post"> |
| 78 <simple-element name="zig"></simple-element> |
| 79 <input name="foo" required> |
| 80 </form> |
| 81 </template> |
| 82 </test-fixture> |
| 83 |
| 84 |
| 85 <script> |
| 86 suite('serializing', function() { |
| 87 test('serializes both custom and native elements', function() { |
| 88 f = fixture('Basic'); |
| 89 |
| 90 assert.equal(f._customElements.length, 1); |
| 91 assert.equal(f.elements.length, 1); |
| 92 |
| 93 var json = f.serialize(); |
| 94 assert.equal(Object.keys(json).length, 2); |
| 95 assert.equal(json['zig'], 'zag'); |
| 96 assert.equal(json['foo'], 'bar'); |
| 97 }); |
| 98 |
| 99 test('serializes elements with duplicate names', function() { |
| 100 f = fixture('Dupes'); |
| 101 |
| 102 assert.equal(f._customElements.length, 3); |
| 103 assert.equal(f.elements.length, 2); |
| 104 |
| 105 var json = f.serialize(); |
| 106 assert.equal(Object.keys(json).length, 2); |
| 107 assert.equal(json['foo'].length, 2); |
| 108 assert.equal(json['foo'][0], 'bar'); |
| 109 assert.equal(json['foo'][1], 'barbar'); |
| 110 assert.equal(json['zig'].length, 3); |
| 111 assert.equal(json['zig'][0], 'zig'); |
| 112 assert.equal(json['zig'][1], 'zag'); |
| 113 assert.equal(json['zig'][2], 'zug'); |
| 114 }); |
| 115 |
| 116 test('serializes elements with checked states', function() { |
| 117 f = fixture('CheckedStates'); |
| 118 |
| 119 assert.equal(f._customElements.length, 0); |
| 120 assert.equal(f.elements.length, 4); |
| 121 |
| 122 var json = f.serialize(); |
| 123 assert.equal(Object.keys(json).length, 1); |
| 124 assert.equal(json['foo'].length, 2); |
| 125 assert.equal(json['foo'][0], 'bar1'); |
| 126 assert.equal(json['foo'][1], 'bar3'); |
| 127 }); |
| 128 |
| 129 }); |
| 130 |
| 131 suite('submitting', function () { |
| 132 var server; |
| 133 var form; |
| 134 |
| 135 setup(function() { |
| 136 server = sinon.fakeServer.create(); |
| 137 server.respondWith( |
| 138 'GET', |
| 139 /\/responds_with_json.*/, |
| 140 [ |
| 141 200, |
| 142 '{"Content-Type":"application/json"}', |
| 143 '{"success":true}' |
| 144 ] |
| 145 ); |
| 146 |
| 147 server.respondWith( |
| 148 'POST', |
| 149 /\/responds_with_json.*/, |
| 150 [ |
| 151 200, |
| 152 '{"Content-Type":"application/json"}', |
| 153 '{"success":true}' |
| 154 ] |
| 155 ); |
| 156 |
| 157 server.respondWith( |
| 158 'GET', |
| 159 /\/responds_with_error.*/, |
| 160 [ |
| 161 404, |
| 162 '{"Content-Type":"application/text"}', |
| 163 '{"success":false}' |
| 164 ] |
| 165 ); |
| 166 }); |
| 167 |
| 168 teardown(function() { |
| 169 server.restore(); |
| 170 }); |
| 171 |
| 172 test('does not submit forms with invalid native elements', function(done) { |
| 173 form = fixture('InvalidForm'); |
| 174 var nativeElement = form.querySelector('input'); |
| 175 var customElement = form.querySelector('simple-element'); |
| 176 customElement.value = "foo"; |
| 177 |
| 178 var submitted = false; |
| 179 form.addEventListener('iron-form-submit', function() { |
| 180 submitted = true; |
| 181 }); |
| 182 |
| 183 form.addEventListener('iron-form-invalid', function() { |
| 184 expect(submitted).to.be.equal(false); |
| 185 expect(nativeElement.validity.valid).to.be.equal(false); |
| 186 expect(customElement.invalid).to.be.equal(false); |
| 187 done(); |
| 188 }); |
| 189 |
| 190 form.submit(); |
| 191 server.respond(); |
| 192 }); |
| 193 |
| 194 test('can submit with method=get', function(done) { |
| 195 form = fixture('FormGet'); |
| 196 |
| 197 var submitted = false; |
| 198 form.addEventListener('iron-form-submit', function() { |
| 199 submitted = true; |
| 200 }); |
| 201 |
| 202 form.addEventListener('iron-form-response', function(event) { |
| 203 expect(submitted).to.be.equal(true); |
| 204 |
| 205 var response = event.detail; |
| 206 expect(response).to.be.ok; |
| 207 expect(response).to.be.an('object'); |
| 208 expect(response.success).to.be.equal(true); |
| 209 done(); |
| 210 }); |
| 211 |
| 212 form.submit(); |
| 213 server.respond(); |
| 214 }); |
| 215 |
| 216 test('can submit with method=post', function(done) { |
| 217 form = fixture('FormPost'); |
| 218 |
| 219 var submitted = false; |
| 220 form.addEventListener('iron-form-submit', function() { |
| 221 submitted = true; |
| 222 }); |
| 223 |
| 224 form.addEventListener('iron-form-response', function(event) { |
| 225 expect(submitted).to.be.equal(true); |
| 226 var response = event.detail; |
| 227 expect(response).to.be.ok; |
| 228 expect(response).to.be.an('object'); |
| 229 expect(response.success).to.be.equal(true); |
| 230 done(); |
| 231 }); |
| 232 |
| 233 form.submit(); |
| 234 server.respond(); |
| 235 }); |
| 236 |
| 237 test('can relay errors', function(done) { |
| 238 form = fixture('FormPost'); |
| 239 form.action = "/responds_with_error"; |
| 240 |
| 241 form.addEventListener('iron-form-error', function(event) { |
| 242 var error = event.detail; |
| 243 |
| 244 expect(error).to.be.ok; |
| 245 expect(error).to.be.an('object'); |
| 246 expect(error.error).to.be.ok; |
| 247 done(); |
| 248 }); |
| 249 |
| 250 form.submit(); |
| 251 server.respond(); |
| 252 }); |
| 253 |
| 254 }); |
| 255 |
| 256 </script> |
| 257 |
| 258 </body> |
| 259 </html> |
| OLD | NEW |