1 | expect = require('chai').expect
|
2 |
|
3 | Injector = require('injector')
|
4 | inject = require('inject')
|
5 |
|
6 | class SomeSingleton
|
7 | @scope: 'SINGLETON'
|
8 | constructor: ->
|
9 | @name = 'singleton!'
|
10 |
|
11 | describe 'Inject', ->
|
12 | it 'should inject requried fields', ->
|
13 | class MyCat
|
14 | constructor: (@likes) ->
|
15 | @name = 'Mittenslayer'
|
16 |
|
17 | class CatSitter
|
18 | cat: inject(MyCat)
|
19 | age: 44
|
20 |
|
21 | injector = new Injector()
|
22 | sitter = injector.getInstance(CatSitter)
|
23 | expect(sitter.cat).to.exist
|
24 | expect(sitter.cat.name).to.equal 'Mittenslayer'
|
25 | expect(sitter.cat.likes).to.not.exist
|
26 |
|
27 | it 'should pass construction fields', ->
|
28 | class MyCat
|
29 | constructor: (@likes) ->
|
30 | @name = 'Mittenslayer'
|
31 |
|
32 | class CatSitter
|
33 | cat: inject(MyCat, 'kibble')
|
34 | age: 44
|
35 |
|
36 | injector = new Injector()
|
37 | sitter = injector.getInstance(CatSitter)
|
38 | expect(sitter.cat.name).to.equal 'Mittenslayer'
|
39 | expect(sitter.cat.likes).to.equal 'kibble'
|
40 |
|
41 | it 'should not pass argument params to a singleton', ->
|
42 | class MyCat
|
43 | @scope: 'SINGLETON'
|
44 | constructor: (@likes) ->
|
45 | @name = 'Mittenslayer'
|
46 |
|
47 | injector = new Injector()
|
48 | create = ->
|
49 | class CatSitter
|
50 | cat: inject(MyCat, 'kibble')
|
51 | injector.getInstance(CatSitter)
|
52 | expect(create).to.throw /Cannot assign arguments to a singleton/
|
53 |
|
54 | it 'should resolve a parents injected fields', ->
|
55 | class Parent
|
56 | parentField: inject(SomeSingleton)
|
57 |
|
58 | class Child extends Parent
|
59 | childField: inject(SomeSingleton)
|
60 |
|
61 | injector = new Injector()
|
62 | child = injector.getInstance(Child)
|
63 |
|
64 | expect(child.childField).to.exist
|
65 | expect(child.parentField).to.exist
|
66 | expect(child.childField).to.equal (child.parentField)
|
67 |
|
68 | it 'should have injected fields in place before the constructor is called', (done) ->
|
69 | class Constd
|
70 | buddy: inject(SomeSingleton)
|
71 | age: 23
|
72 |
|
73 | constructor: ->
|
74 | expect(@buddy).to.exist
|
75 | expect(@buddy._requiresInjection_).to.not.exist
|
76 | expect(@buddy.name).to.equal 'singleton!'
|
77 | expect(@age).to.equal 23
|
78 | done()
|
79 |
|
80 | new Injector().getInstance(Constd)
|