1 | expect = require('chai').expect
|
2 |
|
3 | Binder = require('binder')
|
4 |
|
5 |
|
6 | class Parent
|
7 | name: 'Parent'
|
8 |
|
9 | class Child1 extends Parent
|
10 | name: 'Child1'
|
11 |
|
12 | class Child2 extends Parent
|
13 | name: 'Child2'
|
14 |
|
15 | class GrandChild extends Child1
|
16 | name: 'GrandChild'
|
17 |
|
18 | class Singleton
|
19 | @scope: 'SINGLETON'
|
20 |
|
21 | class InvalidScope
|
22 | @scope: 'Entirely Invalid'
|
23 |
|
24 |
|
25 | describe 'A binder', ->
|
26 |
|
27 | it 'should create bindings', ->
|
28 | class MyBinder extends Binder
|
29 | configure: ->
|
30 | @bind(Parent).to(Child1)
|
31 | @bind(Child1).to(GrandChild)
|
32 |
|
33 | binder = new MyBinder()
|
34 | expect(binder.bindings).to.have.length 2
|
35 |
|
36 | it 'should throw an exception when re-binding', ->
|
37 | class Rebinder extends Binder
|
38 | configure: ->
|
39 | @bind(Parent).to(Child1).to(Child2)
|
40 | create = -> new Rebinder()
|
41 | expect(create).to.throw /Already bound/
|
42 |
|
43 | it 'should bind a class in a scope', ->
|
44 | class Scoped extends Binder
|
45 | configure: ->
|
46 | @bind(Child1).inScope('singleton')
|
47 |
|
48 | binding = new Scoped().bindings[0]
|
49 | expect(binding.iface).to.equal Child1
|
50 | expect(binding.scope).to.equal 'SINGLETON'
|
51 |
|
52 | it 'should throw an error when binding to an invalid scope', ->
|
53 | class InvalidScoped extends Binder
|
54 | configure: ->
|
55 | @bind(Parent).inScope('pant suit')
|
56 | create = -> new InvalidScoped()
|
57 | expect(create).to.throw /Invalid scope, 'pant suit'/
|
58 |
|
59 | it 'should not bind an instance with invalid scope', ->
|
60 | class BadScope extends Binder
|
61 | configure: ->
|
62 | @bind(InvalidScope)
|
63 | expect(InvalidScope.scope).to.equal 'Entirely Invalid'
|
64 | create = -> new BadScope()
|
65 | expect(create).to.throw /Invalid scope, 'Entirely Invalid'/
|
66 |
|
67 | it 'should throw an error when scoping something with a scope', ->
|
68 | class Rescoper extends Binder
|
69 | configure: ->
|
70 | @bind(Singleton).inScope('INSTANCE')
|
71 | create = -> new Rescoper()
|
72 | expect(create).to.throw /Already scoped to SINGLETON/
|
73 |
|
74 | it 'should bind a constant', ->
|
75 | class ConstantBinder extends Binder
|
76 | configure: ->
|
77 | @bindConstant('frank.age').to(22)
|
78 |
|
79 | binder = new ConstantBinder()
|
80 | expect(binder.bindings).to.have.length 1
|