angular-cn/modules/@angular/compiler/test/ng_module_resolver_spec.ts
Tobias Bosch f5c8e0989d feat(core): properly support inheritance
## Inheritance Semantics:

Decorators:
1) list the decorators of the class and its parents in the ancestor first order
2) only use the last decorator of each kind (e.g. @Component / ...)

Constructor parameters:
If a class inherits from a parent class and does not declare
a constructor, it inherits the parent class constructor,
and with it the parameter metadata of that parent class.

Lifecycle hooks:
Follow the normal class inheritance model,
i.e. lifecycle hooks of parent classes will be called
even if the method is not overwritten in the child class.

## Example

E.g. the following is a valid use of inheritance and it will
also inherit all metadata:

```
@Directive({selector: 'someDir'})
class ParentDirective {
  constructor(someDep: SomeDep) {}

  ngOnInit() {}
}

class ChildDirective extends ParentDirective {}
```

Closes #11606
Closes #12892
2016-11-28 14:12:12 -08:00

71 lines
1.8 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {NgModuleResolver} from '@angular/compiler/src/ng_module_resolver';
import {NgModule} from '@angular/core/src/metadata';
import {stringify} from '../src/facade/lang';
class SomeClass1 {}
class SomeClass2 {}
class SomeClass3 {}
class SomeClass4 {}
class SomeClass5 {}
@NgModule({
declarations: [SomeClass1],
imports: [SomeClass2],
exports: [SomeClass3],
providers: [SomeClass4],
entryComponents: [SomeClass5]
})
class SomeModule {
}
class SimpleClass {}
export function main() {
describe('NgModuleResolver', () => {
let resolver: NgModuleResolver;
beforeEach(() => { resolver = new NgModuleResolver(); });
it('should read out the metadata from the class', () => {
const moduleMetadata = resolver.resolve(SomeModule);
expect(moduleMetadata).toEqual(new NgModule({
declarations: [SomeClass1],
imports: [SomeClass2],
exports: [SomeClass3],
providers: [SomeClass4],
entryComponents: [SomeClass5]
}));
});
it('should throw when simple class has no NgModule decorator', () => {
expect(() => resolver.resolve(SimpleClass))
.toThrowError(`No NgModule metadata found for '${stringify(SimpleClass)}'.`);
});
it('should support inheriting the metadata', function() {
@NgModule({id: 'p'})
class Parent {
}
class ChildNoDecorator extends Parent {}
@NgModule({id: 'c'})
class ChildWithDecorator extends Parent {
}
expect(resolver.resolve(ChildNoDecorator)).toEqual(new NgModule({id: 'p'}));
expect(resolver.resolve(ChildWithDecorator)).toEqual(new NgModule({id: 'c'}));
});
});
}