From f7ae28ea45656d2821262998e9a71c351dcced8c Mon Sep 17 00:00:00 2001 From: ewinslow Date: Tue, 2 Nov 2010 04:23:04 +0000 Subject: Refs #2538: Added Elggy event system. Javascript boot sequence mimics PHP. git-svn-id: http://code.elgg.org/elgg/trunk@7186 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/js/tests/ElggEventsTest.js | 28 ++++++++++++++++++++ engine/js/tests/ElggLibTest.js | 39 ++++++++++++++++++++------- engine/js/tests/ElggPriorityListTest.js | 47 +++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 engine/js/tests/ElggEventsTest.js create mode 100644 engine/js/tests/ElggPriorityListTest.js (limited to 'engine/js/tests') diff --git a/engine/js/tests/ElggEventsTest.js b/engine/js/tests/ElggEventsTest.js new file mode 100644 index 000000000..cc30e8418 --- /dev/null +++ b/engine/js/tests/ElggEventsTest.js @@ -0,0 +1,28 @@ +ElggEventsTest = TestCase("ElggEventsTest"); + +ElggEventsTest.prototype.setUp = function() { + elgg.config.events = {}; + elgg.provide('elgg.config.events.all.all'); +}; + +ElggEventsTest.prototype.testEventHandlersMustBeFunctions = function() { + assertException(function() { elgg.register_event_handler('str', 'str', 'oops'); }); +}; + +ElggEventsTest.prototype.testReturnValueDefaultsToTrue = function() { + assertTrue(elgg.trigger_event('fee', 'fum')); + + elgg.register_event_handler('fee', 'fum', function() {}); + assertTrue(elgg.trigger_event('fee', 'fum')); +}; + +ElggEventsTest.prototype.testCanGlomEventsWithAll = function() { + elgg.register_event_handler('all', 'bar', function() { throw new Error(); }); + assertException("all,bar", function() { elgg.trigger_event('foo', 'bar'); }); + + elgg.register_event_handler('foo', 'all', function() { throw new Error(); }); + assertException("foo,all", function() { elgg.trigger_event('foo', 'baz'); }); + + elgg.register_event_handler('all', 'all', function() { throw new Error(); }); + assertException("all,all", function() { elgg.trigger_event('pinky', 'winky'); }); +}; \ No newline at end of file diff --git a/engine/js/tests/ElggLibTest.js b/engine/js/tests/ElggLibTest.js index 920296408..ed4db24e1 100644 --- a/engine/js/tests/ElggLibTest.js +++ b/engine/js/tests/ElggLibTest.js @@ -7,6 +7,35 @@ ElggLibTest.prototype.testGlobal = function() { assertTrue(window === elgg.global); }; +ElggLibTest.prototype.testAssertTypeOf = function() { + var noexceptions = [ + ['string', ''], + ['object', {}], + ['boolean', true], + ['boolean', false], + ['undefined', undefined], + ['number', 0], + ['function', function() {}], + ]; + + for (var i in noexceptions) { + assertNoException(function() { + elgg.assertTypeOf.apply(elgg, noexceptions[i]); + }); + } + + var exceptions = [ + ['function', {}], + ['object', function() {}], + ]; + + for (var i in exceptions) { + assertException(function() { + elgg.assertTypeOf.apply(elgg, exceptions[i]); + }); + } +}; + ElggLibTest.prototype.testProvide = function() { elgg.provide('foo.bar.baz'); @@ -39,7 +68,6 @@ ElggLibTest.prototype.testInherit = function() { elgg.inherit(Child, Base); - assertInstanceOf(Base, new Child()); assertEquals(Child, Child.prototype.constructor); }; @@ -53,11 +81,4 @@ ElggLibTest.prototype.testExtendUrl = function() { url = 'pg/test'; assertEquals('http://www.elgg.org/pg/test', elgg.extendUrl(url)); -}; - - - - - - - +}; \ No newline at end of file diff --git a/engine/js/tests/ElggPriorityListTest.js b/engine/js/tests/ElggPriorityListTest.js new file mode 100644 index 000000000..2549e0ee0 --- /dev/null +++ b/engine/js/tests/ElggPriorityListTest.js @@ -0,0 +1,47 @@ +ElggPriorityListTest = TestCase("ElggPriorityListTest"); + +ElggPriorityListTest.prototype.setUp = function() { + this.list = new elgg.ElggPriorityList(); +}; + +ElggPriorityListTest.prototype.tearDown = function() { + this.list = null; +}; + +ElggPriorityListTest.prototype.testInsert = function() { + this.list.insert('foo'); + + assertEquals('foo', this.list.priorities_[500][0]); + + this.list.insert('bar', 501); + + assertEquals('foo', this.list.priorities_[501][0]); +}; + +ElggPriorityListTest.prototype.testInsertRespectsPriority = function() { + var values = [5, 4, 3, 2, 1, 0]; + + for (var i in values) { + this.list.insert(values[i], values[i]); + } + + this.list.forEach(function(elem, idx)) { + assertEquals(elem, idx); + } +}; + +ElggPriorityListTest.prototype.testInsertHandlesDuplicatePriorities = function() { + values = [0, 1, 2, 3, 4, 5, 6, 7, 8 , 9]; + + for (var i in values) { + this.list.insert(values[i], values[i]/3); + } + + this.list.forEach(function(elem, idx) { + assertEquals(elem, idx); + }); +}; + +ElggPriorityListTest.prototype.testEveryDefaultsToTrue = function() { + assertTrue(this.list.every(function() {})); +}; \ No newline at end of file -- cgit v1.2.3