UNPKG

1.66 kBtext/coffeescriptView Raw
1expect = require('chai').expect
2
3inject = require('inject')
4Binder = require('binder')
5Injector = require('injector')
6
7mapInits = 0
8funMapInits = 0
9
10class Map
11 @scope: 'SINGLETON'
12 createMap: -> throw Error('Abstract method createMap')
13 init: -> @map = @createMap()
14 constructor: ->
15 mapInits += 1
16 throw Error('One can never be too safe')
17
18class FunMap extends Map
19 @inits = 0
20 createMap: -> return 'Fun!'
21 constructor: ->
22 # Don't call super!
23 funMapInits += 1
24
25class App
26 map: inject(Map)
27
28class AppBinder extends Binder
29 configure: ->
30 @bind(Map).to(FunMap)
31
32 # There is a bug when there are multiple bindings it can screw up
33 # implementations are resolved during a single injection cycle. This binding
34 # does nothing other that expose the bug.
35 @bindConstant('unrelated').to(666)
36
37
38describe 'Abstract classes', ->
39 beforeEach ->
40 @binder = new AppBinder()
41 @injector = new Injector(@binder)
42
43 afterEach ->
44 mapInits = 0
45 funMapInits = 0
46
47 it 'should have classes in a default state', ->
48 expect(mapInits).to.equal 0
49 expect(funMapInits).to.equal 0
50
51 it 'should obey FunMap not calling super', ->
52 new FunMap()
53 expect(mapInits).to.equal 0
54 expect(funMapInits).to.equal 1
55
56 it 'should only create the required impls', ->
57 @injector.getInstance(FunMap)
58 expect(mapInits).to.equal 0
59 expect(funMapInits).to.equal 1
60
61 @injector.getInstance(Map)
62 expect(mapInits).to.equal 0
63 expect(funMapInits).to.equal 1
64
65 it 'should only create bound impls via two steps', ->
66 @injector.getInstance(App)
67 expect(mapInits).to.equal 0
68 expect(funMapInits).to.equal 1