delegate.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var delegate = require('../src/delegate');
  2. var simulant = require('simulant');
  3. describe('delegate', function() {
  4. before(function() {
  5. var html = '<ul>' +
  6. '<li><a>Item 1</a></li>' +
  7. '<li><a>Item 2</a></li>' +
  8. '<li><a>Item 3</a></li>' +
  9. '<li><a>Item 4</a></li>' +
  10. '<li><a>Item 5</a></li>' +
  11. '</ul>';
  12. document.body.innerHTML += html;
  13. global.container = document.querySelector('ul');
  14. global.anchor = document.querySelector('a');
  15. global.spy = sinon.spy(global.container, 'removeEventListener');
  16. });
  17. after(function() {
  18. global.spy.restore();
  19. document.body.innerHTML = '';
  20. });
  21. it('should add an event listener', function(done) {
  22. delegate(global.container, 'a', 'click', function() {
  23. done();
  24. });
  25. simulant.fire(global.anchor, simulant('click'));
  26. });
  27. it('should remove an event listener', function() {
  28. var delegation = delegate(global.container, 'a', 'click', function() {});
  29. delegation.destroy();
  30. assert.ok(global.spy.calledOnce);
  31. });
  32. it('should use `document` if the element is unspecified', function(done) {
  33. delegate('a', 'click', function() {
  34. done();
  35. });
  36. simulant.fire(global.anchor, simulant('click'));
  37. });
  38. it('should remove an event listener the unspecified base (`document`)', function() {
  39. var delegation = delegate('a', 'click', function() {});
  40. var spy = sinon.spy(document, 'removeEventListener');
  41. delegation.destroy();
  42. assert.ok(spy.calledOnce);
  43. spy.restore();
  44. });
  45. it('should add event listeners to all the elements in a base selector', function() {
  46. var spy = sinon.spy();
  47. delegate('li', 'a', 'click', spy);
  48. var anchors = document.querySelectorAll('a');
  49. simulant.fire(anchors[0], simulant('click'));
  50. simulant.fire(anchors[1], simulant('click'));
  51. assert.ok(spy.calledTwice);
  52. });
  53. it('should remove the event listeners from all the elements in a base selector', function() {
  54. var items = document.querySelectorAll('li')
  55. var spies = Array.prototype.map.call(items, function (li) {
  56. return sinon.spy(li, 'removeEventListener');
  57. });
  58. var delegations = delegate('li', 'a', 'click', function() {});
  59. delegations.forEach(function (delegation) {
  60. delegation.destroy();
  61. });
  62. spies.every(function (spy) {
  63. var success = spy.calledOnce;
  64. spy.restore();
  65. return success;
  66. });
  67. });
  68. it('should add event listeners to all the elements in a base array', function() {
  69. var spy = sinon.spy();
  70. var items = document.querySelectorAll('li')
  71. delegate(items, 'a', 'click', spy);
  72. var anchors = document.querySelectorAll('a')
  73. simulant.fire(anchors[0], simulant('click'));
  74. simulant.fire(anchors[1], simulant('click'));
  75. assert.ok(spy.calledTwice);
  76. });
  77. it('should remove the event listeners from all the elements in a base array', function() {
  78. var items = document.querySelectorAll('li')
  79. var spies = Array.prototype.map.call(items, function (li) {
  80. return sinon.spy(li, 'removeEventListener');
  81. });
  82. var delegations = delegate(items, 'a', 'click', function() {});
  83. delegations.forEach(function (delegation) {
  84. delegation.destroy();
  85. });
  86. spies.every(function (spy) {
  87. var success = spy.calledOnce;
  88. spy.restore();
  89. return success;
  90. });
  91. });
  92. });