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

refactor: Refactor test setup. #1537

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/mock/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ part 'mock_cache_register.dart';
*/
class AngularMockModule extends Module {
AngularMockModule() {
bind(MockApplication);
bind(Application, toImplementation: MockApplication);
bind(ExceptionHandler, toImplementation: RethrowExceptionHandler);
bind(TestBed);
bind(Probe);
bind(Logger);
bind(MockHttpBackend);
bind(CacheRegister, toImplementation: MockCacheRegister);
bind(Element, toValue: document.body);
bind(Node, toValue: document.body);
bind(Element, toFactory: (app) => app.element, inject: [MockApplication]);
bind(Node, inject: [Element]);
bind(HttpBackend, toInstanceOf: MOCK_HTTP_BACKEND_KEY);
bind(VmTurnZone, toFactory: () {
return new VmTurnZone()
Expand All @@ -78,3 +80,20 @@ class AngularMockModule extends Module {
bind(DefaultPlatformShim, toInstanceOf: MockWebPlatformShim);
}
}

class MockApplication extends Application {
var _element;

Element get element {
if (_element == null) {
_element = new DivElement()..attributes['ng-app'] = '';
}
return _element;
}

void destroyElement() {
_element = null;
}

Injector createInjector() => throw 'MockApplications can not create injectors';
}
15 changes: 14 additions & 1 deletion lib/mock/test_injection.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
library angular.mock.test_injection;

import 'dart:html';

import 'package:angular/application.dart';
import 'package:angular/application_factory.dart';
import 'package:angular/mock/module.dart';
import 'package:di/di.dart';
import 'dart:mirrors';
import 'module.dart';

_SpecInjector _currentSpecInjector = null;

Expand Down Expand Up @@ -61,7 +65,7 @@ class _SpecInjector {
}
}

reset() {
void reset() {
injector = null;
injectorCreateLocation = null;
}
Expand Down Expand Up @@ -161,3 +165,12 @@ void setUpInjector() {
void tearDownInjector() {
_currentSpecInjector = null;
}

void cleanUpAppRoot() {
if (_currentSpecInjector.injector != null) {
var app = _currentSpecInjector.injector.get(Application);
assert(app is MockApplication);
app.destroyElement();
}
document.body.setInnerHtml('');
}
1 change: 1 addition & 0 deletions test/_specs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ _removeNgBinding(node) {

main() {
gns.beforeEach(setUpInjector, priority:3);
gns.afterEach(cleanUpAppRoot);
gns.afterEach(tearDownInjector);

gns.guinnessEnableHtmlMatchers();
Expand Down
4 changes: 0 additions & 4 deletions test/animate/ng_animate_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ main() {
});
});

afterEach(() {
tearDownInjector();
});

it('should control animations on elements', () {
_.compile('<div ng-animate="never"><div></div></div>');
_.rootScope.apply();
Expand Down
34 changes: 11 additions & 23 deletions test/core_dom/event_handler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,37 @@ main() {
describe('EventHandler', () {
Element ngAppElement;
beforeEachModule((Module module) {
ngAppElement = new DivElement()..attributes['ng-app'] = '';
module..bind(BarComponent);
module..bind(Node, toValue: ngAppElement);
document.body.append(ngAppElement);
});

afterEach(() {
ngAppElement.remove();
ngAppElement = null;
});

compile(_, html) {
ngAppElement.setInnerHtml(html, treeSanitizer: new NullTreeSanitizer());
_.compile(ngAppElement);
return ngAppElement.firstChild;
}

it('should register and handle event', (TestBed _) {
var e = compile(_,
it('should register and handle event', (TestBed _, Application app) {
var e = _.compile(
'''<div on-abc="invoked=true;"></div>''');

_.triggerEvent(e, 'abc');
expect(_.rootScope.context['invoked']).toEqual(true);
});

it('shoud register and handle event with long name', (TestBed _) {
var e = compile(_,
it('shoud register and handle event with long name', (TestBed _, Application app) {
var e = _.compile(
'''<div on-my-new-event="invoked=true;"></div>''');

_.triggerEvent(e, 'my-new-event');
expect(_.rootScope.context['invoked']).toEqual(true);
});

it('shoud have model updates applied correctly', (TestBed _) {
var e = compile(_,
it('should have model updates applied correctly', (TestBed _, Application app) {
var e = _.compile(
'''<div on-abc='description="new description";'>{{description}}</div>''');
e.dispatchEvent(new Event('abc'));
_.rootScope.apply();
expect(e.text).toEqual("new description");
});

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

microLeap();

Expand All @@ -73,8 +61,8 @@ main() {
expect(ctrl.invoked).toEqual(true);
}));

it('shoud handle event within content only once', async((TestBed _) {
var e = compile(_,
it('shoud handle event within content only once', async((TestBed _, Application app) {
var e = _.compile(
''' <bar>
<div on-abc="invoked=true;"></div>
</bar>
Expand Down
Loading