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