Browse Source

service container unit tests

Rafał Pitoń 9 years ago
parent
commit
473b283b80
2 changed files with 119 additions and 6 deletions
  1. 22 2
      misago/frontend/misago/app.js
  2. 97 4
      misago/frontend/test/tests/unit/service-container.js

+ 22 - 2
misago/frontend/misago/app.js

@@ -5,7 +5,7 @@
 
   window.Misago = function() {
 
-    var ns = this.prototype;
+    var ns = Object.getPrototypeOf(this);
     var self = this;
 
     // Preloaded data
@@ -28,13 +28,30 @@
     this._initServices = function(services) {
       var ordered_services = new ns.OrderedList(services).order(false);
       ordered_services.forEach(function (item) {
-        var service_instance = item.item(self);
+        var factory = null;
+        if (item.item.factory !== undefined) {
+          factory = item.item.factory;
+        } else {
+          factory = item.item;
+        }
+
+        var service_instance = factory(self);
         if (service_instance) {
           self[item.name] = service_instance;
         }
       });
     };
 
+    this._destroyServices = function(services) {
+      var ordered_services = new ns.OrderedList(services).order();
+      ordered_services.reverse();
+      ordered_services.forEach(function (item) {
+        if (item.destroy !== undefined) {
+          item.destroy(self);
+        }
+      });
+    };
+
     this.registerCoreServices = function() {
       this.addService('Conf', ns.Conf);
     };
@@ -64,6 +81,9 @@
     };
 
     this.destroy = function() {
+      // run destructors for services
+      this._destroyServices()
+
       // unmount components if they are mounted
       if (this._outlet) {
         m.mount(this._outlet, null);

+ 97 - 4
misago/frontend/test/tests/unit/service-container.js

@@ -3,10 +3,6 @@
 
   var container = null;
 
-  var MockServiceFactory = function() {
-    return null;
-  };
-
   QUnit.module("Service Container", {
     beforeEach: function() {
       container = new Misago();
@@ -15,9 +11,106 @@
   });
 
   QUnit.test("addService registers service in container", function(assert) {
+    assert.expect(2);
+
+    var MockServiceFactory = function() {
+      return null;
+    };
+
     container.addService('test', MockServiceFactory);
 
     assert.equal(container._services.length, 1, "addService() registered single service in container");
     assert.equal(container._services[0].item, MockServiceFactory, "addService() registered MockServiceFactory service in container");
   });
+
+  QUnit.test("service factories are called", function(assert) {
+    assert.expect(1);
+
+    var MockServiceFactory = function(_) {
+      assert.ok(_, 'MockServiceFactory was called with container as argument.');
+    };
+
+    container._initServices([{name: 'test', item: MockServiceFactory}]);
+  });
+
+  QUnit.test("factories return values are set as context on container", function(assert) {
+    assert.expect(1);
+
+    var MockServiceFactory = function() {
+      return 'ok!';
+    };
+
+    container._initServices([{name: 'test', item: MockServiceFactory}]);
+    assert.equal(container.test, 'ok!', 'MockServiceFactory return value was set on container.');
+  });
+
+  QUnit.test("factories returning nothing don't set context on container", function(assert) {
+    assert.expect(1);
+
+    var MockServiceFactory = function() {
+      'not returning anything';
+    };
+
+    container._initServices([{name: 'test', item: MockServiceFactory}]);
+    assert.equal(container.test, undefined, "MockServiceFactory return value wasn't set on container.");
+  });
+
+  QUnit.test("services can be destroyed", function(assert) {
+    assert.expect(2);
+
+    var MockService = {
+      factory: function(_) {
+        assert.ok(_, "MockService's factory was called with container as argument.");
+      },
+
+      destroy: function(_) {
+        assert.ok(_, "MockService's destroy was called with container as argument.");
+      }
+    };
+
+    var services = [{name: 'test', item: MockService}];
+
+    container._initServices(services);
+    container._destroyServices(services);
+  });
+
+  QUnit.test("services are initialized and destroyed in right order", function(assert) {
+    assert.expect(2);
+
+    var initialization_order = [];
+    var destruction_order = [];
+
+    var services = [
+      {
+        name: 'test_1',
+        item: {
+          factory: function() {
+            initialization_order.push(1);
+          },
+
+          destroy: function() {
+            destruction_order.push(1);
+          }
+        }
+      },
+      {
+        name: 'test_2',
+        item: {
+          factory: function() {
+            initialization_order.push(2);
+          },
+
+          destroy: function() {
+            destruction_order.push(2);
+          }
+        }
+      }
+    ];
+
+    container._initServices(services);
+    container._destroyServices(services);
+
+    assert.deepEqual(initialization_order, [1, 2], 'services were initialized in right order.');
+    assert.deepEqual(destruction_order, [2, 1], 'services were destroyed in right order.');
+  });
 }());