# Code coverage report for ./suites/dom/DomEventSpec.js

## Statements: 96.43% (54 / 56) Branches: 50% (3 / 6) Functions: 100% (16 / 16) Lines: 96.43% (54 / 56)

All files » ./suites/dom/ » DomEventSpec.js

    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
    1031
    1

    1
    6
    6
    6

    6

    1
    6
    6
    6
    6

    1
    6

    1
    1
    1

    1
    1

    1

    1
    1

    1
    1

    1
    1

    1

    1

    1
    1

    1
    1

    1

    1

    1
    1
    1

    1
    1

    1

    1

    1
    1
    1

    1

    1
    1

    1

    1

    1

    1
    1
    1

    1

     describe('DomEvent', function() {
    	var el;

    	function simulateClick(el) {
    		Eif (document.createEvent) {
    			var e = document.createEvent('MouseEvents');
    			e.initMouseEvent('click', true, true, window,
    					0, 0, 0, 0, 0, false, false, false, false, 0, null);
    			return el.dispatchEvent(e);
    		} else if (el.fireEvent) {
    			return el.fireEvent('onclick');
    		}
    	}

    	beforeEach(function() {
    		el = document.createElement('div');
    		el.style.position = 'absolute';
    		el.style.top = el.style.left = '-10000px';
    		document.body.appendChild(el);
    	});

    	afterEach(function() {
    		document.body.removeChild(el);
    	});

    	describe('#addListener', function() {
    		it('adds a listener and calls it on event', function() {
    			var listener1 = sinon.spy(),
    				listener2 = sinon.spy();

    			L.DomEvent.addListener(el, 'click', listener1);
    			L.DomEvent.addListener(el, 'click', listener2);

    			simulateClick(el);

    			expect(listener1.called).to.be.ok();
    			expect(listener2.called).to.be.ok();
    		});

    		it('binds "this" to the given context', function() {
    			var obj = {foo: 'bar'},
    				result;

    			L.DomEvent.addListener(el, 'click', function() {
    				result = this;
    			}, obj);

    			simulateClick(el);

    			expect(result).to.eql(obj);
    		});

    		it('passes an event object to the listener', function() {
    			var type;

    			L.DomEvent.addListener(el, 'click', function(e) {
    				type = e && e.type;
    			});
    			simulateClick(el);

    			expect(type).to.eql('click');
    		});
    	});

    	describe('#removeListener', function() {
    		it('removes a previously added listener', function() {
    			var listener = sinon.spy();

    			L.DomEvent.addListener(el, 'click', listener);
    			L.DomEvent.removeListener(el, 'click', listener);

    			simulateClick(el);

    			expect(listener.called).to.not.be.ok();
    		});
    	});

    	describe('#stopPropagation', function() {
    		it('stops propagation of the given event', function() {
    			var child = document.createElement('div'),
    				listener = sinon.spy();

    			el.appendChild(child);

    			L.DomEvent.addListener(child, 'click', L.DomEvent.stopPropagation);
    			L.DomEvent.addListener(el, 'click', listener);

    			simulateClick(child);

    			expect(listener.called).to.not.be.ok();

    			el.removeChild(child);
    		});
    	});
    	describe('#preventDefault', function() {
    		it('prevents the default action of event', function() {
    			L.DomEvent.addListener(el, 'click', L.DomEvent.preventDefault);

    			expect(simulateClick(el)).to.be(false);
    		});
    	});
    });