File "field_image_test.js"

Full path: /usr/home/mndrn/domains/mndrn.ru/public_html/block-hill/blockly/tests/mocha/field_image_test.js
File size: 7.57 KiB (7750 bytes)
MIME-type: text/plain
Charset: utf-8

Download   Open   Back

/**
 * @license
 * Copyright 2019 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

suite('Image Fields', function() {
  setup(function() {
    sharedTestSetup.call(this);
  });
  teardown(function() {
    sharedTestTeardown.call(this);
  });
  function assertValue(imageField, expectedValue, expectedText) {
    var actualValue = imageField.getValue();
    var actualText = imageField.getText();
    chai.assert.equal(actualValue, expectedValue);
    chai.assert.equal(actualText, expectedText);
  }
  suite('Constructor', function() {
    test('Empty', function() {
      chai.assert.throws(function() {
        new Blockly.FieldImage();
      });
    });
    test('Undefined Src', function() {
      chai.assert.throws(function() {
        new Blockly.FieldImage(undefined, 1, 1);
      });
    });
    test('Undefined Size', function() {
      chai.assert.throws(function() {
        new Blockly.FieldImage('src', undefined, undefined);
      });
    });
    test('Zero Size', function() {
      chai.assert.throws(function() {
        new Blockly.FieldImage('src', 0, 0);
      });
    });
    test('Non-Parsable String for Size', function() {
      chai.assert.throws(function() {
        new Blockly.FieldImage('src', 'bad', 'bad');
      });
    });
    test('With Alt', function() {
      var imageField = new Blockly.FieldImage('src', 1, 1, 'alt');
      assertValue(imageField, 'src', 'alt');
    });
    test('Without Alt', function() {
      var imageField = new Blockly.FieldImage('src', 1, 1);
      assertValue(imageField, 'src', '');
    });
  });
  suite('fromJson', function() {
    test('Empty', function() {
      chai.assert.throws(function() {
        Blockly.FieldImage.fromJson({});
      });
    });
    test('Undefined Src', function() {
      chai.assert.throws(function() {
        Blockly.FieldImage.fromJson({
          src: undefined,
          width: 1,
          height: 1
        });
      });
    });
    test('Undefined Size', function() {
      chai.assert.throws(function() {
        Blockly.FieldImage.fromJson({
          src: 'src',
          width: undefined,
          height: undefined
        });
      });
    });
    test('Non-Parsable String for Size', function() {
      chai.assert.throws(function() {
        Blockly.FieldImage.fromJson({
          src: 'src',
          width: 'bad',
          height: 'bad'
        });
      });
    });
    test('With Alt', function() {
      var imageField = Blockly.FieldImage.fromJson({
        src: 'src',
        width: 1,
        height: 1,
        alt: 'alt'
      });
      assertValue(imageField, 'src', 'alt');
    });
    test('Without Alt', function() {
      var imageField = Blockly.FieldImage.fromJson({
        src: 'src',
        width: 1,
        height: 1
      });
      assertValue(imageField, 'src', '');
    });
  });
  suite('setValue', function() {
    setup(function() {
      this.imageField = new Blockly.FieldImage('src', 1, 1, 'alt');
    });
    test('Null', function() {
      this.imageField.setValue(null);
      assertValue(this.imageField, 'src', 'alt');
    });
    test('Undefined', function() {
      this.imageField.setValue(undefined);
      assertValue(this.imageField, 'src', 'alt');
    });
    test('Good Src', function() {
      this.imageField.setValue('newSrc');
      assertValue(this.imageField, 'newSrc', 'alt');
    });
  });
  suite('Customizations', function() {
    suite('On Click Handler', function() {
      setup(function() {
        this.onClick = function() {
          console.log('on click');
        };
      });
      test('JS Constructor', function() {
        var field = new Blockly.FieldImage('src', 10, 10, null, this.onClick);
        chai.assert.equal(field.clickHandler_, this.onClick);
      });
      test('setOnClickHandler', function() {
        var field = new Blockly.FieldImage('src', 10, 10);
        field.setOnClickHandler(this.onClick);
        chai.assert.equal(field.clickHandler_, this.onClick);
      });
      test('Remove Click Handler', function() {
        var field = new Blockly.FieldImage('src', 10, 10, null, this.onClick);
        field.setOnClickHandler(null);
        chai.assert.isNull(field.clickHandler_);
      });
    });
    suite('Alt', function() {
      test('JS Constructor', function() {
        var field = new Blockly.FieldImage('src', 10, 10, 'alt');
        chai.assert.equal(field.altText_, 'alt');
      });
      test('JSON Definition', function() {
        var field = Blockly.FieldImage.fromJson({
          src: 'src',
          width: 10,
          height: 10,
          alt: 'alt'
        });
        chai.assert.equal(field.altText_, 'alt');
      });
      test('Deprecated - setText', function() {
        var deprecateWarnSpy = createDeprecationWarningStub();
        var field = new Blockly.FieldImage('src', 10, 10, 'alt');
        chai.assert.throws(function() {
          field.setText('newAlt');
        });
        assertSingleDeprecationWarningCall(deprecateWarnSpy,
            'Field.prototype.setText');
      });
      suite('SetAlt', function() {
        setup(function() {
          this.imageField = new Blockly.FieldImage('src', 10, 10, 'alt');
        });
        test('Null', function() {
          this.imageField.setAlt(null);
          assertValue(this.imageField, 'src', '');
        });
        test('Empty String', function() {
          this.imageField.setAlt('');
          assertValue(this.imageField, 'src', '');
        });
        test('Good Alt', function() {
          this.imageField.setAlt('newAlt');
          assertValue(this.imageField, 'src', 'newAlt');
        });
      });
      test('JS Configuration - Simple', function() {
        var field = new Blockly.FieldImage('src', 10, 10, null, null, null, {
          alt: 'alt'
        });
        chai.assert.equal(field.altText_, 'alt');
      });
      test('JS Configuration - Ignore', function() {
        var field = new Blockly.FieldImage('src', 10, 10, 'alt', null, null, {
          alt: 'configAlt'
        });
        chai.assert.equal(field.altText_, 'configAlt');
      });
      test('JS Configuration - Ignore - \'\'', function() {
        var field = new Blockly.FieldImage('src', 10, 10, '', null, null, {
          alt: 'configAlt'
        });
        chai.assert.equal(field.altText_, 'configAlt');
      });
      test('JS Configuration - Ignore - Config \'\'', function() {
        var field = new Blockly.FieldImage('src', 10, 10, 'alt', null, null, {
          alt: ''
        });
        chai.assert.equal(field.altText_, '');
      });
    });
    suite('Flip RTL', function() {
      test('JS Constructor', function() {
        var field = new Blockly.FieldImage('src', 10, 10, null, null, true);
        chai.assert.isTrue(field.getFlipRtl());
      });
      test('JSON Definition', function() {
        var field = Blockly.FieldImage.fromJson({
          src: 'src',
          width: 10,
          height: 10,
          flipRtl: true
        });
        chai.assert.isTrue(field.getFlipRtl());
      });
      test('JS Configuration - Simple', function() {
        var field = new Blockly.FieldImage('src', 10, 10, null, null, null, {
          flipRtl: true
        });
        chai.assert.isTrue(field.getFlipRtl());
      });
      test('JS Configuration - Ignore - True', function() {
        var field = new Blockly.FieldImage('src', 10, 10, null, null, true, {
          flipRtl: false
        });
        chai.assert.isFalse(field.getFlipRtl());
      });
      test('JS Configuration - Ignore - False', function() {
        var field = new Blockly.FieldImage('src', 10, 10, null, null, false, {
          flipRtl: true
        });
        chai.assert.isTrue(field.getFlipRtl());
      });
    });
  });
});

PHP File Manager