<commit_before>import helpers from '../../lib/helpers';
import skate from '../../../src/index';

describe('api/event', function () {
  it('should bind events', function () {
    var numTriggered = 0;
    var tagName = helpers.safeTagName('my-el');
    var MyEl = skate(tagName.safe, {
      events: {
        test: function () {
          ++numTriggered;
        }
      }
    });

    var myEl = new MyEl();

    skate.emit(myEl, 'test');
    expect(numTriggered).to.equal(1);
  });

  it('should bind to the component element', function () {
    var numTriggered = 0;
    var tagName = helpers.safeTagName('my-el');
    var events = {};
    var MyEl;

    events[`test ${tagName.safe}`] = function () {
      ++numTriggered;
    };

    MyEl = skate(tagName.safe, {
      events: events
    });

    var myEl = new MyEl();
    helpers.dispatchEvent('test', myEl);
    expect(numTriggered).to.equal(1);
  });

  it('should allow you to re-add the element back into the DOM', function () {
    var numTriggered = 0;
    var tagName = helpers.safeTagName('my-el');
    var MyEl = skate(tagName.safe, {
      events: {
        test: function () {
          ++numTriggered;
        }
      }
    });

    var myEl = new MyEl();
    document.body.appendChild(myEl);
    var par = myEl.parentNode;

    par.removeChild(myEl);
    par.appendChild(myEl);
    helpers.dispatchEvent('test', myEl);
    expect(numTriggered).to.equal(1);
  });

  it('should support delegate events', function () {
    var dispatched = 0;
    var { safe: tagName } = helpers.safeTagName('my-component');

    skate(tagName, {
      events: {
        'click': function (e) {
          ++dispatched;
          expect(this.tagName).to.equal(tagName.toUpperCase());
          expect(e.target.tagName).to.equal('SPAN');
          expect(e.currentTarget.tagName).to.equal(tagName.toUpperCase());
          expect(e.delegateTarget.tagName).to.equal(tagName.toUpperCase());
        },
        'click a': function (e) {
          ++dispatched;
          expect(this.tagName).to.equal(tagName.toUpperCase());
          expect(e.target.tagName).to.equal('SPAN');
          expect(e.currentTarget.tagName).to.equal(tagName.toUpperCase());
          expect(e.delegateTarget.tagName).to.equal('A');
        },
        'click span': function (e) {
          ++dispatched;
          expect(this.tagName).to.equal(tagName.toUpperCase());
          expect(e.target.tagName).to.equal('SPAN');
          expect(e.currentTarget.tagName).to.equal(tagName.toUpperCase());
          expect(e.delegateTarget.tagName).to.equal('SPAN');
        }
      },

      template: function () {
        this.innerHTML = '<a><span></span></a>';
      }
    });

    var inst = helpers.fixture(`<${tagName}></${tagName}>`).querySelector(tagName);

    skate.init(inst);
    helpers.dispatchEvent('click', inst.querySelector('span'));
    expect(dispatched).to.equal(3);
  });

  it('should support delegate blur and focus events', function () {
    var blur = false;
    var focus = false;
    var { safe: tagName } = helpers.safeTagName('my-component');

    skate(tagName, {
      events: {
        'blur input': function () {
          blur = true;
        },

        'focus input': function () {
          focus = true;
        }
      },

      prototype: {
        blur: function () {
          helpers.dispatchEvent('blur', this.querySelector('input'));
        },

        focus: function () {
          helpers.dispatchEvent('focus', this.querySelector('input'));
        }
      },

      template: function () {
        this.innerHTML = '<input>';
      }
    });

    var inst = skate.init(helpers.fixture(`<${tagName}></${tagName}>`).querySelector(tagName));

    inst.blur();
    expect(blur).to.equal(true);

    inst.focus();
    expect(focus).to.equal(true);
  });

  it('should support an array of event handlers for a given key', function () {
    var called = function () { ++calledNum; };
    var calledNum = 0;
    var { safe: tagName } = helpers.safeTagName('my-component');
    var MyComponent = skate(tagName, {
      events: {
        click: [
          called,
          called
        ]
      }
    });

    var myComponent = new MyComponent();
    helpers.dispatchEvent('click', myComponent);
    expect(calledNum).to.equal(2);
  });
});<commit_msg>Fix event tests in IE. Seems IE doesn't like it when you trigger a click custom event.<commit_after>import emit from '../../../src/api/emit';
import helpers from '../../lib/helpers';
import helperElement from '../../lib/element';
import skate from '../../../src/index';

describe('api/event', function () {
  it('should bind events', function () {
    var numTriggered = 0;
    var tagName = helpers.safeTagName('my-el');
    var MyEl = skate(tagName.safe, {
      events: {
        test: function () {
          ++numTriggered;
        }
      }
    });

    var myEl = new MyEl();

    skate.emit(myEl, 'test');
    expect(numTriggered).to.equal(1);
  });

  it('should bind to the component element', function () {
    var numTriggered = 0;
    var tagName = helpers.safeTagName('my-el');
    var events = {};
    var MyEl;

    events[`test ${tagName.safe}`] = function () {
      ++numTriggered;
    };

    MyEl = skate(tagName.safe, {
      events: events
    });

    var myEl = new MyEl();
    helpers.dispatchEvent('test', myEl);
    expect(numTriggered).to.equal(1);
  });

  it('should allow you to re-add the element back into the DOM', function () {
    var numTriggered = 0;
    var tagName = helpers.safeTagName('my-el');
    var MyEl = skate(tagName.safe, {
      events: {
        test: function () {
          ++numTriggered;
        }
      }
    });

    var myEl = new MyEl();
    document.body.appendChild(myEl);
    var par = myEl.parentNode;

    par.removeChild(myEl);
    par.appendChild(myEl);
    helpers.dispatchEvent('test', myEl);
    expect(numTriggered).to.equal(1);
  });

  it('should support delegate events', function () {
    var dispatched = 0;
    var { safe: tagName } = helpers.safeTagName('my-component');

    skate(tagName, {
      events: {
        'click': function (e) {
          ++dispatched;
          expect(this.tagName).to.equal(tagName.toUpperCase());
          expect(e.target.tagName).to.equal('SPAN');
          expect(e.currentTarget.tagName).to.equal(tagName.toUpperCase());
          expect(e.delegateTarget.tagName).to.equal(tagName.toUpperCase());
        },
        'click a': function (e) {
          ++dispatched;
          expect(this.tagName).to.equal(tagName.toUpperCase());
          expect(e.target.tagName).to.equal('SPAN');
          expect(e.currentTarget.tagName).to.equal(tagName.toUpperCase());
          expect(e.delegateTarget.tagName).to.equal('A');
        },
        'click span': function (e) {
          ++dispatched;
          expect(this.tagName).to.equal(tagName.toUpperCase());
          expect(e.target.tagName).to.equal('SPAN');
          expect(e.currentTarget.tagName).to.equal(tagName.toUpperCase());
          expect(e.delegateTarget.tagName).to.equal('SPAN');
        }
      },

      template: function () {
        this.innerHTML = '<a><span></span></a>';
      }
    });

    var inst = helpers.fixture(`<${tagName}></${tagName}>`).querySelector(tagName);

    skate.init(inst);
    helpers.dispatchEvent('click', inst.querySelector('span'));
    expect(dispatched).to.equal(3);
  });

  it('should support delegate blur and focus events', function () {
    var blur = false;
    var focus = false;
    var { safe: tagName } = helpers.safeTagName('my-component');

    skate(tagName, {
      events: {
        'blur input': function () {
          blur = true;
        },

        'focus input': function () {
          focus = true;
        }
      },

      prototype: {
        blur: function () {
          helpers.dispatchEvent('blur', this.querySelector('input'));
        },

        focus: function () {
          helpers.dispatchEvent('focus', this.querySelector('input'));
        }
      },

      template: function () {
        this.innerHTML = '<input>';
      }
    });

    var inst = skate.init(helpers.fixture(`<${tagName}></${tagName}>`).querySelector(tagName));

    inst.blur();
    expect(blur).to.equal(true);

    inst.focus();
    expect(focus).to.equal(true);
  });

  it('should support an array of event handlers for a given key', function () {
    var increment = function () { ++calledNum; };
    var calledNum = 0;
    var safe = helperElement();

    skate(safe.safe, {
      events: {
        test: [
          increment,
          increment
        ]
      }
    });

    var el = safe.create();
    emit(el, 'test');
    expect(calledNum).to.equal(2);
  });
});
{'fraction_non_alphanumeric': 0.1669345579793341, 'fraction_numerical': 0.002296211251435132, 'mean_word_length': 2.363320463320463, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '1840899', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2706, 'n_words': 504}