Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit b4654fb

Browse files
mvuksanovicb
authored andcommitted
refactor: Refactor test setup.
Befor each test, a div element with attribute ng-app, is created. This is a container into which test can put some content. This container is destroyed after each test Closes #925
1 parent a300adf commit b4654fb

File tree

6 files changed

+136
-93
lines changed

6 files changed

+136
-93
lines changed

lib/mock/module.dart

+21-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ part 'mock_window.dart';
5656
*/
5757
class AngularMockModule extends Module {
5858
AngularMockModule() {
59+
bind(MockApplication);
60+
bind(Application, toImplementation: MockApplication);
5961
bind(ExceptionHandler, toImplementation: RethrowExceptionHandler);
6062
bind(TestBed);
6163
bind(Probe);
6264
bind(Logger);
6365
bind(MockHttpBackend);
64-
bind(Element, toValue: document.body);
65-
bind(Node, toValue: document.body);
66+
bind(Element, toFactory: (app) => app.element, inject: [MockApplication]);
67+
bind(Node, inject: [Element]);
6668
bind(HttpBackend, toInstanceOf: MOCK_HTTP_BACKEND_KEY);
6769
bind(VmTurnZone, toFactory: () {
6870
return new VmTurnZone()
@@ -74,3 +76,20 @@ class AngularMockModule extends Module {
7476
bind(WebPlatform, toValue: mockPlatform);
7577
}
7678
}
79+
80+
class MockApplication extends Application {
81+
var _element;
82+
83+
Element get element {
84+
if (_element == null) {
85+
_element = new DivElement()..attributes['ng-app'] = '';
86+
}
87+
return _element;
88+
}
89+
90+
void destroyElement() {
91+
_element = null;
92+
}
93+
94+
Injector createInjector() => throw 'MockApplications can not create injectors';
95+
}

lib/mock/test_injection.dart

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
library angular.mock.test_injection;
22

3+
import 'dart:html';
4+
5+
import 'package:angular/application.dart';
36
import 'package:angular/application_factory.dart';
47
import 'package:angular/mock/module.dart';
58
import 'package:di/di.dart';
69
import 'dart:mirrors';
10+
import 'module.dart';
711

812
_SpecInjector _currentSpecInjector = null;
913

@@ -61,7 +65,7 @@ class _SpecInjector {
6165
}
6266
}
6367

64-
reset() {
68+
void reset() {
6569
injector = null;
6670
injectorCreateLocation = null;
6771
}
@@ -161,3 +165,12 @@ void setUpInjector() {
161165
void tearDownInjector() {
162166
_currentSpecInjector = null;
163167
}
168+
169+
void cleanUpAppRoot() {
170+
if (_currentSpecInjector.injector != null) {
171+
var app = _currentSpecInjector.injector.get(Application);
172+
assert(app is MockApplication);
173+
app.destroyElement();
174+
}
175+
document.body.setInnerHtml('');
176+
}

test/_specs.dart

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ _removeNgBinding(node) {
115115

116116
main() {
117117
gns.beforeEach(setUpInjector, priority:3);
118+
gns.afterEach(cleanUpAppRoot);
118119
gns.afterEach(tearDownInjector);
119120

120121
gns.guinnessEnableHtmlMatchers();

test/animate/ng_animate_spec.dart

-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ main() {
2626
});
2727
});
2828

29-
afterEach(() {
30-
tearDownInjector();
31-
});
32-
3329
it('should control animations on elements', () {
3430
_.compile('<div ng-animate="never"><div></div></div>');
3531
_.rootScope.apply();

test/core_dom/event_handler_spec.dart

+20-29
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,50 @@ class BarComponent {
2525

2626
main() {
2727
describe('EventHandler', () {
28-
Element ngAppElement;
2928
beforeEachModule((Module module) {
30-
ngAppElement = new DivElement()..attributes['ng-app'] = '';
31-
module..bind(FooController);
32-
module..bind(BarComponent);
33-
module..bind(Node, toValue: ngAppElement);
34-
document.body.append(ngAppElement);
29+
module..bind(FooController)
30+
..bind(BarComponent);
3531
});
3632

37-
afterEach(() {
38-
ngAppElement.remove();
39-
ngAppElement = null;
40-
});
41-
42-
compile(_, html) {
43-
ngAppElement.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer());
44-
_.compile(ngAppElement);
45-
return ngAppElement.firstChild;
46-
}
4733

48-
it('should register and handle event', (TestBed _) {
49-
var e = compile(_,
34+
it('should register and handle event', (TestBed _, Application app) {
35+
var e = _.compile(
5036
'''<div foo>
51-
<div on-abc="ctrl.invoked=true;"></div>
37+
<div on-abc="ctrl.invoked=true"></div>
5238
</div>''');
39+
document.body.append(app.element..append(e));
5340

5441
_.triggerEvent(e.querySelector('[on-abc]'), 'abc');
5542
expect(_.getScope(e).context['ctrl'].invoked).toEqual(true);
5643
});
5744

58-
it('shoud register and handle event with long name', (TestBed _) {
59-
var e = compile(_,
45+
it('shoud register and handle event with long name', (TestBed _, Application app) {
46+
var e = _.compile(
6047
'''<div foo>
61-
<div on-my-new-event="ctrl.invoked=true;"></div>
48+
<div on-my-new-event="ctrl.invoked=true"></div>
6249
</div>''');
50+
document.body.append(app.element..append(e));
6351

6452
_.triggerEvent(e.querySelector('[on-my-new-event]'), 'myNewEvent');
6553
var fooScope = _.getScope(e);
6654
expect(fooScope.context['ctrl'].invoked).toEqual(true);
6755
});
6856

69-
it('shoud have model updates applied correctly', (TestBed _) {
70-
var e = compile(_,
57+
it('should have model updates applied correctly', (TestBed _, Application app) {
58+
var e = _.compile(
7159
'''<div foo>
72-
<div on-abc='ctrl.description="new description";'>{{ctrl.description}}</div>
60+
<div on-abc='ctrl.description="new description"'>{{ctrl.description}}</div>
7361
</div>''');
62+
document.body.append(app.element..append(e));
7463
var el = document.querySelector('[on-abc]');
7564
el.dispatchEvent(new Event('abc'));
7665
_.rootScope.apply();
7766
expect(el.text).toEqual("new description");
7867
});
7968

80-
it('shoud register event when shadow dom is used', async((TestBed _) {
81-
var e = compile(_,'<bar></bar>');
69+
it('should register event when shadow dom is used', async((TestBed _, Application app) {
70+
var e = _.compile('<bar></bar>');
71+
document.body.append(app.element..append(e));
8272

8373
microLeap();
8474

@@ -89,13 +79,14 @@ main() {
8979
expect(ctrl.invoked).toEqual(true);
9080
}));
9181

92-
it('shoud handle event within content only once', async((TestBed _) {
93-
var e = compile(_,
82+
it('shoud handle event within content only once', async((TestBed _, Application app) {
83+
var e = _.compile(
9484
'''<div foo>
9585
<bar>
9686
<div on-abc="ctrl.invoked=true;"></div>
9787
</bar>
9888
</div>''');
89+
document.body.append(app.element..append(e));
9990

10091
microLeap();
10192

0 commit comments

Comments
 (0)