Debian Codegraph @

delegate/test/delegate.js

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