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

Commit 8f225aa

Browse files
mvuksanomhevery
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 45364e5 commit 8f225aa

File tree

6 files changed

+132
-95
lines changed

6 files changed

+132
-95
lines changed

lib/mock/module.dart

+21-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ part 'mock_window.dart';
5252
*/
5353
class AngularMockModule extends Module {
5454
AngularMockModule() {
55+
bind(MockApplication);
56+
bind(Application, toFactory: (i) => i.get(MockApplication));
5557
bind(ExceptionHandler, toImplementation: RethrowExceptionHandler);
5658
bind(TestBed);
5759
bind(Probe);
5860
bind(Logger);
5961
bind(MockHttpBackend);
60-
bind(Element, toValue: document.body);
61-
bind(Node, toValue: document.body);
62+
bind(Element, toFactory: (i) => i.get(Application).element);
63+
bind(Node, toFactory: (i) => i.get(Element));
6264
bind(HttpBackend, toFactory: (Injector i) => i.get(MockHttpBackend));
6365
bind(VmTurnZone, toFactory: (_) {
6466
return new VmTurnZone()
@@ -67,3 +69,20 @@ class AngularMockModule extends Module {
6769
bind(Window, toImplementation: MockWindow);
6870
}
6971
}
72+
73+
class MockApplication extends Application {
74+
var _element;
75+
76+
Element get element {
77+
if (_element == null) {
78+
_element = new DivElement()..attributes['ng-app'] = '';
79+
}
80+
return _element;
81+
}
82+
83+
void destroyElement() {
84+
_element = null;
85+
}
86+
87+
Injector createInjector() => throw 'MockApplications can not create injectors';
88+
}

lib/mock/test_injection.dart

+13-5
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 'package:di/dynamic_injector.dart';
10+
import 'module.dart';
711

812
_SpecInjector _currentSpecInjector = null;
913

@@ -60,11 +64,6 @@ class _SpecInjector {
6064
throw "$e\n$s\nDECLARED AT:$declarationStack";
6165
}
6266
}
63-
64-
reset() {
65-
injector = null;
66-
injectiorCreateLocation = null;
67-
}
6867
}
6968

7069
/**
@@ -147,3 +146,12 @@ void setUpInjector() {
147146
void tearDownInjector() {
148147
_currentSpecInjector = null;
149148
}
149+
150+
void cleanUpAppRoot() {
151+
if (_currentSpecInjector.injector != null) {
152+
var app = _currentSpecInjector.injector.get(Application);
153+
assert(app is MockApplication);
154+
app.destroyElement();
155+
}
156+
document.body.setInnerHtml('');
157+
}

test/_specs.dart

+1
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,6 @@ var jasmine = jasmine_syntax.jasmine;
251251

252252
main() {
253253
jasmine_syntax.beforeEach(setUpInjector, priority:3);
254+
jasmine_syntax.afterEach(cleanUpAppRoot);
254255
jasmine_syntax.afterEach(tearDownInjector);
255256
}

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

+17-27
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,49 @@ 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
30+
..bind(FooController)..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-
}
47-
48-
it('should register and handle event', inject((TestBed _) {
49-
var e = compile(_,
33+
it('should register and handle event', inject((TestBed _, Application app) {
34+
var e = _.compile(
5035
'''<div foo>
5136
<div on-abc="ctrl.invoked=true;"></div>
5237
</div>''');
38+
document.body.append(app.element..append(e));
5339

5440
_.triggerEvent(e.querySelector('[on-abc]'), 'abc');
5541
expect(_.getScope(e).context['ctrl'].invoked).toEqual(true);
5642
}));
5743

58-
it('shoud register and handle event with long name', inject((TestBed _) {
59-
var e = compile(_,
44+
it('shoud register and handle event with long name', inject((TestBed _, Application app) {
45+
var e = _.compile(
6046
'''<div foo>
6147
<div on-my-new-event="ctrl.invoked=true;"></div>
6248
</div>''');
49+
document.body.append(app.element..append(e));
6350

6451
_.triggerEvent(e.querySelector('[on-my-new-event]'), 'myNewEvent');
6552
var fooScope = _.getScope(e);
6653
expect(fooScope.context['ctrl'].invoked).toEqual(true);
6754
}));
6855

69-
it('shoud have model updates applied correctly', inject((TestBed _) {
70-
var e = compile(_,
56+
it('should have model updates applied correctly', inject((TestBed _, Application app) {
57+
var e = _.compile(
7158
'''<div foo>
7259
<div on-abc='ctrl.description="new description";'>{{ctrl.description}}</div>
7360
</div>''');
61+
document.body.append(app.element..append(e));
7462
var el = document.querySelector('[on-abc]');
7563
el.dispatchEvent(new Event('abc'));
7664
_.rootScope.apply();
7765
expect(el.text).toEqual("new description");
7866
}));
7967

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

8372
microLeap();
8473

@@ -89,13 +78,14 @@ main() {
8978
expect(ctrl.invoked).toEqual(true);
9079
}));
9180

92-
it('shoud handle event within content only once', async(inject((TestBed _) {
93-
var e = compile(_,
81+
it('shoud handle event within content only once', async(inject((TestBed _, Application app) {
82+
var e = _.compile(
9483
'''<div foo>
9584
<bar>
9685
<div on-abc="ctrl.invoked=true;"></div>
9786
</bar>
9887
</div>''');
88+
document.body.append(app.element..append(e));
9989

10090
microLeap();
10191

0 commit comments

Comments
 (0)