Skip to content

Commit d3dda61

Browse files
committed
chore(test): migrate remaining core tests to testcomponentbuilder
Also add a small utility function to debug element to get an array of native elements, which works smoothly with the toHaveText matcher.
1 parent 1c8a589 commit d3dda61

File tree

5 files changed

+103
-90
lines changed

5 files changed

+103
-90
lines changed

modules/angular2/src/debug/debug_element.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ export function inspectElement(elementRef: ElementRef): DebugElement {
145145
return DebugElement.create(elementRef);
146146
}
147147

148+
export function asNativeElements(arr: List<DebugElement>): List<any> {
149+
return arr.map((debugEl) => debugEl.nativeElement);
150+
}
151+
148152
/**
149153
* @exportedAs angular2/test
150154
*/

modules/angular2/test/core/compiler/integration_dart_spec.dart

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ library angular2.test.di.integration_dart_spec;
33

44
import 'package:angular2/angular2.dart';
55
import 'package:angular2/di.dart';
6-
import 'package:angular2/src/test_lib/test_bed.dart';
76
import 'package:angular2/test_lib.dart';
87

98
class MockException implements Error {
@@ -40,45 +39,45 @@ void functionThatThrowsNonError() {
4039
main() {
4140
describe('TypeLiteral', () {
4241
it('should publish via appInjector', inject([
43-
TestBed,
42+
TestComponentBuilder,
4443
AsyncTestCompleter
4544
], (tb, async) {
4645
tb.overrideView(Dummy, new View(
4746
template: '<type-literal-component></type-literal-component>',
48-
directives: [TypeLiteralComponent]));
47+
directives: [TypeLiteralComponent]))
4948

50-
tb.createView(Dummy).then((view) {
51-
view.detectChanges();
52-
expect(view.rootNodes).toHaveText('[Hello, World]');
49+
.createAsync(Dummy).then((tc) {
50+
tc.detectChanges();
51+
expect(asNativeElements(tc.componentViewChildren)).toHaveText('[Hello, World]');
5352
async.done();
5453
});
5554
}));
5655
});
5756

5857
describe('Error handling', () {
5958
it('should preserve Error stack traces thrown from components', inject([
60-
TestBed,
59+
TestComponentBuilder,
6160
AsyncTestCompleter
6261
], (tb, async) {
6362
tb.overrideView(Dummy, new View(
6463
template: '<throwing-component></throwing-component>',
65-
directives: [ThrowingComponent]));
64+
directives: [ThrowingComponent]))
6665

67-
tb.createView(Dummy).catchError((e, stack) {
66+
.createAsync(Dummy).catchError((e, stack) {
6867
expect(stack.toString().split('\n')[0]).toEqual(e.message);
6968
async.done();
7069
});
7170
}));
7271

7372
it('should preserve non-Error stack traces thrown from components', inject([
74-
TestBed,
73+
TestComponentBuilder,
7574
AsyncTestCompleter
7675
], (tb, async) {
7776
tb.overrideView(Dummy, new View(
7877
template: '<throwing-component2></throwing-component2>',
79-
directives: [ThrowingComponent2]));
78+
directives: [ThrowingComponent2]))
8079

81-
tb.createView(Dummy).catchError((e, stack) {
80+
.createAsync(Dummy).catchError((e, stack) {
8281
expect(stack.toString().split('\n')[0]).toEqual(e.message);
8382
async.done();
8483
});
@@ -87,30 +86,30 @@ main() {
8786

8887
describe('Property access', () {
8988
it('should distinguish between map and property access', inject([
90-
TestBed,
89+
TestComponentBuilder,
9190
AsyncTestCompleter
9291
], (tb, async) {
9392
tb.overrideView(Dummy, new View(
9493
template: '<property-access></property-access>',
95-
directives: [PropertyAccess]));
94+
directives: [PropertyAccess]))
9695

97-
tb.createView(Dummy).then((view) {
98-
view.detectChanges();
99-
expect(view.rootNodes).toHaveText('prop:foo-prop;map:foo-map');
96+
.createAsync(Dummy).then((tc) {
97+
tc.detectChanges();
98+
expect(asNativeElements(tc.componentViewChildren)).toHaveText('prop:foo-prop;map:foo-map');
10099
async.done();
101100
});
102101
}));
103102

104103
it('should not fallback on map access if property missing', inject([
105-
TestBed,
104+
TestComponentBuilder,
106105
AsyncTestCompleter
107106
], (tb, async) {
108107
tb.overrideView(Dummy, new View(
109108
template: '<no-property-access></no-property-access>',
110-
directives: [NoPropertyAccess]));
109+
directives: [NoPropertyAccess]))
111110

112-
tb.createView(Dummy).then((view) {
113-
expect(() => view.detectChanges())
111+
.createAsync(Dummy).then((tc) {
112+
expect(() => tc.detectChanges())
114113
.toThrowError(new RegExp('property not found'));
115114
async.done();
116115
});
@@ -119,16 +118,16 @@ main() {
119118

120119
describe('OnChange', () {
121120
it('should be notified of changes', inject([
122-
TestBed,
121+
TestComponentBuilder,
123122
AsyncTestCompleter
124123
], (tb, async) {
125124
tb.overrideView(Dummy, new View(
126125
template: '''<on-change [prop]="'hello'"></on-change>''',
127-
directives: [OnChangeComponent]));
126+
directives: [OnChangeComponent]))
128127

129-
tb.createView(Dummy).then((view) {
130-
view.detectChanges();
131-
var cmp = view.rawView.elementInjectors[0].get(OnChangeComponent);
128+
.createAsync(Dummy).then((tc) {
129+
tc.detectChanges();
130+
var cmp = tc.componentViewChildren[0].inject(OnChangeComponent);
132131
expect(cmp.prop).toEqual('hello');
133132
expect(cmp.changes.containsKey('prop')).toEqual(true);
134133
async.done();

0 commit comments

Comments
 (0)