Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

138 lines (116 sloc) 3.868 kb
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="stylesheet" href="test.css">
<title>Zepto event tests</title>
<script src="../vendor/evidence.js"></script>
<script src="evidence_runner.js"></script>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<script src="../src/event.js"></script>
</head>
<body>
<h1>Zepto event tests</h1>
<p id="results">
Running… see browser console for results
</p>
<div id="fixtures">
</div><!-- fixtures -->
<script>
(function(){
function click(el){
var event = document.createEvent('MouseEvents')
event.initMouseEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null)
$(el).get(0).dispatchEvent(event)
}
Evidence('ProxyTest', {
testProxyFnContext: function(t){
var a = {name: 'A', fn: function(n, o){ return this.name + n + o }},
b = {name: 'B'}
t.assertEqual('A13', a.fn(1, 3))
t.assertEqual('B52', $.proxy(a.fn, b)(5, 2))
},
testProxyInvalidFn: function(t){
try {
$.proxy(null)
t.fail("shouldn't be here")
} catch(e) {
t.assertEqual('TypeError', e.name)
t.assertEqual("expected function", e.message)
}
},
testProxyContextName: function(t){
var b = {name: 'B', fn: function(n, o){ return this.name + n + o }},
oldFn = b.fn
t.assertEqual('B52', $.proxy(b, 'fn')(5, 2))
t.assertIdentical(oldFn, b.fn)
},
testProxyUndefinedProperty: function(t){
try {
$.proxy({}, 'nonexistent')
t.fail("shouldn't be here")
} catch(e) {
t.assertEqual('TypeError', e.name)
t.assertEqual("expected function", e.message)
}
},
testProxyInvalidProperty: function(t){
try {
$.proxy({num:3}, 'num')
t.fail("shouldn't be here")
} catch(e) {
t.assertEqual('TypeError', e.name)
t.assertEqual("expected function", e.message)
}
}
})
Evidence('EventTest', {
setUp: function(){
this.el = $('<div />').appendTo('#fixtures')
},
tearDown: function(){
this.el.off().remove()
$([document, document.body]).off()
},
testProxiedHandlerCanBeUnbindedWithOriginal: function(t){
var obj = {times:0, fn: function(){ this.times++ }}
this.el.on('click', $.proxy(obj, 'fn'))
click(this.el)
t.assertEqual(1, obj.times)
this.el.off('click', obj.fn)
click(this.el)
t.assertEqual(1, obj.times)
},
testOnWithObject: function(t){
var log = []
this.el.on({ click: function(){ log.push('a') } }).
on({ click: function(){ log.push('b') } }, null)
click(this.el)
t.assertEqual('a b', log.sort().join(' '))
},
testOnWithNullSelector: function(t){
var log = []
this.el.on('click', null, function(){ log.push('a') }).
on('click', undefined, function(){ log.push('b') })
click(this.el)
t.assertEqual('a b', log.sort().join(' '))
},
testOffWithObject: function(t){
var log = [],
fn = function(){ log.push('a') },
fn2 = function(){ log.push('b') },
fn3 = function(){ log.push('c') }
this.el.on('click', fn).on('click', fn2).on('click', fn3)
click(this.el)
t.assertEqual('a b c', log.sort().join(' '))
this.el.off({ click: fn }).off({ click: fn2 }, null)
click(this.el)
t.assertEqual('a b c c', log.sort().join(' '))
}
})
})()
</script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.