<commit_before>'use strict';

require('../../../TestHelper');

/* global bootstrapDiagram, inject */


var interactionEventsModule = require('lib/features/interaction-events');


describe('features/interaction-events', function() {

  beforeEach(bootstrapDiagram({ modules: [ interactionEventsModule ] }));


  var rootShape, parentShape, childShape, childShape2, connection;

  beforeEach(inject(function(elementFactory, canvas) {

    rootShape = elementFactory.createRoot({
      id: 'root'
    });

    canvas.setRootElement(rootShape);

    parentShape = elementFactory.createShape({
      id: 'parent',
      x: 100, y: 100, width: 300, height: 300
    });

    canvas.addShape(parentShape, rootShape);

    childShape = elementFactory.createShape({
      id: 'child',
      x: 110, y: 110, width: 100, height: 100
    });

    canvas.addShape(childShape, parentShape);

    childShape2 = elementFactory.createShape({
      id: 'child2',
      x: 200, y: 110, width: 100, height: 100
    });

    canvas.addShape(childShape2, parentShape);

    connection = elementFactory.createConnection({
      id: 'connection',
      waypoints: [ { x: 150, y: 150 }, { x: 150, y: 200 }, { x: 350, y: 150 } ],
      source: childShape,
      target: childShape2
    });

    canvas.addConnection(connection, parentShape);
  }));


  describe('bootstrap', function() {

    it('should bootstrap diagram with component', inject(function() {}));

  });


  describe('event emitting', function() {

    it('should emit element.(click|hover|out|dblclick|contextmenu) events', inject(function(eventBus) {

      // TODO(nre): automate this test
      // we could mock raw dom events via custom events and ensure our correct synthetic events fire

      [ 'hover', 'out', 'click', 'dblclick', 'contextmenu' ].forEach(function(type) {
        eventBus.on('element.' + type, function(event) {
          console.log(type, event);
        });
      });

    }));

  });

});<commit_msg>test(interaction-events): add event emitting coverage<commit_after>'use strict';

require('../../../TestHelper');

/* global bootstrapDiagram, inject, sinon */


var interactionEventsModule = require('lib/features/interaction-events');

var bindings = {
  mouseover: 'element.hover',
  mouseout: 'element.out',
  click: 'element.click',
  dblclick: 'element.dblclick',
  mousedown: 'element.mousedown',
  mouseup: 'element.mouseup',
  contextmenu: 'element.contextmenu'
};


describe('features/interaction-events', function() {

  beforeEach(bootstrapDiagram({
    modules: [
      interactionEventsModule
    ]
  }));


  var rootShape,
      parentShape,
      childShape,
      childShape2,
      connection;

  beforeEach(inject(function(elementFactory, canvas) {

    rootShape = elementFactory.createRoot({
      id: 'root'
    });

    canvas.setRootElement(rootShape);

    parentShape = elementFactory.createShape({
      id: 'parent',
      x: 100, y: 100, width: 300, height: 300
    });

    canvas.addShape(parentShape, rootShape);

    childShape = elementFactory.createShape({
      id: 'child',
      x: 110, y: 110, width: 100, height: 100
    });

    canvas.addShape(childShape, parentShape);

    childShape2 = elementFactory.createShape({
      id: 'child2',
      x: 200, y: 110, width: 100, height: 100
    });

    canvas.addShape(childShape2, parentShape);

    connection = elementFactory.createConnection({
      id: 'connection',
      waypoints: [ { x: 150, y: 150 }, { x: 150, y: 200 }, { x: 350, y: 150 } ],
      source: childShape,
      target: childShape2
    });

    canvas.addConnection(connection, parentShape);
  }));


  describe('bootstrap', function() {

    it('should bootstrap diagram with component', inject(function() {}));

  });


  describe('event handling', function() {

    function verifyEvent(type, button) {

      var description =
        type +
        (button ? ' with button=' + button : '');

      it(description, inject(function(eventBus, elementRegistry) {

        // given
        var parentGfx = elementRegistry.getGraphics(parentShape);

        var listenerFn = sinon.spy(function(e) {
          expect(e.element).to.eql(parentShape);
          expect(e.gfx).to.eql(parentGfx);

          expect(e.type).to.eql(bindings[type]);
        });

        // given
        eventBus.on(bindings[type], listenerFn);

        // when
        triggerMouseEvent(parentGfx, type, button);

        // then
        expect(listenerFn).to.have.been.calledOnce;

        expect(listenerFn).not.to.have.thrown();
      }));
    }

    function verifyNoEvent(type, button) {

      var description =
        'should suppress ' + type +
        (button ? ' with button=' + button : '');

      it(description, inject(function(eventBus, elementRegistry) {

        // given
        var rootGfx = elementRegistry.getGraphics(rootShape);

        var listenerFn = sinon.spy();

        // given
        eventBus.on(bindings[type], listenerFn);

        // when
        triggerMouseEvent(rootGfx, type, button);

        // then
        expect(listenerFn).not.to.have.been.called;
      }));
    }

    describe('should handle', function() {

      // left-clicks
      Object.keys(bindings).forEach(function(event) {
        verifyEvent(event);
      });

      // <contextmenu> right-click
      verifyEvent('contextmenu', 2);

    });


    describe('should suppress', function() {

      // right-clicks
      [
        'click',
        'mousedown',
        'mouseup',
        'dblclick'
      ].forEach(function(event) {
        verifyNoEvent(event, 2);
      });

    });

  });

});


////////// helpers /////////////////////////

function triggerMouseEvent(gfx, type, button) {
  var event = mouseEvent(type, button);

  return gfx.dispatchEvent(event);
}

function mouseEvent(type, button) {

  if (!button) {
    button = 0;
  }

  var event = document.createEvent('MouseEvent');

  event.initMouseEvent(
    type, true, true, window,
    0, 0, 0, 80, 20,
    false, false, false, false,
    button, null
  );

  return event;
}
{'fraction_non_alphanumeric': 0.1490170163555262, 'fraction_numerical': 0.02114653890632744, 'mean_word_length': 2.741656365883807, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '4669028', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1758, 'n_words': 387}