Blame view

bower_components/iron-form/test/basic.html 13.8 KB
73bcce88   luigser   COMPONENTS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  <!doctype html>
  <!--
  @license
  Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
  This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  Code distributed by Google as part of the polymer project is also
  subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  -->
  <html>
  <head>
    <title>iron-form</title>
  
    <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
    <script src="../../web-component-tester/browser.js"></script>
73bcce88   luigser   COMPONENTS
17
18
  
    <link rel="import" href="../../polymer/polymer.html">
73bcce88   luigser   COMPONENTS
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    <link rel="import" href="../iron-form.html">
    <link rel="import" href="simple-element.html">
  
  </head>
  <body>
  
    <test-fixture id="Basic">
      <template>
        <form is="iron-form">
          <simple-element name="zig" value="zag"></simple-element>
          <input name="foo" value="bar">
        </form>
      </template>
    </test-fixture>
  
    <test-fixture id="Dupes">
      <template>
        <form is="iron-form">
          <input name="foo" value="bar">
          <input name="foo" value="barbar">
          <simple-element name="zig" value="zig"></simple-element>
          <simple-element name="zig" value="zag"></simple-element>
          <simple-element name="zig" value="zug"></simple-element>
        </form>
      </template>
    </test-fixture>
  
    <test-fixture id="CheckedStates">
      <template>
        <form is="iron-form">
          <input type="checkbox" name="foo" value="bar1" checked>
          <input type="checkbox" name="foo" value="bar2">
          <input type="checkbox" name="foo" value="bar3" checked>
          <input type="checkbox" name="foo" value="bar4">
        </form>
      </template>
    </test-fixture>
  
    <test-fixture id="Disabled">
      <template>
        <form is="iron-form">
          <input name="foo" value="bar1">
          <input name="foo" value="bar2" disabled>
          <input type="checkbox" name="zig" value="zag" disabled checked>
        </form>
      </template>
    </test-fixture>
  
    <test-fixture id="FormGet">
      <template>
        <form is="iron-form" action="/responds_with_json" method="get">
          <simple-element name="zig" value="zag"></simple-element>
        </form>
      </template>
    </test-fixture>
  
    <test-fixture id="FormPost">
      <template>
        <form is="iron-form" action="/responds_with_json" method="post">
          <simple-element name="zig" value="zag"></simple-element>
        </form>
      </template>
    </test-fixture>
  
    <test-fixture id="InvalidForm">
      <template>
        <form is="iron-form" action="/responds_with_json" method="post">
          <simple-element name="zig"></simple-element>
          <input name="foo" required>
        </form>
      </template>
    </test-fixture>
  
e619a3b0   Luigi Serra   Controllet cross ...
92
93
94
95
96
97
98
99
100
    <test-fixture id="FormWithRequiredElements">
      <template>
        <form is="iron-form" action="/responds_with_json" method="post">
          <simple-element required></simple-element>
          <input required>
        </form>
      </template>
    </test-fixture>
  
a1a3bc73   Luigi Serra   graphs updates
101
102
103
104
105
106
107
108
109
110
111
    <test-fixture id="FormForResetting">
      <template>
        <form is="iron-form">
          <simple-element name="zig" value="zag"></simple-element>
          <input name="foo" value="bar">
          <input type="checkbox" name="foo" value="bar1" checked>
          <input type="checkbox" name="foo" value="bar2">
        </form>
      </template>
    </test-fixture>
  
73bcce88   luigser   COMPONENTS
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    <script>
    suite('registration', function() {
      var f;
      test('elements can be registered', function() {
        f = fixture('Basic');
  
        assert.equal(f._customElements.length, 1);
        assert.equal(f.elements.length, 1);
      });
  
      test('elements can be unregistered', function(done) {
        f = fixture('Basic');
        var element = f.querySelector('simple-element');
  
        assert.equal(f._customElements.length, 1);
        assert.equal(f.elements.length, 1);
  
        f.removeChild(element);
  
        setTimeout(function() {
          assert.equal(f._customElements.length, 0);
          assert.equal(f.elements.length, 1);
          done();
        }, 200);
      });
    });
  
e619a3b0   Luigi Serra   Controllet cross ...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    suite('validation', function() {
      test('elements are validated if they don\'t have a name', function() {
        var f = fixture('FormWithRequiredElements');
        assert.equal(f._customElements.length, 1);
        assert.equal(f.elements.length, 1);
  
        var simpleElement = f._customElements[0];
        var input = f.elements[0];
  
        assert.isFalse(f.validate());
        assert.isTrue(simpleElement.invalid);
        assert.isFalse(input.validity.valid);
  
        simpleElement.value = 'batman';
        input.value = 'robin';
  
        assert.isTrue(f.validate());
        assert.isFalse(simpleElement.invalid);
        assert.isTrue(input.validity.valid);
  
        // Since the elements don't have names, they don't get serialized.
        var json = f.serialize();
        assert.equal(Object.keys(json).length, 0);
      });
  
      test('elements are validated if they have a name', function() {
        var f = fixture('FormWithRequiredElements');
        assert.equal(f._customElements.length, 1);
        assert.equal(f.elements.length, 1);
  
        var simpleElement = f._customElements[0];
        var input = f.elements[0];
        simpleElement.name = 'zig';
        input.name = 'zag';
  
        assert.isFalse(f.validate());
        assert.isTrue(simpleElement.invalid);
        assert.isFalse(input.validity.valid);
  
        simpleElement.value = 'batman';
        input.value = 'robin';
  
        assert.isTrue(f.validate());
        assert.isFalse(simpleElement.invalid);
        assert.isTrue(input.validity.valid);
  
        // The elements have names, so they're serialized.
        var json = f.serialize();
        assert.equal(Object.keys(json).length, 2);
      });
    });
  
73bcce88   luigser   COMPONENTS
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    suite('serializing', function() {
      var f;
      test('serializes both custom and native elements', function() {
        f = fixture('Basic');
  
        assert.equal(f._customElements.length, 1);
        assert.equal(f.elements.length, 1);
  
        var json = f.serialize();
        assert.equal(Object.keys(json).length, 2);
        assert.equal(json['zig'], 'zag');
        assert.equal(json['foo'], 'bar');
      });
  
      test('serializes elements with duplicate names', function() {
        f = fixture('Dupes');
  
        assert.equal(f._customElements.length, 3);
        assert.equal(f.elements.length, 2);
  
        var json = f.serialize();
        assert.equal(Object.keys(json).length, 2);
        assert.equal(json['foo'].length, 2);
        assert.equal(json['foo'][0], 'bar');
        assert.equal(json['foo'][1], 'barbar');
        assert.equal(json['zig'].length, 3);
        assert.equal(json['zig'][0], 'zig');
        assert.equal(json['zig'][1], 'zag');
        assert.equal(json['zig'][2], 'zug');
      });
  
      test('serializes elements with checked states', function() {
        f = fixture('CheckedStates');
  
        assert.equal(f._customElements.length, 0);
        assert.equal(f.elements.length, 4);
  
        var json = f.serialize();
        assert.equal(Object.keys(json).length, 1);
        assert.equal(json['foo'].length, 2);
        assert.equal(json['foo'][0], 'bar1');
        assert.equal(json['foo'][1], 'bar3');
      });
  
      test('does not serialize disabled elements', function() {
        f = fixture('Disabled');
  
        assert.equal(f._customElements.length, 0);
        assert.equal(f.elements.length, 3);
  
        var json = f.serialize();
        assert.equal(Object.keys(json).length, 1);
        assert.equal(json['foo'], 'bar1');
      });
  
    });
  
a1a3bc73   Luigi Serra   graphs updates
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    suite('resetting', function () {
      test('form restores the default values', function(done) {
        var form = fixture('FormForResetting');
  
        assert.equal(form._customElements.length, 1);
        assert.equal(form.elements.length, 3);
  
        // Initial values.
        var customElement = form.querySelector('simple-element');
        var input = form.querySelector('input[name="foo"]');
        var checkbox1 = form.querySelectorAll('input[type="checkbox"]')[0];
        var checkbox2 = form.querySelectorAll('input[type="checkbox"]')[1];
  
        assert.equal(customElement.value, 'zag');
        assert.equal(input.value, 'bar');
        assert.isTrue(checkbox1.checked);
        assert.isFalse(checkbox2.checked);
  
        // Change the values.
        customElement.value = 'not zag';
        input.value = 'not bar';
        checkbox1.checked = false;
        checkbox2.checked = true;
        assert.equal(customElement.value, 'not zag');
        assert.equal(input.value, 'not bar');
        assert.isFalse(checkbox1.checked);
        assert.isTrue(checkbox2.checked);
  
        form.addEventListener('iron-form-reset', function(event) {
          // Restored initial values.
          assert.equal(customElement.value, 'zag');
          assert.equal(input.value, 'bar');
          assert.isTrue(checkbox1.checked);
          assert.isFalse(checkbox2.checked);
          done();
        });
  
        form.reset();
  
      });
    });
  
73bcce88   luigser   COMPONENTS
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
    suite('submitting', function () {
      var server;
      var form;
  
      setup(function() {
        server = sinon.fakeServer.create();
        server.respondWith(
          'GET',
          /\/responds_with_json.*/,
          [
            200,
            '{"Content-Type":"application/json"}',
            '{"success":true}'
          ]
        );
  
        server.respondWith(
          'POST',
          /\/responds_with_json.*/,
          [
            200,
            '{"Content-Type":"application/json"}',
            '{"success":true}'
          ]
        );
  
        server.respondWith(
          'GET',
          /\/responds_with_error.*/,
          [
            404,
            '{"Content-Type":"application/text"}',
            '{"success":false}'
          ]
        );
      });
  
      teardown(function() {
        server.restore();
      });
  
      test('does not submit forms with invalid native elements', function(done) {
        form = fixture('InvalidForm');
        var nativeElement = form.querySelector('input');
        var customElement = form.querySelector('simple-element');
        customElement.value = "foo";
  
        var submitted = false;
        form.addEventListener('iron-form-submit', function() {
          submitted = true;
        });
  
        form.addEventListener('iron-form-invalid', function() {
          expect(submitted).to.be.equal(false);
          expect(nativeElement.validity.valid).to.be.equal(false);
          expect(customElement.invalid).to.be.equal(false);
          done();
        });
  
        form.submit();
        server.respond();
      });
  
a1a3bc73   Luigi Serra   graphs updates
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
      test('can modify the request in the presubmit', function(done) {
        form = fixture('FormGet');
  
        var submitted = false;
        var presubmitted = false;
  
        form.addEventListener('iron-form-submit', function() {
          submitted = true;
        });
  
        form.addEventListener('iron-form-presubmit', function() {
          presubmitted = true;
          this.request.params = {batman: true};
        });
  
        form.addEventListener('iron-form-response', function(event) {
          expect(submitted).to.be.equal(true);
          expect(presubmitted).to.be.equal(true);
  
          // We have changed the json parameters
          expect(event.detail.url).to.contain('batman=true');
  
          var response = event.detail.response;
          expect(response).to.be.ok;
          expect(response).to.be.an('object');
          expect(response.success).to.be.equal(true);
          done();
        });
  
        form.submit();
        server.respond();
      });
  
  
      test('can do a custom submission in the presubmit', function(done) {
        form = fixture('FormGet');
  
        var presubmitted = false;
  
        // Since we are not using the normal form submission, these events should
        // never be called.
        var formResponseHandler = sinon.spy();
        form.addEventListener('iron-form-response', formResponseHandler);
        var formSubmitHandler = sinon.spy();
        form.addEventListener('iron-form-submit', formSubmitHandler);
  
        form.addEventListener('iron-form-presubmit', function(event) {
          presubmitted = true;
          event.preventDefault();
  
          // Your custom submission logic could go here (like using Firebase).
          // In this case, fire a custom event as a an example.
          this.fire('custom-form-submit');
        });
  
        form.addEventListener('custom-form-submit', function(event) {
          expect(presubmitted).to.be.equal(true);
          expect(formResponseHandler.callCount).to.be.equal(0);
          expect(formSubmitHandler.callCount).to.be.equal(0);
          done();
        });
  
        form.submit();
      });
  
73bcce88   luigser   COMPONENTS
418
419
420
421
422
423
424
425
426
427
      test('can submit with method=get', function(done) {
        form = fixture('FormGet');
  
        var submitted = false;
        form.addEventListener('iron-form-submit', function() {
          submitted = true;
        });
  
        form.addEventListener('iron-form-response', function(event) {
          expect(submitted).to.be.equal(true);
a1a3bc73   Luigi Serra   graphs updates
428
          expect(event.detail.url).to.contain('zig=zag');
73bcce88   luigser   COMPONENTS
429
  
eb240478   Luigi Serra   public room cards...
430
          var response = event.detail.response;
73bcce88   luigser   COMPONENTS
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
          expect(response).to.be.ok;
          expect(response).to.be.an('object');
          expect(response.success).to.be.equal(true);
          done();
        });
  
        form.submit();
        server.respond();
      });
  
      test('can submit with method=post', function(done) {
        form = fixture('FormPost');
  
        var submitted = false;
        form.addEventListener('iron-form-submit', function() {
          submitted = true;
        });
  
        form.addEventListener('iron-form-response', function(event) {
          expect(submitted).to.be.equal(true);
a1a3bc73   Luigi Serra   graphs updates
451
  
eb240478   Luigi Serra   public room cards...
452
          var response = event.detail.response;
73bcce88   luigser   COMPONENTS
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
          expect(response).to.be.ok;
          expect(response).to.be.an('object');
          expect(response.success).to.be.equal(true);
          done();
        });
  
        form.submit();
        server.respond();
      });
  
      test('can relay errors', function(done) {
        form = fixture('FormPost');
        form.action = "/responds_with_error";
  
        form.addEventListener('iron-form-error', function(event) {
          var error = event.detail;
  
          expect(error).to.be.ok;
          expect(error).to.be.an('object');
          expect(error.error).to.be.ok;
          done();
        });
  
        form.submit();
        server.respond();
      });
  
    });
  
    </script>
  
  </body>
  </html>