1 /** 2 * Created by JetBrains WebStorm. 3 * User: creynder 4 * Date: 21/09/11 5 * Time: 14:53 6 * To change this template use File | Settings | File Templates. 7 */ 8 var injector; 9 10 function TestClassA(){ 11 this.name = 'TestClassA'; 12 } 13 14 function TestClassB(){ 15 this.name = 'TestClassB'; 16 17 this.bar = undefined; 18 } 19 20 //TODO: test Injectro#setValue 21 22 module( 'dijon.Injector', { 23 setup : function(){ 24 injector = new dijon.System(); 25 }, 26 teardown : function(){ 27 injector = undefined; 28 } 29 }); 30 31 test( 'mapSingleton', function(){ 32 injector.mapSingleton( 'a', TestClassA ); 33 ok( injector.hasMapping( 'a' ) ); 34 }) 35 36 test( 'mapValue', function(){ 37 var a = new TestClassA(); 38 injector.mapValue( 'a', a ); 39 ok( injector.hasMapping( 'a' ) ); 40 }) 41 42 test( 'mapClass', function(){ 43 injector.mapClass( 'a', function(){} ); 44 ok( injector.hasMapping( 'a' ) ); 45 }) 46 47 test( 'getObject for mapSingleton', function(){ 48 injector.mapSingleton( 'a', TestClassA ); 49 var inj = injector; 50 var a = injector.getObject( 'a' ); 51 ok( a instanceof TestClassA, 'must be instance of TestClassA' ); 52 strictEqual( injector.getObject( 'a' ), a, 'must be single instance' ); 53 }) 54 55 test( 'getObject for mapClass', function(){ 56 injector.mapClass( 'a', TestClassA ); 57 var a = injector.getObject( 'a' ); 58 ok( a instanceof TestClassA ); 59 notStrictEqual( injector.getObject( 'a' ), a ); 60 }) 61 62 test( 'getObject for mapValue', function(){ 63 var a = new TestClassA(); 64 injector.mapValue( 'a', a ); 65 strictEqual( injector.getObject( 'a' ), a ); 66 }) 67 68 test( 'instantiate for mapSingleton', function(){ 69 injector.mapSingleton( 'a', TestClassA ); 70 var a = injector.getObject( 'a' ); 71 notStrictEqual( injector.instantiate( 'a' ), a, 'instantiate must always provide a new instance regardless of rules' ); 72 }) 73 74 75 test( 'instantiate for mapClass', function(){ 76 injector.mapClass( 'a', TestClassA ); 77 var a = injector.getObject( 'a' ); 78 notStrictEqual( injector.instantiate( 'a' ), a ); 79 }) 80 81 test( 'instantiate for mapValue', function(){ 82 var a = new TestClassA(); 83 injector.mapValue( 'a', a ); 84 notStrictEqual( injector.instantiate( 'a' ), a ); 85 }) 86 87 test( 'unmap', function(){ 88 injector.mapSingleton( 'a', TestClassA ) 89 injector.unmap( 'a' ); 90 ok( ! injector.hasMapping( 'a' ) ); 91 }) 92 93 test( 'addOutlet', function(){ 94 injector.mapSingleton( 'a', TestClassA ); 95 injector.mapSingleton( 'b', TestClassB ); 96 injector.mapOutlet( 'a', 'b', 'bar' ); 97 var b = injector.getObject( 'b' ); 98 ok( b.bar instanceof TestClassA ); 99 }) 100 101 test( 'removeOutlet', function() { 102 injector.mapSingleton( 'a', TestClassA ); 103 injector.mapSingleton( 'b', TestClassB ); 104 injector.mapOutlet( 'a', 'b', 'bar' ); 105 injector.unmapOutlet( 'b', 'bar' ); 106 var b = injector.getObject( 'b' ); 107 strictEqual( b.bar, undefined ); 108 }) 109 test( 'addHandler: simple use', function(){ 110 var hasExecuted = false; 111 var c = function(){ 112 this.loginStart = function(){ 113 hasExecuted = true; 114 } 115 } 116 injector.mapSingleton( 'a', c ); 117 injector.mapHandler( 'loginStart', 'a' ) 118 injector.notify( 'loginStart' ); 119 ok( hasExecuted ); 120 }) 121 122 test( 'addHandler: oneShot', function(){ 123 var hasExecuted = 0; 124 var c = function(){ 125 this.loginStart = function(){ 126 hasExecuted++; 127 } 128 } 129 injector.mapSingleton( 'a', c ); 130 injector.mapHandler( 'loginStart', 'a', null, true ); 131 injector.notify( 'loginStart' ); 132 injector.notify( 'loginStart' ); 133 injector.notify( 'loginStart' ); 134 injector.notify( 'loginStart' ); 135 injector.notify( 'loginStart' ); 136 ok( hasExecuted==1); 137 }) 138 139 test( 'addHandler: diffreten handler name', function(){ 140 var hasExecuted = false; 141 var c = function(){ 142 this.onLoginStart = function(){ 143 hasExecuted = true; 144 } 145 } 146 injector.mapSingleton( 'a', c ); 147 injector.mapHandler( 'loginStart', 'a', 'onLoginStart' ) 148 injector.notify( 'loginStart' ); 149 ok( hasExecuted ); 150 }) 151 152 153 test( 'unmapHandler: simple use', function(){ 154 var hasExecuted = 0; 155 var c = function(){ 156 this.loginStart = function(){ 157 hasExecuted++; 158 } 159 } 160 injector.mapSingleton( 'a', c ); 161 injector.mapHandler( 'loginStart', 'a' ); 162 injector.notify( 'loginStart' ); 163 injector.notify( 'loginStart' ); 164 injector.unmapHandler( 'loginStart', 'a' ); 165 injector.notify( 'loginStart' ); 166 ok( hasExecuted == 2 ); 167 }) 168 169 test( 'addCallback: simple use', function(){ 170 var hasExecuted = false; 171 var o = { 172 loginStart : function(){ 173 hasExecuted = true; 174 } 175 } 176 injector.addCallback( 'loginStart', o.loginStart ); 177 injector.notify( 'loginStart' ); 178 ok( hasExecuted ); 179 }) 180 181 182 test( 'removeCallback: simple use', function(){ 183 var hasExecuted = 0; 184 var o = { 185 loginStart : function(){ 186 hasExecuted ++; 187 } 188 } 189 injector.addCallback( 'loginStart', o.loginStart ); 190 injector.notify( 'loginStart' ); 191 injector.notify( 'loginStart' ); 192 injector.removeCallback( 'loginStart', o.loginStart ); 193 ok( hasExecuted==2 ); 194 }) 195 196 test( 'notify: payload passing to callbacks', function(){ 197 var a = 'a'; 198 var b = { 199 name : 'b' 200 } 201 var passed1; 202 var passed2; 203 var o = { 204 foo : function( e, p1, p2 ){ 205 passed1 = p1; 206 passed2 = p2; 207 } 208 } 209 210 injector.addCallback( 'mofo', o.foo, true, true ); 211 injector.notify( 'mofo', a, b ); 212 equal( passed1, a ); 213 equal( passed2, b ); 214 }) 215 216 test( 'instantiate value', function(){ 217 var a = { 218 name : 'a' 219 } 220 221 injector.mapValue( 'a', a ); 222 var inst = injector.instantiate( 'a' ); 223 ok( inst !== a ); 224 }) 225 226 test( 'getInstance value', function(){ 227 var a = { 228 name : 'a' 229 } 230 231 injector.mapValue( 'a', a ); 232 var inst = injector.getObject( 'a' ); 233 ok( inst === a ); 234 })