2015-09-14 18:59:09 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
xdescribe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach,
|
|
|
|
AsyncTestCompleter,
|
|
|
|
inject,
|
2015-11-02 19:03:42 -05:00
|
|
|
beforeEachProviders
|
2015-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2015-11-06 20:34:07 -05:00
|
|
|
import {stringify} from 'angular2/src/facade/lang';
|
2015-11-05 17:07:57 -05:00
|
|
|
import {RuntimeMetadataResolver} from 'angular2/src/compiler/runtime_metadata';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {LifecycleHooks, LIFECYCLE_HOOKS_VALUES} from 'angular2/src/core/linker/interfaces';
|
2015-09-14 18:59:09 -04:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
View,
|
|
|
|
Directive,
|
|
|
|
ViewEncapsulation,
|
|
|
|
ChangeDetectionStrategy,
|
|
|
|
OnChanges,
|
|
|
|
OnInit,
|
|
|
|
DoCheck,
|
|
|
|
OnDestroy,
|
|
|
|
AfterContentInit,
|
|
|
|
AfterContentChecked,
|
|
|
|
AfterViewInit,
|
2015-09-23 14:43:31 -04:00
|
|
|
AfterViewChecked,
|
2015-11-02 19:03:42 -05:00
|
|
|
SimpleChange,
|
|
|
|
provide
|
2015-09-14 18:59:09 -04:00
|
|
|
} from 'angular2/core';
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
import {TEST_PROVIDERS} from './test_bindings';
|
2015-11-05 17:07:57 -05:00
|
|
|
import {MODULE_SUFFIX} from 'angular2/src/compiler/util';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {IS_DART} from 'angular2/src/facade/lang';
|
2015-11-09 17:33:22 -05:00
|
|
|
import {PLATFORM_DIRECTIVES} from 'angular2/src/core/platform_directives_and_pipes';
|
2015-09-14 18:59:09 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('RuntimeMetadataResolver', () => {
|
2015-11-02 19:03:42 -05:00
|
|
|
beforeEachProviders(() => TEST_PROVIDERS);
|
2015-09-14 18:59:09 -04:00
|
|
|
|
|
|
|
describe('getMetadata', () => {
|
|
|
|
it('should read metadata',
|
|
|
|
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
|
2015-12-02 13:35:51 -05:00
|
|
|
var meta = resolver.getDirectiveMetadata(ComponentWithEverything);
|
2015-09-14 18:59:09 -04:00
|
|
|
expect(meta.selector).toEqual('someSelector');
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(meta.exportAs).toEqual('someExportAs');
|
2015-09-14 18:59:09 -04:00
|
|
|
expect(meta.isComponent).toBe(true);
|
|
|
|
expect(meta.dynamicLoadable).toBe(true);
|
|
|
|
expect(meta.type.runtime).toBe(ComponentWithEverything);
|
|
|
|
expect(meta.type.name).toEqual(stringify(ComponentWithEverything));
|
2015-10-01 13:07:49 -04:00
|
|
|
expect(meta.type.moduleUrl).toEqual(`package:someModuleId${MODULE_SUFFIX}`);
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(meta.lifecycleHooks).toEqual(LIFECYCLE_HOOKS_VALUES);
|
|
|
|
expect(meta.changeDetection).toBe(ChangeDetectionStrategy.CheckAlways);
|
2015-09-30 23:59:23 -04:00
|
|
|
expect(meta.inputs).toEqual({'someProp': 'someProp'});
|
|
|
|
expect(meta.outputs).toEqual({'someEvent': 'someEvent'});
|
2015-09-18 13:33:23 -04:00
|
|
|
expect(meta.hostListeners).toEqual({'someHostListener': 'someHostListenerExpr'});
|
|
|
|
expect(meta.hostProperties).toEqual({'someHostProp': 'someHostPropExpr'});
|
|
|
|
expect(meta.hostAttributes).toEqual({'someHostAttr': 'someHostAttrValue'});
|
2015-09-14 18:59:09 -04:00
|
|
|
expect(meta.template.encapsulation).toBe(ViewEncapsulation.Emulated);
|
|
|
|
expect(meta.template.styles).toEqual(['someStyle']);
|
|
|
|
expect(meta.template.styleUrls).toEqual(['someStyleUrl']);
|
|
|
|
expect(meta.template.template).toEqual('someTemplate');
|
|
|
|
expect(meta.template.templateUrl).toEqual('someTemplateUrl');
|
|
|
|
}));
|
|
|
|
|
2015-10-01 13:07:49 -04:00
|
|
|
it('should use the moduleUrl from the reflector if none is given',
|
2015-09-14 18:59:09 -04:00
|
|
|
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
|
2015-12-02 13:35:51 -05:00
|
|
|
var value: string =
|
|
|
|
resolver.getDirectiveMetadata(ComponentWithoutModuleId).type.moduleUrl;
|
2015-10-01 13:07:49 -04:00
|
|
|
var expectedEndValue =
|
2015-11-05 17:07:57 -05:00
|
|
|
IS_DART ? 'base/dist/dart/angular2/test/compiler/runtime_metadata_spec.dart' : './';
|
2015-12-13 20:35:33 -05:00
|
|
|
expect(value.endsWith(expectedEndValue)).toBe(true);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getViewDirectivesMetadata', () => {
|
|
|
|
|
|
|
|
it('should return the directive metadatas',
|
|
|
|
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
|
|
|
|
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
|
2015-12-02 13:35:51 -05:00
|
|
|
.toEqual([resolver.getDirectiveMetadata(SomeDirective)]);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
2015-11-02 19:03:42 -05:00
|
|
|
|
2015-11-09 17:33:22 -05:00
|
|
|
describe("platform directives", () => {
|
|
|
|
beforeEachProviders(() => [provide(PLATFORM_DIRECTIVES, {useValue: [ADirective]})]);
|
2015-11-02 19:03:42 -05:00
|
|
|
|
2015-11-09 17:33:22 -05:00
|
|
|
it('should include platform directives when available',
|
2015-11-02 19:03:42 -05:00
|
|
|
inject([RuntimeMetadataResolver], (resolver: RuntimeMetadataResolver) => {
|
|
|
|
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
|
2015-12-02 13:35:51 -05:00
|
|
|
.toEqual([
|
|
|
|
resolver.getDirectiveMetadata(ADirective),
|
|
|
|
resolver.getDirectiveMetadata(SomeDirective)
|
|
|
|
]);
|
2015-11-02 19:03:42 -05:00
|
|
|
}));
|
|
|
|
});
|
2015-09-14 18:59:09 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-02 19:03:42 -05:00
|
|
|
@Directive({selector: 'a-directive'})
|
|
|
|
class ADirective {
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:59:09 -04:00
|
|
|
@Directive({selector: 'someSelector'})
|
2015-12-13 20:35:33 -05:00
|
|
|
class SomeDirective {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'someComponent', template: ''})
|
|
|
|
class ComponentWithoutModuleId {
|
2015-09-14 18:59:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'someSelector',
|
2015-09-30 23:59:23 -04:00
|
|
|
inputs: ['someProp'],
|
|
|
|
outputs: ['someEvent'],
|
2015-09-14 18:59:09 -04:00
|
|
|
host: {
|
|
|
|
'[someHostProp]': 'someHostPropExpr',
|
|
|
|
'(someHostListener)': 'someHostListenerExpr',
|
|
|
|
'someHostAttr': 'someHostAttrValue'
|
|
|
|
},
|
2015-09-18 13:33:23 -04:00
|
|
|
exportAs: 'someExportAs',
|
2015-09-14 18:59:09 -04:00
|
|
|
moduleId: 'someModuleId',
|
|
|
|
changeDetection: ChangeDetectionStrategy.CheckAlways
|
|
|
|
})
|
|
|
|
@View({
|
|
|
|
template: 'someTemplate',
|
|
|
|
templateUrl: 'someTemplateUrl',
|
|
|
|
encapsulation: ViewEncapsulation.Emulated,
|
|
|
|
styles: ['someStyle'],
|
|
|
|
styleUrls: ['someStyleUrl'],
|
2015-12-13 20:35:33 -05:00
|
|
|
directives: [SomeDirective]
|
2015-09-14 18:59:09 -04:00
|
|
|
})
|
|
|
|
class ComponentWithEverything implements OnChanges,
|
|
|
|
OnInit, DoCheck, OnDestroy, AfterContentInit, AfterContentChecked, AfterViewInit,
|
|
|
|
AfterViewChecked {
|
refactor(lifecycle): prefix lifecycle methods with "ng"
BREAKING CHANGE:
Previously, components that would implement lifecycle interfaces would include methods
like "onChanges" or "afterViewInit." Given that components were at risk of using such
names without realizing that Angular would call the methods at different points of
the component lifecycle. This change adds an "ng" prefix to all lifecycle hook methods,
far reducing the risk of an accidental name collision.
To fix, just rename these methods:
* onInit
* onDestroy
* doCheck
* onChanges
* afterContentInit
* afterContentChecked
* afterViewInit
* afterViewChecked
* _Router Hooks_
* onActivate
* onReuse
* onDeactivate
* canReuse
* canDeactivate
To:
* ngOnInit,
* ngOnDestroy,
* ngDoCheck,
* ngOnChanges,
* ngAfterContentInit,
* ngAfterContentChecked,
* ngAfterViewInit,
* ngAfterViewChecked
* _Router Hooks_
* routerOnActivate
* routerOnReuse
* routerOnDeactivate
* routerCanReuse
* routerCanDeactivate
The names of lifecycle interfaces and enums have not changed, though interfaces
have been updated to reflect the new method names.
Closes #5036
2015-11-16 20:04:36 -05:00
|
|
|
ngOnChanges(changes: {[key: string]: SimpleChange}): void {}
|
|
|
|
ngOnInit(): void {}
|
|
|
|
ngDoCheck(): void {}
|
|
|
|
ngOnDestroy(): void {}
|
|
|
|
ngAfterContentInit(): void {}
|
|
|
|
ngAfterContentChecked(): void {}
|
|
|
|
ngAfterViewInit(): void {}
|
|
|
|
ngAfterViewChecked(): void {}
|
2015-09-14 18:59:09 -04:00
|
|
|
}
|