-
Notifications
You must be signed in to change notification settings - Fork 2
/
injector.class.spec.ts
136 lines (90 loc) · 3.24 KB
/
injector.class.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { expect } from 'chai';
import { GetMapping } from '../../http/mappings';
import { Injector } from './injector.class';
import { LitComponent } from '../decorators/component.decorator';
import { LitService } from '../decorators/service.decorator';
describe('Class: Injector', () => {
it('should instantiate an injector', () => {
expect(Injector).to.not.be.undefined;
});
it('should get a new instance of a class', () => {
class TestClass {
someProp: string = 'test';
constructor() {
}
}
const aTestClass: TestClass = Injector.resolve(TestClass);
expect(aTestClass.someProp).to.equal('test');
});
it('should get a new instance of a service with dependencies', () => {
@LitService()
class TestService {
someProp = 'test-val';
}
@LitService()
class AnotherService {
constructor(private testDep: TestService) {
}
get theProp() {
return this.testDep.someProp;
}
}
const aTestClass: AnotherService = Injector.resolve(AnotherService);
expect(aTestClass.theProp).to.equal('test-val');
});
it('should get a new instance of a component with dependencies', () => {
@LitService()
class TestService {
someProp = 'test-val';
}
@LitComponent()
class TestComponent {
constructor(private testDep: TestService) {
}
get theProp() {
return this.testDep.someProp;
}
}
const aTestComponent: TestComponent = Injector.resolve(TestComponent);
expect(aTestComponent.theProp).to.equal('test-val');
});
it('should allow setting metadata without a property key', () => {
class TestClass {
}
Injector.set(TestClass, { someProp: 'test' });
});
it('should get all metadata for a method', () => {
const expectedMetadata: any = {
path: 'some-path',
produces: 'some-test'
};
@LitComponent()
class TestComponent {
@GetMapping(expectedMetadata)
someTest() {
}
}
const aTestComponent: any = Injector.resolve(TestComponent);
const metadata: any = Injector.getAll(aTestComponent, 'someTest');
expect(metadata.path).to.equal(expectedMetadata.path);
expect(metadata.produces).to.equal(expectedMetadata.produces);
});
it('should return a list of param types', () => {
@LitComponent()
class TestComponent {
@GetMapping()
resOnly(res) {
}
@GetMapping()
reqRes(req, res) {
}
@GetMapping()
reqResNext(req, res, next) {
}
}
expect(Injector.getParams(TestComponent, 'noMethod').length).to.equal(0);
expect(Injector.getParams(TestComponent, 'resOnly').length).to.equal(1);
expect(Injector.getParams(TestComponent, 'reqRes').length).to.equal(2);
expect(Injector.getParams(TestComponent, 'reqResNext').length).to.equal(3);
});
});