This repository was archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathbootstrap_test.dart
138 lines (123 loc) · 3.91 KB
/
bootstrap_test.dart
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
import 'dart:html';
import 'package:test/test.dart';
import 'package:angular/angular.dart';
import 'package:angular_test/src/bootstrap.dart';
import 'bootstrap_test.template.dart' as ng_generated;
void main() {
ng_generated.initReflector();
Injector _noopInjector(Injector i) => i;
test('should create a new component in the DOM', () async {
final host = Element.div();
final test = await bootstrapForTest(
ng_generated.createNewComponentInDomFactory(),
host,
_noopInjector,
);
expect(host.text, contains('Hello World'));
test.destroy();
});
test('should call a synchronous handler before initial load', () async {
final host = Element.div();
final test = await bootstrapForTest<BeforeChangeDetection>(
ng_generated.createBeforeChangeDetectionFactory(),
host,
_noopInjector,
beforeChangeDetection: (comp) => comp.users.add('Mati'),
);
expect(host.text, contains('Hello Mati!'));
test.destroy();
});
test('should call an asynchronous handler before initial load', () async {
final host = Element.div();
final test = await bootstrapForTest<BeforeChangeDetection>(
ng_generated.createBeforeChangeDetectionFactory(),
host,
_noopInjector,
beforeChangeDetection: (comp) async => comp.users.add('Mati'),
);
expect(host.text, contains('Hello Mati!'));
test.destroy();
});
test('should include user-specified providers', () async {
final host = Element.div();
final test = await bootstrapForTest(
ng_generated.createAddProvidersFactory(),
host,
(i) => Injector.map({TestService: TestService()}, i),
);
var instance = test.instance;
expect(instance._testService, isNotNull);
test.destroy();
});
test('should be able to call injector before component creation', () async {
final host = Element.div();
TestService? testService;
final test = await bootstrapForTest(
ng_generated.createAddProvidersFactory(),
host,
(i) => Injector.map({TestService: TestService()}, i),
beforeComponentCreated: (injector) {
testService = injector.provideType(TestService);
testService!.count++;
}, beforeChangeDetection: (_) {
if (testService == null) {
fail('`beforeComponentCreated` should be invoked before'
' `beforeChangeDetection`, `testService` should not be null.');
}
});
var instance = test.instance;
expect(testService, instance._testService);
expect(testService!.count, 1);
test.destroy();
});
test('should be able to call asynchronous injector before component creation',
() async {
final host = Element.div();
TestService? testService;
final test = await bootstrapForTest(
ng_generated.createAddProvidersFactory(),
host,
(i) => Injector.map({TestService: TestService()}, i),
beforeComponentCreated: (injector) =>
Future.delayed(Duration(milliseconds: 200), () {}).then((_) {
testService = injector.provideType(TestService);
testService!.count++;
}),
beforeChangeDetection: (_) {
if (testService == null) {
fail('`beforeComponentCreated` should be invoked before'
' `beforeChangeDetection`, `testService` should not be null.');
}
},
);
var instance = test.instance;
expect(testService, instance._testService);
expect(testService!.count, 1);
test.destroy();
});
}
@Component(
selector: 'test',
template: 'Hello World',
)
class NewComponentInDom {}
@Component(
selector: 'test',
template: 'Hello {{users.first}}!',
)
class BeforeChangeDetection {
// This will fail with an NPE if not initialized before change detection.
final users = <String>[];
}
@Component(
selector: 'test',
template: '',
)
class AddProviders {
final TestService _testService;
AddProviders(this._testService);
}
@Injectable()
class TestService {
int count = 0;
}