aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/iron-autogrow-textarea/test/basic.html
blob: 51f3abab786c70656bd9039cd6d6af6f76374eb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
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
<!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-autogrow-textarea tests</title>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
      <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">

    <script src="../../webcomponentsjs/webcomponents-lite.js"></script>

    <script src="../../web-component-tester/browser.js"></script>
    <script src="../../iron-test-helpers/mock-interactions.js"></script>
    <link rel="import" href="../iron-autogrow-textarea.html">

  </head>
  <body>

    <test-fixture id="basic">
      <template>
        <iron-autogrow-textarea></iron-autogrow-textarea>
      </template>
    </test-fixture>

    <test-fixture id="has-bindValue">
      <template>
        <iron-autogrow-textarea bind-value="foobar"></iron-autogrow-textarea>
      </template>
    </test-fixture>

    <test-fixture id="has-value">
      <template>
        <iron-autogrow-textarea value="foobar"></iron-autogrow-textarea>
      </template>
    </test-fixture>

    <test-fixture id="rows">
      <template>
        <iron-autogrow-textarea rows="3"></iron-autogrow-textarea>
      </template>
    </test-fixture>

    <script>

      suite('basic', function() {

        test('setting bindValue sets textarea value', function() {
          var autogrow = fixture('basic');
          var textarea = autogrow.textarea;

          autogrow.bindValue = 'batman';
          assert.equal(textarea.value, autogrow.bindValue, 'textarea value equals to bindValue');
        });

        test('can set an initial bindValue', function() {
          var autogrow = fixture('has-bindValue');
          assert.equal(autogrow.textarea.value, 'foobar', 'textarea value equals to initial bindValue');
          assert.equal(autogrow.value, 'foobar', 'value equals to initial bindValue');
        });

        test('can set an initial value', function() {
          var autogrow = fixture('has-value');
          assert.equal(autogrow.textarea.value, 'foobar', 'textarea value equals to initial bindValue');
          assert.equal(autogrow.bindValue, 'foobar', 'textarea bindValue equals to initial value');
        });

        test('can update the value', function() {
          var autogrow = fixture('has-bindValue');
          assert.equal(autogrow.textarea.value, 'foobar', 'textarea value equals to initial bindValue');
          autogrow.value = 'batman';
          assert.equal(autogrow.textarea.value, 'batman', 'textarea value is updated');
          assert.equal(autogrow.bindValue, 'batman', 'bindValue is updated');
          assert.equal(autogrow.value, 'batman', 'value is updated');
        });

        test('can update the bindValue', function() {
          var autogrow = fixture('has-bindValue');
          assert.equal(autogrow.textarea.value, 'foobar', 'textarea value equals to initial bindValue');
          autogrow.bindValue = 'batman';
          assert.equal(autogrow.textarea.value, 'batman', 'textarea value is updated');
          assert.equal(autogrow.bindValue, 'batman', 'bindValue is updated');
          assert.equal(autogrow.value, 'batman', 'value is updated');
        });

        test('can set an initial number of rows', function() {
          var autogrow = fixture("rows");
          assert.equal(autogrow.textarea.rows, 3, 'textarea has rows=3');
        });

        test('adding rows grows the textarea', function() {
          var autogrow = fixture('basic');
          var initialHeight = autogrow.offsetHeight;

          autogrow.bindValue = 'batman\nand\nrobin';
          var finalHeight = autogrow.offsetHeight
          assert.isTrue(finalHeight > initialHeight);
        });

        test('removing rows shrinks the textarea', function() {
          var autogrow = fixture('basic');
          autogrow.bindValue = 'batman\nand\nrobin';
          var initialHeight = autogrow.offsetHeight;

          autogrow.bindValue = 'batman';
          var finalHeight = autogrow.offsetHeight
          assert.isTrue(finalHeight < initialHeight);
        });

        test('an undefined bindValue is the empty string', function() {
          var autogrow = fixture('basic');
          var initialHeight = autogrow.offsetHeight;

          autogrow.bindValue = 'batman\nand\nrobin';
          var finalHeight = autogrow.offsetHeight;
          assert.isTrue(finalHeight > initialHeight);

          autogrow.bindValue = undefined;
          assert.equal(autogrow.offsetHeight, initialHeight);
          assert.equal(autogrow.textarea.value, '');
        });

        test('textarea selection works', function() {
          var autogrow = fixture('basic');
          var textarea = autogrow.textarea;
          autogrow.bindValue = 'batman\nand\nrobin';

          autogrow.selectionStart = 3;
          autogrow.selectionEnd = 5;

          assert.equal(textarea.selectionStart, 3);
          assert.equal(textarea.selectionEnd, 5);
        });
      });

      suite('focus/blur events', function() {
        var input;

        setup(function() {
          input = fixture('basic');
        });

        test('focus/blur events fired on host element', function(done) {
          var nFocusEvents = 0;
          var nBlurEvents = 0;
          input.addEventListener('focus', function() {
            nFocusEvents += 1;
            // setTimeout to wait for potentially more, erroneous events
            setTimeout(function() {
              assert.equal(nFocusEvents, 1, 'one focus event fired');
              MockInteractions.blur(input.textarea);
            });
          });
          input.addEventListener('blur', function() {
            nBlurEvents += 1;
            // setTimeout to wait for potentially more, erroneous events
            setTimeout(function() {
              assert.equal(nBlurEvents, 1, 'one blur event fired');
              done();
            });
          });
          MockInteractions.focus(input.textarea);
        });

      });

      suite('validation', function() {
        test('a required textarea with no text is invalid', function() {
          var input = fixture('basic');
          input.required = true;
          assert.isFalse(input.validate());

          input.bindValue = 'batman';
          assert.isTrue(input.validate());
        });
      });

    </script>

  </body>
</html>