service-container.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function () {
  2. 'use strict';
  3. var container = null;
  4. var orgServices = Misago.prototype._services;
  5. QUnit.module("Service Container", {
  6. beforeEach: function() {
  7. Misago.prototype._services = [];
  8. container = new Misago();
  9. },
  10. afterEach: function() {
  11. Misago.prototype._services = orgServices;
  12. }
  13. });
  14. QUnit.test("addService registers service in container", function(assert) {
  15. assert.expect(2);
  16. var MockServiceFactory = function() {
  17. return null;
  18. };
  19. Misago.prototype.addService('test', MockServiceFactory);
  20. assert.equal(Misago.prototype._services.length, 1, "addService() registered single service in container");
  21. assert.equal(Misago.prototype._services[0].item, MockServiceFactory, "addService() registered MockServiceFactory service in container");
  22. });
  23. QUnit.test("service factories are called", function(assert) {
  24. assert.expect(1);
  25. var MockServiceFactory = function(_) {
  26. assert.ok(_, 'MockServiceFactory was called with container as argument.');
  27. };
  28. container._initServices([{key: 'test', item: MockServiceFactory}]);
  29. });
  30. QUnit.test("factories return values are set as context on container", function(assert) {
  31. assert.expect(1);
  32. var MockServiceFactory = function() {
  33. return 'ok!';
  34. };
  35. container._initServices([{key: 'test', item: MockServiceFactory}]);
  36. assert.equal(container.test, 'ok!', 'MockServiceFactory return value was set on container.');
  37. });
  38. QUnit.test("factories returning nothing don't set context on container", function(assert) {
  39. assert.expect(1);
  40. var MockServiceFactory = function() {
  41. 'not returning anything';
  42. };
  43. container._initServices([{key: 'test', item: MockServiceFactory}]);
  44. assert.equal(container.test, undefined, "MockServiceFactory return value wasn't set on container.");
  45. });
  46. QUnit.test("services can be destroyed", function(assert) {
  47. assert.expect(2);
  48. var MockService = {
  49. factory: function(_) {
  50. assert.ok(_, "MockService's factory was called with container as argument.");
  51. },
  52. destroy: function(_) {
  53. assert.ok(_, "MockService's destroy was called with container as argument.");
  54. }
  55. };
  56. var services = [{key: 'test', item: MockService}];
  57. container._initServices(services);
  58. container._destroyServices(services);
  59. });
  60. QUnit.test("services are initialized and destroyed in right order", function(assert) {
  61. assert.expect(2);
  62. var initializationOrder = [];
  63. var destructionOrder = [];
  64. var services = [
  65. {
  66. key: 'test_1',
  67. item: {
  68. factory: function() {
  69. initializationOrder.push(1);
  70. },
  71. destroy: function() {
  72. destructionOrder.push(1);
  73. }
  74. }
  75. },
  76. {
  77. key: 'test_2',
  78. item: {
  79. factory: function() {
  80. initializationOrder.push(2);
  81. },
  82. destroy: function() {
  83. destructionOrder.push(2);
  84. }
  85. }
  86. }
  87. ];
  88. container._initServices(services);
  89. container._destroyServices(services);
  90. assert.deepEqual(initializationOrder, [1, 2], 'services were initialized in right order.');
  91. assert.deepEqual(destructionOrder, [2, 1], 'services were destroyed in right order.');
  92. });
  93. QUnit.test("initialization data is stored on container", function(assert) {
  94. assert.expect();
  95. container.init({fixture: 'test'});
  96. assert.equal(container.setup.fixture, 'test', 'container stored initialization data');
  97. });
  98. }());