ElggLibTest.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. define(function(require) {
  2. var elgg = require('elgg');
  3. describe("Elgg", function() {
  4. it("gives access to window via elgg.global", function() {
  5. expect(elgg.global).toBe(window);
  6. });
  7. describe("elgg.parse_url()", function() {
  8. it("break urls down into component parts", function() {
  9. [
  10. ["http://www.elgg.org/test/", {'scheme': 'http', 'host': 'www.elgg.org', 'path': '/test/'}],
  11. ["https://www.elgg.org/test/", {'scheme': 'https', 'host': 'www.elgg.org', 'path': '/test/'}],
  12. ["ftp://www.elgg.org/test/", {'scheme': 'ftp', 'host': 'www.elgg.org', 'path': '/test/'}],
  13. ["http://elgg.org/test?val1=one&val2=two", {'scheme': 'http', 'host': 'elgg.org', 'path': '/test', 'query': 'val1=one&val2=two'}],
  14. ["http://elgg.org:8080/", {'scheme': 'http', 'host': 'elgg.org', 'port': '8080', 'path': '/'}],
  15. ["http://elgg.org/test#there", {'scheme': 'http', 'host': 'elgg.org', 'path': '/test', 'fragment': 'there'}],
  16. ["test?val=one", {'host': 'test', 'query': 'val=one'}],
  17. ["?val=one", {'query': 'val=one'}],
  18. ["mailto:joe@elgg.org", {'scheme': 'mailto', 'path': 'joe@elgg.org'}],
  19. ["javascript:load()", {'scheme': 'javascript', 'path': 'load()'}]
  20. ].forEach(function(args) {
  21. expect(elgg.parse_url(args[0])).toEqual(args[1]);
  22. });
  23. });
  24. });
  25. describe("elgg.assertTypeOf()", function() {
  26. it("is a noop when the value is of the given type", function() {
  27. expect(function() {
  28. elgg.assertTypeOf('string', '');
  29. elgg.assertTypeOf('object', {});
  30. elgg.assertTypeOf('boolean', true);
  31. elgg.assertTypeOf('boolean', false);
  32. elgg.assertTypeOf('undefined', undefined);
  33. elgg.assertTypeOf('number', 1);
  34. elgg.assertTypeOf('function', elgg.nullFunction);
  35. }).not.toThrow();
  36. });
  37. it("throws an exception when the value is not of the given type", function() {
  38. expect(function() { elgg.assertTypeOf('function', {}); }).toThrow();
  39. expect(function() { elgg.assertTypeOf('object', elgg.nullFunction); }).toThrow();
  40. });
  41. });
  42. describe("elgg.provide()", function() {
  43. it("generates a global namespace", function() {
  44. expect(window.foo).toBe(undefined);
  45. elgg.provide('foo.bar.baz');
  46. expect(window.foo.bar.baz).not.toBe(undefined);
  47. window.foo = undefined; // cleanup
  48. });
  49. it("plays nice with existing namespaces", function() {
  50. elgg.provide('foo.bar.baz');
  51. window.foo.bar.baz.oof = "test";
  52. elgg.provide('foo.bar.baz');
  53. expect(window.foo.bar.baz.oof).toBe("test");
  54. window.foo = undefined; // cleanup
  55. });
  56. });
  57. describe("elgg.require()", function() {
  58. it("is a noop if the namespace exists", function() {
  59. expect(function(){
  60. elgg.require('jQuery');
  61. elgg.require('elgg');
  62. elgg.require('elgg.config');
  63. elgg.require('elgg.security');
  64. }).not.toThrow();
  65. });
  66. it("throws an exception when then the namespace does not exist", function() {
  67. expect(function(){ elgg.require(''); }).toThrow();
  68. expect(function(){ elgg.require('garbage'); }).toThrow();
  69. expect(function(){ elgg.require('gar.ba.ge'); }).toThrow();
  70. });
  71. });
  72. describe("elgg.inherit()", function() {
  73. function Base() {}
  74. function Child() {}
  75. elgg.inherit(Child, Base);
  76. it("establishes an inheritance relationship between classes", function() {
  77. expect(new Child() instanceof Base).toBe(true);
  78. });
  79. it("preserves the constructor prototype property", function() {
  80. expect(Child.prototype.constructor).toBe(Child);
  81. });
  82. });
  83. describe("elgg.normalize_url()", function() {
  84. var wwwroot;
  85. beforeEach(function() {
  86. wwwroot = elgg.config.wwwroot;
  87. elgg.config.wwwroot = 'http://elgg.org/';
  88. });
  89. afterEach(function() {
  90. elgg.config.wwwroot = wwwroot;
  91. })
  92. it("prepends elgg.config.wwroot to relative URLs", function() {
  93. [
  94. ['', elgg.config.wwwroot],
  95. ['test', elgg.config.wwwroot + 'test'],
  96. ['mod/my_plugin/graphics/image.jpg', elgg.config.wwwroot + 'mod/my_plugin/graphics/image.jpg'],
  97. ['page/handler', elgg.config.wwwroot + 'page/handler'],
  98. ['page/handler?p=v&p2=v2', elgg.config.wwwroot + 'page/handler?p=v&p2=v2'],
  99. ['mod/plugin/file.php', elgg.config.wwwroot + 'mod/plugin/file.php'],
  100. ['mod/plugin/file.php?p=v&p2=v2', elgg.config.wwwroot + 'mod/plugin/file.php?p=v&p2=v2'],
  101. ['rootfile.php', elgg.config.wwwroot + 'rootfile.php'],
  102. ['rootfile.php?p=v&p2=v2', elgg.config.wwwroot + 'rootfile.php?p=v&p2=v2'],
  103. ['/page/handler', elgg.config.wwwroot + 'page/handler'],
  104. ['/page/handler?p=v&p2=v2', elgg.config.wwwroot + 'page/handler?p=v&p2=v2'],
  105. ['/mod/plugin/file.php', elgg.config.wwwroot + 'mod/plugin/file.php'],
  106. ['/mod/plugin/file.php?p=v&p2=v2', elgg.config.wwwroot + 'mod/plugin/file.php?p=v&p2=v2'],
  107. ['/rootfile.php', elgg.config.wwwroot + 'rootfile.php'],
  108. ['/rootfile.php?p=v&p2=v2', elgg.config.wwwroot + 'rootfile.php?p=v&p2=v2']
  109. ].forEach(function(args) {
  110. expect(elgg.normalize_url(args[0])).toBe(args[1]);
  111. });
  112. });
  113. it("leaves absolute and scheme-relative URLs alone", function() {
  114. [
  115. 'http://example.com',
  116. 'http://example-time.com',
  117. 'https://example.com',
  118. '//example.com',
  119. 'http://shouldn\'t matter what\'s here',
  120. 'http://elgg_private.srokap.c9.io/',
  121. 'https://shouldn\'t matter what\'s here',
  122. 'ftp://example.com/file',
  123. 'mailto:brett@elgg.org',
  124. 'javascript:alert("test")',
  125. 'app://endpoint',
  126. ].forEach(function(url) {
  127. expect(elgg.normalize_url(url)).toBe(url);
  128. });
  129. });
  130. it("prepends scheme to domains that lack it", function() {
  131. [
  132. ['example.com', 'http://example.com'],
  133. ['example.com/subpage', 'http://example.com/subpage'],
  134. ].forEach(function(args) {
  135. expect(elgg.normalize_url(args[0])).toBe(args[1]);
  136. });
  137. });
  138. });
  139. describe("elgg.parse_str()", function () {
  140. it("parses values like PHP's urldecode()", function () {
  141. [
  142. ["A+%2B+B=A+%2B+B", {"A + B": "A + B"}],
  143. ["key1=val1", {'key1': 'val1'}],
  144. ["key1=val1&key2=val2", {'key1': 'val1', 'key2': 'val2'}],
  145. ["key1[]=value1&key1[]=value2&key2=value3", {'key1': ['value1', 'value2'], 'key2': 'value3'}]
  146. ].forEach(function(args) {
  147. expect(elgg.parse_str(args[0])).toEqual(args[1]);
  148. });
  149. });
  150. });
  151. });
  152. });