<commit_before>"use strict";
var testcase = require('nodeunit').testCase;
var jsdom = require("../..");

class EventMonitor {
  constructor() {
    this.atEvents = [];
    this.bubbledEvents = [];
    this.capturedEvents = [];
    this.allEvents = [];

    this.handleEvent = function (event) {
      this.allEvents.push(event);

      switch (event.eventPhase) {
        case event.CAPTURING_PHASE:
          this.capturedEvents.push(event);
          break;
        case event.AT_TARGET:
          this.atEvents.push(event);
          break;
        case event.BUBBLING_PHASE:
          this.bubbledEvents.push(event);
          break;
        default:
          throw new Error("Unspecified event phase");
      }
    }.bind(this);
  }
}

var _setUp = function() {
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
  var monitor = this.monitor = new EventMonitor();
  this.win = doc.defaultView;
  this.title = doc.getElementsByTagName("title").item(0);
  this.body = doc.getElementsByTagName("body").item(0);
  this.plist = doc.getElementsByTagName("p").item(0);
  this.event = doc.createEvent("Events");
  this._handleEvent = function(type) { return(function(event) { event[type](); monitor.handleEvent(event) }); }
}
var _tearDown = function(xs) {
  xs = ['monitor', 'title', 'body', 'plist', 'event', '_handleEvent'].concat(xs ? xs : []);
  var self = this;
  xs.forEach(function(x){
    self[x] = undefined;
    delete(self[x]);
  })
}

// A document is created using implementation.createDocument and cast to a DocumentEvent interface.
// @author <NAME>
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent
exports['DocumentEvent interface'] = function (test) {
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
  test.ok((doc.createEvent instanceof Function), "should have createEvent function");
  test.done();
}

// A document is created using implementation.createDocument and cast to a EventTarget interface.
// @author <NAME>
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget
exports['EventTarget interface'] = function (test) {
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
  test.ok((doc instanceof doc.defaultView.EventTarget), 'should be an instance of EventTarget');
  test.done();
}

// An object implementing the Event interface is created by using DocumentEvent.createEvent method with an eventType
// @author <NAME>
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent-createEvent
exports['create event with each event type'] = function(test){
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff(),
      event_types = {'Events': doc.defaultView.Event,
                     'MutationEvents': doc.defaultView.MutationEvent,
                     'UIEvents': doc.defaultView.UIEvent,
                     'MouseEvents': doc.defaultView.MouseEvent ,
                     'HTMLEvents': doc.defaultView.Event};
  test.expect(10);
  for (var type in event_types) {
    var event = doc.createEvent(type);
    test.notEqual(event, null, "should not be null for " + type);
    test.ok((event instanceof event_types[type]),"should be instanceof " + type);
  }
  test.done();
}

// @see https://dom.spec.whatwg.org/#interface-event
exports['event interface eventInit parameter'] = function (test) {
  var doc = jsdom.jsdom();

  testEventConstructor(doc.defaultView.Event);
  testEventConstructor(doc.defaultView.UIEvent);
  testEventConstructor(doc.defaultView.MouseEvent);
  testEventConstructor(doc.defaultView.MutationEvent);

  test.done();

  function testEventConstructor(Constructor) {
    test.throws(function () {
      new Constructor('myevent', 'not a dictionary');<commit_msg>Modify MutationEvent test to ensure constructor throws<commit_after>"use strict";
var testcase = require('nodeunit').testCase;
var jsdom = require("../..");

class EventMonitor {
  constructor() {
    this.atEvents = [];
    this.bubbledEvents = [];
    this.capturedEvents = [];
    this.allEvents = [];

    this.handleEvent = function (event) {
      this.allEvents.push(event);

      switch (event.eventPhase) {
        case event.CAPTURING_PHASE:
          this.capturedEvents.push(event);
          break;
        case event.AT_TARGET:
          this.atEvents.push(event);
          break;
        case event.BUBBLING_PHASE:
          this.bubbledEvents.push(event);
          break;
        default:
          throw new Error("Unspecified event phase");
      }
    }.bind(this);
  }
}

var _setUp = function() {
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
  var monitor = this.monitor = new EventMonitor();
  this.win = doc.defaultView;
  this.title = doc.getElementsByTagName("title").item(0);
  this.body = doc.getElementsByTagName("body").item(0);
  this.plist = doc.getElementsByTagName("p").item(0);
  this.event = doc.createEvent("Events");
  this._handleEvent = function(type) { return(function(event) { event[type](); monitor.handleEvent(event) }); }
}
var _tearDown = function(xs) {
  xs = ['monitor', 'title', 'body', 'plist', 'event', '_handleEvent'].concat(xs ? xs : []);
  var self = this;
  xs.forEach(function(x){
    self[x] = undefined;
    delete(self[x]);
  })
}

// A document is created using implementation.createDocument and cast to a DocumentEvent interface.
// @author <NAME>
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent
exports['DocumentEvent interface'] = function (test) {
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
  test.ok((doc.createEvent instanceof Function), "should have createEvent function");
  test.done();
}

// A document is created using implementation.createDocument and cast to a EventTarget interface.
// @author <NAME>
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget
exports['EventTarget interface'] = function (test) {
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff();
  test.ok((doc instanceof doc.defaultView.EventTarget), 'should be an instance of EventTarget');
  test.done();
}

// An object implementing the Event interface is created by using DocumentEvent.createEvent method with an eventType
// @author <NAME>
// @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-DocumentEvent-createEvent
exports['create event with each event type'] = function(test){
  var doc = require('../level1/core/files/hc_staff.xml').hc_staff(),
      event_types = {'Events': doc.defaultView.Event,
                     'MutationEvents': doc.defaultView.MutationEvent,
                     'UIEvents': doc.defaultView.UIEvent,
                     'MouseEvents': doc.defaultView.MouseEvent ,
                     'HTMLEvents': doc.defaultView.Event};
  test.expect(10);
  for (var type in event_types) {
    var event = doc.createEvent(type);
    test.notEqual(event, null, "should not be null for " + type);
    test.ok((event instanceof event_types[type]),"should be instanceof " + type);
  }
  test.done();
}

// @see https://dom.spec.whatwg.org/#interface-event
exports['event interface eventInit parameter'] = function (test) {
  var doc = jsdom.jsdom();

  testEventConstructor(doc.defaultView.Event);
  testEventConstructor(doc.defaultView.UIEvent);
  testEventConstructor(doc.defaultView.MouseEvent);
  test.throws(function () {
    new doc.defaultView.MutationEvent('myevent');
  }, TypeError);

  test.done();

  function testEventConstructor(Constructor) {
    test.throws(function () {
      new Constructor('myevent', 'not a dictionary');
{'fraction_non_alphanumeric': 0.14908226594480392, 'fraction_numerical': 0.003961441964875215, 'mean_word_length': 3.5244922341696534, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 8}, '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': '3789234', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2282, 'n_words': 480}