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