test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var assert = require('assert');
  2. var CE = require('../');
  3. describe('CustomEvent', function () {
  4. describe('new CustomEvent()', function () {
  5. it('should create a `CustomEvent` instance', function () {
  6. var e = new CE('cat');
  7. assert.equal(e.type, 'cat');
  8. assert.equal(e.bubbles, false);
  9. assert.equal(e.cancelable, false);
  10. assert.equal(e.detail, undefined);
  11. });
  12. it('should create a `CustomEvent` instance with a `details` object', function () {
  13. var e = new CE('meow', { detail: { foo: 'bar' } });
  14. assert.equal(e.type, 'meow');
  15. assert.equal(e.bubbles, false);
  16. assert.equal(e.cancelable, false);
  17. assert.equal(e.detail.foo, 'bar');
  18. });
  19. it('should create a `CustomEvent` instance with a `bubbles` boolean', function () {
  20. var e = new CE('purr', { bubbles: true });
  21. assert.equal(e.type, 'purr');
  22. assert.equal(e.bubbles, true);
  23. assert.equal(e.cancelable, false);
  24. assert.equal(e.detail, undefined);
  25. });
  26. it('should create a `CustomEvent` instance with a `cancelable` boolean', function () {
  27. var e = new CE('scratch', { cancelable: true });
  28. assert.equal(e.type, 'scratch');
  29. assert.equal(e.bubbles, false);
  30. assert.equal(e.cancelable, true);
  31. assert.equal(e.detail, undefined);
  32. });
  33. it('should create a `CustomEvent` instance that is dispatchable', function (done) {
  34. var e = new CE('claw', {
  35. bubbles: true,
  36. cancelable: true,
  37. detail: { canhaz: 'cheeseburger' }
  38. });
  39. function onclaw (ev) {
  40. if (!ev) ev = window.event;
  41. assert.equal(e.bubbles, true);
  42. assert.equal(e.cancelable, true);
  43. assert.equal(e.detail.canhaz, 'cheeseburger');
  44. done();
  45. }
  46. if (document.body.dispatchEvent) {
  47. document.body.addEventListener('claw', onclaw, false);
  48. document.body.dispatchEvent(e);
  49. } else {
  50. // IE <= 8 will only allow us to fire "known" event names,
  51. // so we need to fire "click" instead of "claw :\
  52. document.body.attachEvent('onclick', onclaw);
  53. // need to fire event in a separate tick for some reason…
  54. setTimeout(function () {
  55. e.type = 'click';
  56. e.eventName = 'click';
  57. e.eventType = 'click';
  58. document.body.fireEvent('onclick', e);
  59. }, 50);
  60. }
  61. });
  62. });
  63. });