Browse Source

added some tests and moved component factory to extra service

Rafał Pitoń 9 years ago
parent
commit
64101935db

+ 1 - 11
misago/frontend/misago/app.js

@@ -54,6 +54,7 @@
 
     this.registerCoreServices = function() {
       this.addService('conf', ns.Conf);
+      this.addService('component', ns.ComponentFactory);
       this.addService('router', ns.RouterFactory);
       this.addService('api', ns.Api);
       this.addService('outlet', ns.Outlet);
@@ -61,17 +62,6 @@
       this.addService('start-routing', ns.startRouting);
     };
 
-    // Component factory
-    this.component = function() {
-      var arguments_array = [];
-      for (var i = 0; i < arguments.length; i += 1) {
-        arguments_array.push(arguments[i]);
-      }
-
-      arguments_array.push(this);
-      return m.component.apply(undefined, arguments_array);
-    };
-
     // App init/destory
     this.setup = false;
     this.init = function(setup) {

+ 16 - 0
misago/frontend/misago/services/component.js

@@ -0,0 +1,16 @@
+(function (ns) {
+  'use strict';
+
+  ns.ComponentFactory = function(_) {
+    // Component factory
+    _.component = function() {
+      var arguments_array = [];
+      for (var i = 0; i < arguments.length; i += 1) {
+        arguments_array.push(arguments[i]);
+      }
+
+      arguments_array.push(_);
+      return m.component.apply(undefined, arguments_array);
+    };
+  };
+}(Misago.prototype));

+ 2 - 2
misago/frontend/misago/services/router.js

@@ -104,8 +104,8 @@
       };
     };
 
-    this.staticUrl = prefixUrl(ns.get(_.preloaded_data, 'STATIC_URL', '/'));
-    this.mediaUrl = prefixUrl(ns.get(_.preloaded_data, 'MEDIA_URL', '/'));
+    this.staticUrl = prefixUrl(this.static_url);
+    this.mediaUrl = prefixUrl(this.media_url);
   };
 
   ns.RouterFactory = function(_) {

+ 1 - 0
misago/frontend/test/index.html

@@ -5,6 +5,7 @@
   <title>Misago QUnit</title>
   <link rel="stylesheet" href="//code.jquery.com/qunit/qunit-1.18.0.css">
   <link rel="stylesheet" href="/dist/css/misago.css">
+  <base href="/">
 </head>
 <body>
   <div id="qunit"></div>

+ 12 - 0
misago/frontend/test/tests/unit/component.js

@@ -0,0 +1,12 @@
+(function (ns) {
+  'use strict';
+
+  QUnit.module("Component factory");
+
+  QUnit.test("service factory registers component() function on container", function(assert) {
+    var container = {};
+    ns.ComponentFactory(container);
+
+    assert.ok(container.component, "service has set component factory on container.");
+  });
+}(Misago.prototype));

+ 54 - 0
misago/frontend/test/tests/unit/router.js

@@ -0,0 +1,54 @@
+(function (ns) {
+  'use strict';
+
+  QUnit.module("Router");
+
+  QUnit.test("cleanUrl cleans url's that should be routed", function(assert) {
+    var container = {
+      preloaded_data: {
+        'STATIC_URL': '/static/',
+        'MEDIA_URL': 'http://nocookie.somewhere.com/'
+      }
+    };
+
+    var router = new ns.RouterFactory(container);
+
+    assert.equal(router.cleanUrl('/'), '/');
+    assert.equal(router.cleanUrl('/lorem-ipsum/'), '/lorem-ipsum/');
+    assert.equal(router.cleanUrl('/lorem-ipsum/dolor/'), '/lorem-ipsum/dolor/');
+
+    assert.equal(router.cleanUrl('/static/'), undefined);
+    assert.equal(router.cleanUrl('http://nocookie.somewhere.com/'), undefined);
+    assert.equal(router.cleanUrl('/static/test.png'), undefined);
+    assert.equal(router.cleanUrl('http://nocookie.somewhere.com/test.png'), undefined);
+
+    router.base_url = '/misago/';
+
+    assert.equal(router.cleanUrl('/misago/'), '/misago/');
+    assert.equal(router.cleanUrl('/misago/lorem-ipsum/'), '/misago/lorem-ipsum/');
+    assert.equal(router.cleanUrl('/misago/lorem-ipsum/dolor/'), '/misago/lorem-ipsum/dolor/');
+
+    assert.equal(router.cleanUrl('/'), undefined);
+    assert.equal(router.cleanUrl('/lorem-ipsum/'), undefined);
+    assert.equal(router.cleanUrl('/lorem-ipsum/dolor/'), undefined);
+
+    assert.equal(router.cleanUrl('/misago/static/'), undefined);
+    assert.equal(router.cleanUrl('http://nocookie.somewhere.com/'), undefined);
+    assert.equal(router.cleanUrl('/misago/static/test.png'), undefined);
+    assert.equal(router.cleanUrl('http://nocookie.somewhere.com/test.png'), undefined);
+  });
+
+  QUnit.test("staticUrl and mediaUrl prefix paths", function(assert) {
+    var container = {
+      preloaded_data: {
+        'STATIC_URL': '/static/',
+        'MEDIA_URL': 'http://nocookie.somewhere.com/'
+      }
+    };
+
+    var router = new ns.RouterFactory(container);
+
+    assert.equal(router.staticUrl('logo.png'), '/static/logo.png', 'staticUrl correctly prefixed url to static asset.');
+    assert.equal(router.mediaUrl('avatar_1.png'), 'http://nocookie.somewhere.com/avatar_1.png', 'mediaUrl correctly prefixed url to media asset.');
+  });
+}(Misago.prototype));