diff options
Diffstat (limited to 'js/tests')
-rw-r--r-- | js/tests/ElggAjaxOptionsTest.js | 61 | ||||
-rw-r--r-- | js/tests/ElggAjaxTest.js | 59 | ||||
-rw-r--r-- | js/tests/ElggHooksTest.js | 28 | ||||
-rw-r--r-- | js/tests/ElggLanguagesTest.js | 45 | ||||
-rw-r--r-- | js/tests/ElggLibTest.js | 129 | ||||
-rw-r--r-- | js/tests/ElggPriorityListTest.js | 47 | ||||
-rw-r--r-- | js/tests/ElggSecurityTest.js | 75 | ||||
-rw-r--r-- | js/tests/ElggSessionTest.js | 36 | ||||
-rw-r--r-- | js/tests/README | 24 | ||||
-rw-r--r-- | js/tests/jsTestDriver.conf | 10 |
10 files changed, 514 insertions, 0 deletions
diff --git a/js/tests/ElggAjaxOptionsTest.js b/js/tests/ElggAjaxOptionsTest.js new file mode 100644 index 000000000..a6b40d439 --- /dev/null +++ b/js/tests/ElggAjaxOptionsTest.js @@ -0,0 +1,61 @@ +/** + * Tests elgg.ajax.handleOptions() with all of the possible valid inputs + */ +ElggAjaxOptionsTest = TestCase("ElggAjaxOptionsTest"); + +ElggAjaxOptionsTest.prototype.testHandleOptionsAcceptsNoArgs = function() { + assertNotUndefined(elgg.ajax.handleOptions()); +}; + +ElggAjaxOptionsTest.prototype.testHandleOptionsAcceptsUrl = function() { + var url = 'url', + result = elgg.ajax.handleOptions(url); + + assertEquals(url, result.url); +}; + +ElggAjaxOptionsTest.prototype.testHandleOptionsAcceptsDataOnly = function() { + var options = {}, + result = elgg.ajax.handleOptions(options); + + assertEquals(options, result.data); +}; + +ElggAjaxOptionsTest.prototype.testHandleOptionsAcceptsOptions = function() { + var options = {data:{arg:1}}, + result = elgg.ajax.handleOptions(options); + + assertEquals(options, result); + + function func() {} + options = {success: func}; + result = elgg.ajax.handleOptions(options); + + assertEquals(options, result); +}; + +ElggAjaxOptionsTest.prototype.testHandleOptionsAcceptsUrlThenDataOnly = function() { + var url = 'url', + options = {arg:1}, + result = elgg.ajax.handleOptions(url, options); + + assertEquals(url, result.url); + assertEquals(options, result.data); +}; + +ElggAjaxOptionsTest.prototype.testHandleOptionsAcceptsUrlThenSuccessOnly = function() { + var url = 'url', + result = elgg.ajax.handleOptions(url, elgg.nullFunction); + + assertEquals(url, result.url); + assertEquals(elgg.nullFunction, result.success); +}; + +ElggAjaxOptionsTest.prototype.testHandleOptionsAcceptsUrlThenOptions = function() { + var url = 'url', + options = {data:{arg:1}}, + result = elgg.ajax.handleOptions(url, options); + + assertEquals(url, result.url); + assertEquals(options.data, result.data); +};
\ No newline at end of file diff --git a/js/tests/ElggAjaxTest.js b/js/tests/ElggAjaxTest.js new file mode 100644 index 000000000..a683415fc --- /dev/null +++ b/js/tests/ElggAjaxTest.js @@ -0,0 +1,59 @@ +/** + * Makes sure that each of the helper ajax functions ends up calling $.ajax + * with the right options. + */ +ElggAjaxTest = TestCase("ElggAjaxTest"); + +ElggAjaxTest.prototype.setUp = function() { + + this.wwwroot = elgg.config.wwwroot; + this.ajax = $.ajax; + + elgg.config.wwwroot = 'http://www.elgg.org/'; + + $.ajax = function(options) { + return options; + }; +}; + +ElggAjaxTest.prototype.tearDown = function() { + $.ajax = this.ajax; + elgg.config.wwwroot = this.wwwroot; +}; + +ElggAjaxTest.prototype.testElggAjax = function() { + assertEquals(elgg.config.wwwroot, elgg.ajax().url); +}; + +ElggAjaxTest.prototype.testElggGet = function() { + assertEquals('get', elgg.get().type); +}; + +ElggAjaxTest.prototype.testElggGetJSON = function() { + assertEquals('json', elgg.getJSON().dataType); +}; + +ElggAjaxTest.prototype.testElggPost = function() { + assertEquals('post', elgg.post().type); +}; + +ElggAjaxTest.prototype.testElggAction = function() { + assertException(function() { elgg.action(); }); + assertException(function() { elgg.action({}); }); + + var result = elgg.action('action'); + assertEquals('post', result.type); + assertEquals('json', result.dataType); + assertEquals(elgg.config.wwwroot + 'action/action', result.url); + assertEquals(elgg.security.token.__elgg_ts, result.data.__elgg_ts); +}; + +ElggAjaxTest.prototype.testElggAPI = function() { + assertException(function() { elgg.api(); }); + assertException(function() { elgg.api({}); }); + + var result = elgg.api('method'); + assertEquals('json', result.dataType); + assertEquals('method', result.data.method); + assertEquals(elgg.config.wwwroot + 'services/api/rest/json/', result.url); +}; diff --git a/js/tests/ElggHooksTest.js b/js/tests/ElggHooksTest.js new file mode 100644 index 000000000..e7a2440e7 --- /dev/null +++ b/js/tests/ElggHooksTest.js @@ -0,0 +1,28 @@ +ElggHooksTest = TestCase("ElggHooksTest"); + +ElggHooksTest.prototype.setUp = function() { + elgg.config.hooks = {}; + elgg.provide('elgg.config.hooks.all.all'); +}; + +ElggHooksTest.prototype.testHookHandlersMustBeFunctions = function () { + assertException(function() { elgg.register_hook_handler('str', 'str', 'oops'); }); +}; + +ElggHooksTest.prototype.testReturnValueDefaultsToTrue = function () { + assertTrue(elgg.trigger_hook('fee', 'fum')); + + elgg.register_hook_handler('fee', 'fum', elgg.nullFunction); + assertTrue(elgg.trigger_hook('fee', 'fum')); +}; + +ElggHooksTest.prototype.testCanGlomHooksWithAll = function () { + elgg.register_hook_handler('all', 'bar', elgg.abstractMethod); + assertException("all,bar", function() { elgg.trigger_hook('foo', 'bar'); }); + + elgg.register_hook_handler('foo', 'all', elgg.abstractMethod); + assertException("foo,all", function() { elgg.trigger_hook('foo', 'baz'); }); + + elgg.register_hook_handler('all', 'all', elgg.abstractMethod); + assertException("all,all", function() { elgg.trigger_hook('pinky', 'winky'); }); +};
\ No newline at end of file diff --git a/js/tests/ElggLanguagesTest.js b/js/tests/ElggLanguagesTest.js new file mode 100644 index 000000000..9186ff5bb --- /dev/null +++ b/js/tests/ElggLanguagesTest.js @@ -0,0 +1,45 @@ +ElggLanguagesTest = TestCase("ElggLanguagesTest"); + +ElggLanguagesTest.prototype.setUp = function() { + this.ajax = $.ajax; + + //Immediately execute some dummy "returned" javascript instead of sending + //an actual ajax request + $.ajax = function(settings) { + var lang = settings.data.language; + elgg.config.translations[lang] = {'language':lang}; + }; +}; + +ElggLanguagesTest.prototype.tearDown = function() { + $.ajax = this.ajax; + + //clear translations + elgg.config.translations['en'] = undefined; + elgg.config.translations['aa'] = undefined; +}; + +ElggLanguagesTest.prototype.testLoadTranslations = function() { + assertUndefined(elgg.config.translations['en']); + assertUndefined(elgg.config.translations['aa']); + + elgg.reload_all_translations(); + elgg.reload_all_translations('aa'); + + assertNotUndefined(elgg.config.translations['en']['language']); + assertNotUndefined(elgg.config.translations['aa']['language']); +}; + +ElggLanguagesTest.prototype.testElggEchoTranslates = function() { + elgg.reload_all_translations('en'); + elgg.reload_all_translations('aa'); + + assertEquals('en', elgg.echo('language')); + assertEquals('aa', elgg.echo('language', 'aa')); +}; + +ElggLanguagesTest.prototype.testElggEchoFallsBackToDefaultLanguage = function() { + elgg.reload_all_translations('en'); + assertEquals('en', elgg.echo('language', 'aa')); +}; + diff --git a/js/tests/ElggLibTest.js b/js/tests/ElggLibTest.js new file mode 100644 index 000000000..2a676e22a --- /dev/null +++ b/js/tests/ElggLibTest.js @@ -0,0 +1,129 @@ +/** + * Test basic elgg library functions + */ +ElggLibTest = TestCase("ElggLibTest"); + +ElggLibTest.prototype.testGlobal = function() { + assertTrue(window === elgg.global); +}; + +ElggLibTest.prototype.testAssertTypeOf = function() { + [//Valid inputs + ['string', ''], + ['object', {}], + ['boolean', true], + ['boolean', false], + ['undefined', undefined], + ['number', 0], + ['function', elgg.nullFunction] + ].forEach(function(args) { + assertNoException(function() { + elgg.assertTypeOf.apply(undefined, args); + }); + }); + + [//Invalid inputs + ['function', {}], + ['object', elgg.nullFunction] + ].forEach(function() { + assertException(function(args) { + elgg.assertTypeOf.apply(undefined, args); + }); + }); +}; + +ElggLibTest.prototype.testProvideDoesntClobber = function() { + elgg.provide('foo.bar.baz'); + + foo.bar.baz.oof = "test"; + + elgg.provide('foo.bar.baz'); + + assertEquals("test", foo.bar.baz.oof); +}; + +/** + * Try requiring bogus input + */ +ElggLibTest.prototype.testRequire = function () { + assertException(function(){ elgg.require(''); }); + assertException(function(){ elgg.require('garbage'); }); + assertException(function(){ elgg.require('gar.ba.ge'); }); + + assertNoException(function(){ + elgg.require('jQuery'); + elgg.require('elgg'); + elgg.require('elgg.config'); + elgg.require('elgg.security'); + }); +}; + +ElggLibTest.prototype.testInherit = function () { + function Base() {} + function Child() {} + + elgg.inherit(Child, Base); + + assertInstanceOf(Base, new Child()); + assertEquals(Child, Child.prototype.constructor); +}; + +ElggLibTest.prototype.testNormalizeUrl = function() { + elgg.config.wwwroot = "http://elgg.org/"; + + [ + ['', elgg.config.wwwroot], + ['test', elgg.config.wwwroot + 'test'], + ['http://example.com', 'http://example.com'], + ['https://example.com', 'https://example.com'], + ['http://example-time.com', 'http://example-time.com'], + ['//example.com', '//example.com'], + + ['ftp://example.com/file', 'ftp://example.com/file'], + ['mailto:brett@elgg.org', 'mailto:brett@elgg.org'], + ['javascript:alert("test")', 'javascript:alert("test")'], + ['app://endpoint', 'app://endpoint'], + + ['example.com', 'http://example.com'], + ['example.com/subpage', 'http://example.com/subpage'], + + ['page/handler', elgg.config.wwwroot + 'page/handler'], + ['page/handler?p=v&p2=v2', elgg.config.wwwroot + 'page/handler?p=v&p2=v2'], + ['mod/plugin/file.php', elgg.config.wwwroot + 'mod/plugin/file.php'], + ['mod/plugin/file.php?p=v&p2=v2', elgg.config.wwwroot + 'mod/plugin/file.php?p=v&p2=v2'], + ['rootfile.php', elgg.config.wwwroot + 'rootfile.php'], + ['rootfile.php?p=v&p2=v2', elgg.config.wwwroot + 'rootfile.php?p=v&p2=v2'], + + ['/page/handler', elgg.config.wwwroot + 'page/handler'], + ['/page/handler?p=v&p2=v2', elgg.config.wwwroot + 'page/handler?p=v&p2=v2'], + ['/mod/plugin/file.php', elgg.config.wwwroot + 'mod/plugin/file.php'], + ['/mod/plugin/file.php?p=v&p2=v2', elgg.config.wwwroot + 'mod/plugin/file.php?p=v&p2=v2'], + ['/rootfile.php', elgg.config.wwwroot + 'rootfile.php'], + ['/rootfile.php?p=v&p2=v2', elgg.config.wwwroot + 'rootfile.php?p=v&p2=v2'] + + ].forEach(function(args) { + assertEquals(args[1], elgg.normalize_url(args[0])); + }); +}; + +ElggLibTest.prototype.testParseUrl = function() { + + [ + ["http://www.elgg.org/test/", {'scheme': 'http', 'host': 'www.elgg.org', 'path': '/test/'}], + ["https://www.elgg.org/test/", {'scheme': 'https', 'host': 'www.elgg.org', 'path': '/test/'}], + ["ftp://www.elgg.org/test/", {'scheme': 'ftp', 'host': 'www.elgg.org', 'path': '/test/'}], + ["http://elgg.org/test?val1=one&val2=two", {'scheme': 'http', 'host': 'elgg.org', 'path': '/test', 'query': 'val1=one&val2=two'}], + ["http://elgg.org:8080/", {'scheme': 'http', 'host': 'elgg.org', 'port': 8080, 'path': '/'}], + ["http://elgg.org/test#there", {'scheme': 'http', 'host': 'elgg.org', 'path': '/test', 'fragment': 'there'}], + + ["test?val=one", {'host': 'test', 'query': 'val=one'}], + ["?val=one", {'query': 'val=one'}], + + ["mailto:joe@elgg.org", {'scheme': 'mailto', 'path': 'joe@elgg.org'}], + ["javascript:load()", {'scheme': 'javascript', 'path': 'load()'}] + + ].forEach(function(args) { + assertEquals(args[1], elgg.parse_url(args[0])); + }); +}; + diff --git a/js/tests/ElggPriorityListTest.js b/js/tests/ElggPriorityListTest.js new file mode 100644 index 000000000..2329a8490 --- /dev/null +++ b/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('bar', 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 diff --git a/js/tests/ElggSecurityTest.js b/js/tests/ElggSecurityTest.js new file mode 100644 index 000000000..107c0adbd --- /dev/null +++ b/js/tests/ElggSecurityTest.js @@ -0,0 +1,75 @@ +ElggSecurityTest = TestCase("ElggSecurityTest"); + +ElggSecurityTest.prototype.setUp = function() { + //fill with fake, but reasonable, values for testing + this.ts = elgg.security.token.__elgg_ts = 12345; + this.token = elgg.security.token.__elgg_token = 'abcdef'; +}; + +ElggSecurityTest.prototype.testAddTokenAcceptsUndefined = function() { + var input, + expected = { + __elgg_ts: this.ts, + __elgg_token: this.token + }; + + assertEquals(expected, elgg.security.addToken(input)); +}; + +ElggSecurityTest.prototype.testAddTokenAcceptsObject = function() { + var input = {}, + expected = { + __elgg_ts: this.ts, + __elgg_token: this.token + }; + + assertEquals(expected, elgg.security.addToken(input)); +}; + +ElggSecurityTest.prototype.testAddTokenAcceptsRelativeUrl = function() { + var input, + str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token; + + input = "test"; + assertEquals(input + '?' + str, elgg.security.addToken(input)); +}; + +ElggSecurityTest.prototype.testAddTokenAcceptsFullUrl = function() { + var input, + str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token; + + input = "http://elgg.org/"; + assertEquals(input + '?' + str, elgg.security.addToken(input)); +}; + +ElggSecurityTest.prototype.testAddTokenAcceptsQueryString = function() { + var input, + str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token; + + input = "?data=sofar"; + assertEquals(input + '&' + str, elgg.security.addToken(input)); + + input = "test?data=sofar"; + assertEquals(input + '&' + str, elgg.security.addToken(input)); + + input = "http://elgg.org/?data=sofar"; + assertEquals(input + '&' + str, elgg.security.addToken(input)); +}; + +ElggSecurityTest.prototype.testAddTokenAlreadyAdded = function() { + var input, + str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token; + + input = "http://elgg.org/?" + str + "&data=sofar"; + assertEquals(input, elgg.security.addToken(input)); +}; + +ElggSecurityTest.prototype.testSetTokenSetsElggSecurityToken = function() { + var json = { + __elgg_ts: 4567, + __elgg_token: 'abcdef' + }; + + elgg.security.setToken(json); + assertEquals(json, elgg.security.token); +}; diff --git a/js/tests/ElggSessionTest.js b/js/tests/ElggSessionTest.js new file mode 100644 index 000000000..5ff8ca13e --- /dev/null +++ b/js/tests/ElggSessionTest.js @@ -0,0 +1,36 @@ +ElggSessionTest = TestCase("ElggSessionTest"); + +ElggSessionTest.prototype.testGetCookie = function() { + assertEquals(document.cookie, elgg.session.cookie()); +}; + +ElggSessionTest.prototype.testGetCookieKey = function() { + document.cookie = "name=value"; + assertEquals('value', elgg.session.cookie('name')); + + document.cookie = "name=value2"; + assertEquals('value2', elgg.session.cookie('name')); + + document.cookie = "name=value"; + document.cookie = "name2=value2"; + assertEquals('value', elgg.session.cookie('name')); + assertEquals('value2', elgg.session.cookie('name2')); +}; + +ElggSessionTest.prototype.testSetCookieKey = function() { + elgg.session.cookie('name', 'value'); + assertEquals('value', elgg.session.cookie('name')); + + elgg.session.cookie('name', 'value2'); + assertEquals('value2', elgg.session.cookie('name')); + + elgg.session.cookie('name', 'value'); + elgg.session.cookie('name2', 'value2'); + assertEquals('value', elgg.session.cookie('name')); + assertEquals('value2', elgg.session.cookie('name2')); + + elgg.session.cookie('name', null); + elgg.session.cookie('name2', null); + assertUndefined(elgg.session.cookie('name')); + assertUndefined(elgg.session.cookie('name2')); +};
\ No newline at end of file diff --git a/js/tests/README b/js/tests/README new file mode 100644 index 000000000..4f86b27c6 --- /dev/null +++ b/js/tests/README @@ -0,0 +1,24 @@ +Elgg JavaScript Unit Tests +-------------------------- + +Introduction +============ +Elgg uses js-test-driver to run its unit tests. Instructions on obtaining, +configuring, and using it are at http://code.google.com/p/js-test-driver/. It +supports running the test in multiple browsers and debugging using web browser +based debuggers. Visit its wiki at the Google Code site for more information. + + +Sample Usage +============ + 1. Put jar file in the base directory of Elgg + 2. Run the server: java -jar JsTestDriver-1.3.3d.jar --port 4224 + 3. Point a web browser at http://localhost:4224 + 4. Run the tests: java -jar JsTestDriver-1.3.3d.jar --config js/tests/jsTestDriver.conf --basePath . --tests all + + +Configuration Hints +=================== + * The port when running the server must be the same as listed in the + configuration file. If that port is being used, change the configuration file. + * The basePath must be the base directory of Elgg.
\ No newline at end of file diff --git a/js/tests/jsTestDriver.conf b/js/tests/jsTestDriver.conf new file mode 100644 index 000000000..cc0b5d373 --- /dev/null +++ b/js/tests/jsTestDriver.conf @@ -0,0 +1,10 @@ +server: http://localhost:4224 + +load: + - vendors/jquery/jquery-1.6.4.min.js + - vendors/sprintf.js + - js/lib/elgglib.js + - js/lib/hooks.js + - js/classes/*.js + - js/lib/*.js + - js/tests/*.js
\ No newline at end of file |