-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
injector_spec.ts
101 lines (91 loc) 路 3 KB
/
injector_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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
inject,
InjectFlags,
InjectionToken,
InjectOptions,
Injector,
ProviderToken,
傻InjectorProfilerContext,
傻setCurrentInjector as setCurrentInjector,
傻setInjectorProfilerContext,
} from '@angular/core';
class MockRootScopeInjector implements Injector {
constructor(readonly parent: Injector) {}
get<T>(
token: ProviderToken<T>,
defaultValue?: any,
flags: InjectFlags | InjectOptions = InjectFlags.Default,
): T {
if ((token as any).傻prov && (token as any).傻prov.providedIn === 'root') {
const old = setCurrentInjector(this);
const previousInjectorProfilerContext = 傻setInjectorProfilerContext({
injector: this,
token: null,
});
try {
return (token as any).傻prov.factory();
} finally {
setCurrentInjector(old);
傻setInjectorProfilerContext(previousInjectorProfilerContext);
}
}
return this.parent.get(token, defaultValue, flags);
}
}
{
describe('injector metadata examples', () => {
it('works', () => {
// #docregion Injector
const injector: Injector = Injector.create({
providers: [{provide: 'validToken', useValue: 'Value'}],
});
expect(injector.get('validToken')).toEqual('Value');
expect(() => injector.get('invalidToken')).toThrowError();
expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');
// #enddocregion
});
it('injects injector', () => {
// #docregion injectInjector
const injector = Injector.create({providers: []});
expect(injector.get(Injector)).toBe(injector);
// #enddocregion
});
it('should infer type', () => {
// #docregion InjectionToken
const BASE_URL = new InjectionToken<string>('BaseUrl');
const injector = Injector.create({
providers: [{provide: BASE_URL, useValue: 'http://localhost'}],
});
const url = injector.get(BASE_URL);
// Note: since `BASE_URL` is `InjectionToken<string>`
// `url` is correctly inferred to be `string`
expect(url).toBe('http://localhost');
// #enddocregion
});
it('injects a tree-shakeable InjectionToken', () => {
class MyDep {}
const injector = new MockRootScopeInjector(
Injector.create({providers: [{provide: MyDep, deps: []}]}),
);
// #docregion ShakableInjectionToken
class MyService {
constructor(readonly myDep: MyDep) {}
}
const MY_SERVICE_TOKEN = new InjectionToken<MyService>('Manually constructed MyService', {
providedIn: 'root',
factory: () => new MyService(inject(MyDep)),
});
const instance = injector.get(MY_SERVICE_TOKEN);
expect(instance instanceof MyService).toBeTruthy();
expect(instance.myDep instanceof MyDep).toBeTruthy();
// #enddocregion
});
});
}