-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject-token.spec.ts
120 lines (98 loc) · 3.83 KB
/
inject-token.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
import { expect } from 'chai';
import { declareInjectToken, InjectConfig, TypeInjector } from './index';
describe('inject tokens', () => {
it('should be possible to use any constructor without arguments as inject token', () => {
const simpleClassPropValue = 'given simple class prop value';
class SimpleClass {
prop = simpleClassPropValue;
}
const injector = new TypeInjector();
const instance = injector.get(SimpleClass);
expect(instance.prop).to.equal(simpleClassPropValue);
});
it('should be possible to enable injection of any class by adding a static inject config', () => {
const givenSimpleClassPropValue = 'given simple class prop value';
class SimpleClass {
prop = givenSimpleClassPropValue;
}
class ComposedClassWithConfiguration {
simpleClass: SimpleClass;
static injectConfig: InjectConfig = {
deps: [SimpleClass]
}
constructor(simpleClass: SimpleClass) {
this.simpleClass = simpleClass;
}
}
const injector = new TypeInjector();
const instance = injector.get(ComposedClassWithConfiguration);
expect(instance.simpleClass.prop).to.equal(givenSimpleClassPropValue);
});
it('should be possible to add static inject config by extending a class', () => {
const givenSimpleClassPropValue = 'given simple class prop value';
class SimpleClass {
prop = givenSimpleClassPropValue;
}
class ThirdPartyLibraryService {
simpleClass: SimpleClass;
constructor(simpleClass: SimpleClass) {
this.simpleClass = simpleClass;
}
}
const thirdPartyLibraryService = declareInjectToken(ThirdPartyLibraryService)
const injector = TypeInjector.construct()
.provideFactory(thirdPartyLibraryService, {
deps: [SimpleClass],
create: (simpleClass) => new ThirdPartyLibraryService(simpleClass),
})
.build()
;
const instance = injector.get(thirdPartyLibraryService);
expect(instance.simpleClass.prop).to.equal(givenSimpleClassPropValue);
});
describe('create inject token for anyting', () => {
it('should be possible to inject boolean values', () => {
const givenBooleanValue = false;
const tokenForBoolean = declareInjectToken<boolean>('any unique string');
const injector = TypeInjector.construct()
.provideValue(tokenForBoolean, givenBooleanValue)
.build()
;
const result = injector.get(tokenForBoolean);
expect(result).to.equal(givenBooleanValue);
});
it('should be possible to inject functions', () => {
let expectedResult = 'initial value';
const providedFunction = () => expectedResult;
const tokenForFunction = declareInjectToken<() => string>('any unique string');
const injector = TypeInjector.construct()
.provideValue(tokenForFunction, providedFunction)
.build()
;
const fn = injector.get(tokenForFunction);
expectedResult = 'current value';
const actualResult = fn();
expect(actualResult).to.equal(expectedResult);
});
it('should be possible to inject any object', () => {
const providedObject = { prop: 'initial value' };
const tokenForObject = declareInjectToken<{ prop: string }>('any unique string');
const injector = TypeInjector.construct()
.provideValue(tokenForObject, providedObject)
.build()
;
const instance = injector.get(tokenForObject);
expect(instance === providedObject).to.be.true;
});
});
it('will throw an error for tokens that are not provided (yet)', () => {
const unknownToken = declareInjectToken('unknown');
const injector = new TypeInjector();
try {
injector.get(unknownToken);
expect.fail('did not throw');
} catch (e) {
expect((e as {message: string}).message).to.include('could not find a factory');
}
});
});