ef173ef93220b80d0a25233f41952fb6677f1b5046efff50a81f77fec1aa00687df559e36d716ad30c2cb59644985cf8c94dc7f9d02c7beaaf5f180b1b438d 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var listen = require('../src/listen');
  2. var simulant = require('simulant');
  3. describe('good-listener', function() {
  4. before(function() {
  5. global.node = document.createElement('div');
  6. global.node.setAttribute('id', 'foo');
  7. global.node.setAttribute('class', 'foo');
  8. document.body.appendChild(global.node);
  9. });
  10. after(function() {
  11. document.body.innerHTML = '';
  12. });
  13. describe('listen', function() {
  14. it('should throw an error since arguments were not passed', function(done) {
  15. try {
  16. listen();
  17. }
  18. catch(error) {
  19. assert.equal(error.message, 'Missing required arguments');
  20. done();
  21. }
  22. });
  23. it('should throw an error since "target" was invalid', function(done) {
  24. try {
  25. listen(null, 'click', function() {});
  26. }
  27. catch(error) {
  28. assert.equal(error.message, 'First argument must be a String, HTMLElement, HTMLCollection, or NodeList');
  29. done();
  30. }
  31. });
  32. it('should throw an error since "type" was invalid', function(done) {
  33. try {
  34. listen('.btn', false, function() {});
  35. }
  36. catch(error) {
  37. assert.equal(error.message, 'Second argument must be a String');
  38. done();
  39. }
  40. });
  41. it('should throw an error since "callback" was invalid', function(done) {
  42. try {
  43. listen('.btn', 'click', []);
  44. }
  45. catch(error) {
  46. assert.equal(error.message, 'Third argument must be a Function');
  47. done();
  48. }
  49. });
  50. });
  51. describe('listenNode', function() {
  52. before(function() {
  53. global.target = document.querySelector('#foo');
  54. global.spy = sinon.spy(global.target, 'removeEventListener');
  55. });
  56. after(function() {
  57. global.spy.restore();
  58. });
  59. it('should add an event listener', function(done) {
  60. listen(global.target, 'click', function() {
  61. done();
  62. });
  63. simulant.fire(global.target, simulant('click'));
  64. });
  65. it('should remove an event listener', function() {
  66. var listener = listen(global.target, 'click', function() {});
  67. listener.destroy();
  68. assert.ok(global.spy.calledOnce);
  69. });
  70. });
  71. describe('listenNodeList', function() {
  72. before(function() {
  73. global.targets = document.querySelectorAll('.foo');
  74. global.spy = sinon.spy(global.targets[0], 'removeEventListener');
  75. });
  76. after(function() {
  77. global.spy.restore();
  78. });
  79. it('should add an event listener', function(done) {
  80. listen(global.targets, 'click', function() {
  81. done();
  82. });
  83. simulant.fire(global.targets[0], simulant('click'));
  84. });
  85. it('should remove an event listener', function() {
  86. var listener = listen(global.targets, 'click', function() {});
  87. listener.destroy();
  88. assert.ok(global.spy.calledOnce);
  89. });
  90. });
  91. describe('listenSelector', function() {
  92. before(function() {
  93. global.target = document.querySelector('.foo');
  94. global.spy = sinon.spy(document.body, 'removeEventListener');
  95. });
  96. after(function() {
  97. global.spy.restore();
  98. });
  99. it('should add an event listener', function(done) {
  100. listen('.foo', 'click', function() {
  101. done();
  102. });
  103. simulant.fire(global.target, simulant('click'));
  104. });
  105. it('should remove an event listener', function() {
  106. var listener = listen('.foo', 'click', function() {});
  107. listener.destroy();
  108. assert.ok(global.spy.calledOnce);
  109. });
  110. });
  111. });