forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_spec.ts
169 lines (147 loc) · 4.66 KB
/
app_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @license
* Copyright Google Inc. 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 {Component, INJECTOR, Injectable, NgModule} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {renderModuleFactory} from '@angular/platform-server';
import {BasicAppModuleNgFactory} from 'app_built/src/basic.ngfactory';
import {DepAppModuleNgFactory} from 'app_built/src/dep.ngfactory';
import {HierarchyAppModuleNgFactory} from 'app_built/src/hierarchy.ngfactory';
import {RootAppModuleNgFactory} from 'app_built/src/root.ngfactory';
import {SelfAppModuleNgFactory} from 'app_built/src/self.ngfactory';
import {StringAppModuleNgFactory} from 'app_built/src/string.ngfactory';
import {TokenAppModuleNgFactory} from 'app_built/src/token.ngfactory';
describe('ngInjectableDef Bazel Integration', () => {
it('works in AOT', done => {
renderModuleFactory(BasicAppModuleNgFactory, {
document: '<id-app></id-app>',
url: '/',
}).then(html => {
expect(html).toMatch(/>0:0<\//);
done();
});
});
it('@Self() works in component hierarchies', done => {
renderModuleFactory(HierarchyAppModuleNgFactory, {
document: '<hierarchy-app></hierarchy-app>',
url: '/',
}).then(html => {
expect(html).toMatch(/>false<\//);
done();
});
});
it('@Optional() Self() resolves to @Injectable() scoped service', done => {
renderModuleFactory(SelfAppModuleNgFactory, {
document: '<self-app></self-app>',
url: '/',
}).then(html => {
expect(html).toMatch(/>true<\//);
done();
});
});
it('InjectionToken ngInjectableDef works', done => {
renderModuleFactory(TokenAppModuleNgFactory, {
document: '<token-app></token-app>',
url: '/',
}).then(html => {
expect(html).toMatch(/>fromToken<\//);
done();
});
});
it('APP_ROOT_SCOPE works', done => {
renderModuleFactory(RootAppModuleNgFactory, {
document: '<root-app></root-app>',
url: '/',
}).then(html => {
expect(html).toMatch(/>true:false<\//);
done();
});
});
it('can inject dependencies', done => {
renderModuleFactory(DepAppModuleNgFactory, {
document: '<dep-app></dep-app>',
url: '/',
}).then(html => {
expect(html).toMatch(/>true<\//);
done();
});
});
it('string tokens work', done => {
renderModuleFactory(StringAppModuleNgFactory, {
document: '<string-app></string-app>',
url: '/',
}).then(html => {
expect(html).toMatch(/>works<\//);
done();
});
});
it('allows provider override in JIT for root-scoped @Injectables', () => {
@Injectable({
providedIn: 'root',
useValue: new Service('default'),
})
class Service {
constructor(readonly value: string) {}
}
TestBed.configureTestingModule({});
TestBed.overrideProvider(Service, {useValue: new Service('overridden')});
expect(TestBed.get(Service).value).toEqual('overridden');
});
it('allows provider override in JIT for module-scoped @Injectables', () => {
@NgModule()
class Module {
}
@Injectable({
providedIn: Module,
useValue: new Service('default'),
})
class Service {
constructor(readonly value: string) {}
}
TestBed.configureTestingModule({
imports: [Module],
});
TestBed.overrideProvider(Service, {useValue: new Service('overridden')});
expect(TestBed.get(Service).value).toEqual('overridden');
});
it('does not override existing ngInjectableDef', () => {
@Injectable({
providedIn: 'root',
useValue: new Service(false),
})
class Service {
constructor(public value: boolean) {}
static ngInjectableDef = {
providedIn: 'root',
factory: () => new Service(true),
token: Service,
};
}
TestBed.configureTestingModule({});
expect(TestBed.get(Service).value).toEqual(true);
});
it('NgModule injector understands requests for INJECTABLE', () => {
TestBed.configureTestingModule({
providers: [{provide: 'foo', useValue: 'bar'}],
});
expect(TestBed.get(INJECTOR).get('foo')).toEqual('bar');
});
it('Component injector understands requests for INJECTABLE', () => {
@Component({
selector: 'test-cmp',
template: 'test',
providers: [{provide: 'foo', useValue: 'bar'}],
})
class TestCmp {
}
TestBed.configureTestingModule({
declarations: [TestCmp],
});
const fixture = TestBed.createComponent(TestCmp);
expect(fixture.componentRef.injector.get(INJECTOR).get('foo')).toEqual('bar');
});
});