feat(core): added support for detecting lifecycle events based on interfaces

This commit is contained in:
vsavkin
2015-05-27 08:08:14 -07:00
parent 2b6a653050
commit 30b6542fc8
12 changed files with 257 additions and 60 deletions

View File

@ -1,23 +0,0 @@
import {ddescribe, describe, it, iit, expect, beforeEach} from 'angular2/test_lib';
import {Directive, onChange} from 'angular2/src/core/annotations_impl/annotations';
export function main() {
describe("Directive", () => {
describe("lifecycle", () => {
it("should be false when no lifecycle specified", () => {
var d = new Directive();
expect(d.hasLifecycleHook(onChange)).toBe(false);
});
it("should be false when the lifecycle does not contain the hook", () => {
var d = new Directive({lifecycle: []});
expect(d.hasLifecycleHook(onChange)).toBe(false);
});
it("should be true otherwise", () => {
var d = new Directive({lifecycle: [onChange]});
expect(d.hasLifecycleHook(onChange)).toBe(true);
});
});
});
}

View File

@ -0,0 +1,75 @@
library angular2.test.core.compiler.directive_lifecycle_spec;
import 'package:angular2/test_lib.dart';
import 'package:angular2/angular2.dart';
import 'package:angular2/src/core/compiler/element_injector.dart';
main() {
describe('Create DirectiveMetadata', () {
describe('lifecycle', () {
metadata(type, annotation) => DirectiveBinding.createFromType(type, annotation).metadata;
describe("onChange", () {
it("should be true when the directive implements OnChange", () {
expect(metadata(DirectiveImplementingOnChange, new Directive()).callOnChange).toBe(true);
});
it("should be true when the lifecycle includes onChange", () {
expect(metadata(DirectiveNoHooks, new Directive(lifecycle: [onChange])).callOnChange).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive()).callOnChange).toBe(false);
});
it("should be false when empty lifecycle", () {
expect(metadata(DirectiveImplementingOnChange, new Directive(lifecycle: [])).callOnChange).toBe(false);
});
});
describe("onDestroy", () {
it("should be true when the directive implements OnDestroy", () {
expect(metadata(DirectiveImplementingOnDestroy, new Directive()).callOnDestroy).toBe(true);
});
it("should be true when the lifecycle includes onDestroy", () {
expect(metadata(DirectiveNoHooks, new Directive(lifecycle: [onDestroy])).callOnDestroy).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive()).callOnDestroy).toBe(false);
});
});
describe("onAllChangesDone", () {
it("should be true when the directive implements OnAllChangesDone", () {
expect(metadata(DirectiveImplementingOnAllChangesDone, new Directive()).callOnAllChangesDone).toBe(true);
});
it("should be true when the lifecycle includes onAllChangesDone", () {
expect(metadata(DirectiveNoHooks, new Directive(lifecycle: [onAllChangesDone])).callOnAllChangesDone).toBe(true);
});
it("should be false otherwise", () {
expect(metadata(DirectiveNoHooks, new Directive()).callOnAllChangesDone).toBe(false);
});
});
});
});
}
class DirectiveNoHooks {
}
class DirectiveImplementingOnChange implements OnChange {
onChange(_){}
}
class DirectiveImplementingOnDestroy implements OnDestroy {
onDestroy(){}
}
class DirectiveImplementingOnAllChangesDone implements OnAllChangesDone {
onAllChangesDone(){}
}

View File

@ -0,0 +1,100 @@
import {
AsyncTestCompleter,
beforeEach,
xdescribe,
ddescribe,
describe,
el,
expect,
iit,
inject,
IS_DARTIUM,
it,
SpyObject,
proxy
} from 'angular2/test_lib';
import {
Directive,
onChange,
onDestroy,
onAllChangesDone
} from 'angular2/src/core/annotations_impl/annotations';
import {DirectiveBinding} from 'angular2/src/core/compiler/element_injector';
export function main() {
describe('Create DirectiveMetadata', () => {
describe('lifecycle', () => {
function metadata(type, annotation) {
return DirectiveBinding.createFromType(type, annotation).metadata;
}
describe("onChange", () => {
it("should be true when the directive has the onChange method", () => {
expect(metadata(DirectiveWithOnChangeMethod, new Directive({})).callOnChange).toBe(true);
});
it("should be true when the lifecycle includes onChange", () => {
expect(metadata(DirectiveNoHooks, new Directive({lifecycle: [onChange]})).callOnChange)
.toBe(true);
});
it("should be false otherwise",
() => { expect(metadata(DirectiveNoHooks, new Directive()).callOnChange).toBe(false); });
it("should be false when empty lifecycle", () => {
expect(metadata(DirectiveWithOnChangeMethod, new Directive({lifecycle: []})).callOnChange)
.toBe(false);
});
});
describe("onDestroy", () => {
it("should be true when the directive has the onDestroy method", () => {
expect(metadata(DirectiveWithOnDestroyMethod, new Directive({})).callOnDestroy)
.toBe(true);
});
it("should be true when the lifecycle includes onDestroy", () => {
expect(metadata(DirectiveNoHooks, new Directive({lifecycle: [onDestroy]})).callOnDestroy)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new Directive()).callOnDestroy).toBe(false);
});
});
describe("onAllChangesDone", () => {
it("should be true when the directive has the onAllChangesDone method", () => {
expect(
metadata(DirectiveWithOnAllChangesDoneMethod, new Directive({})).callOnAllChangesDone)
.toBe(true);
});
it("should be true when the lifecycle includes onAllChangesDone", () => {
expect(metadata(DirectiveNoHooks, new Directive({lifecycle: [onAllChangesDone]}))
.callOnAllChangesDone)
.toBe(true);
});
it("should be false otherwise", () => {
expect(metadata(DirectiveNoHooks, new Directive()).callOnAllChangesDone).toBe(false);
});
});
});
});
}
class DirectiveNoHooks {}
class DirectiveWithOnChangeMethod {
onChange(_) {}
}
class DirectiveWithOnDestroyMethod {
onDestroy(_) {}
}
class DirectiveWithOnAllChangesDoneMethod {
onAllChangesDone(_) {}
}