basic.html 5.33 KB
<!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="../../test-fixture/test-fixture-mocha.js"></script>
    <script src="../../iron-test-helpers/mock-interactions.js"></script>

    <link rel="import" href="../../test-fixture/test-fixture.html">
    <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="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');
        });

        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>