<commit_before><commit_msg>Add tests on 'velocity' component.<commit_after>var entityFactory = require('../helpers').entityFactory;

var EPS = 1e-6;

suite('velocity', function () {
  var el,
      component;

  suite('default', function () {
    setup(function (done) {
      el = this.el = entityFactory();
      el.setAttribute('velocity', '');
      el.addEventListener('loaded', function () {
        component = el.components.velocity;
        done();
      });
    });

    test('defaults to 0 0 0', function () {
      component.update();
      expect(el.getComputedAttribute('velocity')).to.shallowDeepEqual({x: 0, y: 0, z: 0});
    });

    test('updates position', function () {
      el.setAttribute('velocity', {x: 1, y: 2, z: 3});
      component.tick(100, 0.1);
      var position = el.getComputedAttribute('position');
      expect(position.x).to.be.closeTo(0.0001, EPS);
      expect(position.y).to.be.closeTo(0.0002, EPS);
      expect(position.z).to.be.closeTo(0.0003, EPS);
    });
  });

  suite('physics', function () {
    var el,
        component,
        physics = {
          data: {maxInterval: 0.00005},
          Phase: {SIMULATE: 0, RENDER: 1},
          addBehavior: function () {},
          removeBehavior: function () {}
        };

    setup(function (done) {
      el = this.el = entityFactory();
      el.sceneEl.systems.physics = physics;
      sinon.spy(physics, 'addBehavior');
      sinon.spy(physics, 'removeBehavior');
      el.setAttribute('velocity', '');
      el.addEventListener('loaded', function () {
        component = el.components.velocity;
        done();
      });
    });

    teardown(function () {
      physics.addBehavior.restore();
      physics.removeBehavior.restore();
    });

    test('registers with the physics system', function () {
      expect(physics.addBehavior).to.have.been.calledWith(component, physics.Phase.RENDER);
    });

    test('unregisters with the physics system', function () {
      el.removeAttribute('velocity');
      expect(physics.removeBehavior).to.have.been.calledWith(component, physics.Phase.RENDER);
    });

    test('defaults to 0 0 0', function () {
      component.update();
      expect(el.getComputedAttribute('position')).to.shallowDeepEqual({x: 0, y: 0, z: 0});
    });

    test('updates position', function () {
      el.setAttribute('velocity', {x: 1, y: 2, z: 3});
      component.tick(100, 0.1);
      expect(el.getComputedAttribute('position')).to.shallowDeepEqual({x: 0, y: 0, z: 0});
      component.step(100, 0.1 /* overridden by maxInterval */);
      var position = el.getComputedAttribute('position');
      expect(position.x).to.be.closeTo(0.00005, EPS);
      expect(position.y).to.be.closeTo(0.00010, EPS);
      expect(position.z).to.be.closeTo(0.00015, EPS);
    });
  });
});
{'fraction_non_alphanumeric': 0.17386647625847912, 'fraction_numerical': 0.02820421278114959, 'mean_word_length': 2.880886426592798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6116809', 'n_tokens_mistral': 945, 'n_tokens_neox': 870, 'n_words': 162}