Permalink
Find file
add9d35 May 23, 2008
107 lines (90 sloc) 3.95 KB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>helpers.js test file</title>
<link rel="stylesheet" href="../lib/test.css" type="text/css" />
<script src="../lib/prototype.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<script src="../function.extensions.js" type="text/javascript"></script>
</head>
<body>
<div id="testlog"></div>
<div id="dummy">foo</div>
<div id="runOnceDummy"></div>
<div id="addAdviceDummy"></div>
<script type="text/javascript">
new Test.Unit.Runner({
testFunctionPrototypeNot: function() {
var truthy = function() { return true; };
var falsy = function() { return false; };
var something = function() { return {foo: 'bar'} };
this.assert(truthy());
this.assert(!truthy.not()());
this.assert(!falsy());
this.assert(falsy.not()());
},
testFunctionPrototypeRunOnce: function() {
function updater(){
$('runOnceDummy').update('foo');
};
this.assertIdentical('', $('runOnceDummy').innerHTML); // contents should be empty
this.assertIdentical('function', typeof updater.runOnce()); // should return function
this.assertIdentical('foo', $('runOnceDummy').innerHTML); // should update content when run
},
testFunctionPrototype_New: function() {
var Person = Class.create({
initialize: function(name) { this.name = name },
speak: function(message) { return this.name + ': ' + message }
})
var jd = Person._new('John');
this.assert(jd instanceof Person);
this.assertIdentical('John', jd.name);
this.assertIdentical('John: hi there', jd.speak('hi there'));
},
testFunctionPrototypeToDelayed: function() {
var delayed = (function(){
$('dummy').addClassName('foo');
}).toDelayed(1);
delayed();
this.assert(!$('dummy').hasClassName('foo'));
this.wait(1000, (function(){
this.assert($('dummy').hasClassName('foo'));
}).bind(this))
},
testFunctionK: function() {
this.assert(Function.K(true)());
this.assert(!Function.K(false)());
this.assertEqual('foo', Function.K('foo')());
var source = { foo: 'bar', baz: 'qux' }, dest = { };
// "prop" variable should NOT be shared via closure
for (var prop in source) {
dest[prop] = Function.K(prop);
}
this.assertEqual('foo', dest.foo());
this.assertEqual('baz', dest.baz());
},
testFunctionPrototypeAddAdvice: function() {
Element.addMethods({
addClassName: Element.Methods.addClassName.addAdvice({
before: function(element, className) {
// when passed 'draggable' to addClassName, set element's position to 'absolute'
if (className == 'draggable')
$(element).absolutize();
}
})
});
// should not be "absolute" initially
this.assertNotEqual('absolute', $('addAdviceDummy').getStyle('position'));
Element.addClassName('addAdviceDummy', 'foo');
// should not be "absolute" after assigning "foo" className
this.assertNotEqual('absolute', $('addAdviceDummy').getStyle('position'));
Element.addClassName('addAdviceDummy', 'draggable');
// should be "absolute" after assigning "draggable" className
this.assertEqual('absolute', $('addAdviceDummy').getStyle('position'));
}
})
</script>
</body>
</html>