2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
xdescribe,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
expect,
|
|
|
|
beforeEach,
|
|
|
|
afterEach,
|
|
|
|
inject,
|
|
|
|
beforeEachProviders
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core/testing/testing_internal';
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {IS_DART, stringify} from '../src/facade/lang';
|
|
|
|
import {CompileMetadataResolver} from '../src/metadata_resolver';
|
|
|
|
import {LifecycleHooks, LIFECYCLE_HOOKS_VALUES} from '@angular/core/src/metadata/lifecycle_hooks';
|
2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
Directive,
|
|
|
|
ViewEncapsulation,
|
|
|
|
ChangeDetectionStrategy,
|
|
|
|
OnChanges,
|
|
|
|
OnInit,
|
|
|
|
DoCheck,
|
|
|
|
OnDestroy,
|
|
|
|
AfterContentInit,
|
|
|
|
AfterContentChecked,
|
|
|
|
AfterViewInit,
|
|
|
|
AfterViewChecked,
|
|
|
|
SimpleChange,
|
|
|
|
provide
|
2016-04-28 20:50:03 -04:00
|
|
|
} from '@angular/core';
|
2015-09-14 18:59:09 -04:00
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
import {TEST_PROVIDERS} from './test_bindings';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {MODULE_SUFFIX} from '@angular/compiler/src/util';
|
|
|
|
import {PLATFORM_DIRECTIVES} from '@angular/core/src/platform_directives_and_pipes';
|
2016-02-18 13:53:21 -05:00
|
|
|
import {MalformedStylesComponent} from './metadata_resolver_fixture';
|
2015-09-14 18:59:09 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-02-18 13:53:21 -05:00
|
|
|
describe('CompileMetadataResolver', () => {
|
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',
|
2016-02-18 13:53:21 -05:00
|
|
|
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
|
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.type.runtime).toBe(ComponentWithEverything);
|
|
|
|
expect(meta.type.name).toEqual(stringify(ComponentWithEverything));
|
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');
|
2016-04-29 00:54:02 -04:00
|
|
|
expect(meta.template.baseUrl).toEqual(`package:someModuleId${MODULE_SUFFIX}`);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
|
|
|
|
2015-10-01 13:07:49 -04:00
|
|
|
it('should use the moduleUrl from the reflector if none is given',
|
2016-02-18 13:53:21 -05:00
|
|
|
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
|
2015-12-02 13:35:51 -05:00
|
|
|
var value: string =
|
2016-04-29 00:54:02 -04:00
|
|
|
resolver.getDirectiveMetadata(ComponentWithoutModuleId).template.baseUrl;
|
2016-01-06 17:13:44 -05:00
|
|
|
var expectedEndValue =
|
2016-02-18 13:53:21 -05:00
|
|
|
IS_DART ? 'test/compiler/metadata_resolver_spec.dart' : './ComponentWithoutModuleId';
|
2015-12-13 20:35:33 -05:00
|
|
|
expect(value.endsWith(expectedEndValue)).toBe(true);
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
2016-03-09 17:55:27 -05:00
|
|
|
|
|
|
|
it('should throw when metadata is incorrectly typed',
|
2016-02-18 13:53:21 -05:00
|
|
|
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
|
2016-03-09 17:55:27 -05:00
|
|
|
if (!IS_DART) {
|
|
|
|
expect(() => resolver.getDirectiveMetadata(MalformedStylesComponent))
|
|
|
|
.toThrowError(`Expected 'styles' to be an array of strings.`);
|
|
|
|
}
|
|
|
|
}));
|
2015-09-14 18:59:09 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getViewDirectivesMetadata', () => {
|
|
|
|
|
|
|
|
it('should return the directive metadatas',
|
2016-02-18 13:53:21 -05:00
|
|
|
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
|
2015-09-14 18:59:09 -04:00
|
|
|
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
|
2015-12-15 19:38:27 -05:00
|
|
|
.toContain(resolver.getDirectiveMetadata(SomeDirective));
|
2015-09-14 18:59:09 -04:00
|
|
|
}));
|
2015-11-02 19:03:42 -05:00
|
|
|
|
2016-04-12 12:40:37 -04:00
|
|
|
describe("platform directives", () => {
|
2015-12-15 19:38:27 -05:00
|
|
|
beforeEachProviders(
|
|
|
|
() => [provide(PLATFORM_DIRECTIVES, {useValue: [ADirective], multi: true})]);
|
2015-11-02 19:03:42 -05:00
|
|
|
|
2015-11-09 17:33:22 -05:00
|
|
|
it('should include platform directives when available',
|
2016-02-18 13:53:21 -05:00
|
|
|
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
|
2015-11-02 19:03:42 -05:00
|
|
|
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
|
2015-12-15 19:38:27 -05:00
|
|
|
.toContain(resolver.getDirectiveMetadata(ADirective));
|
|
|
|
expect(resolver.getViewDirectivesMetadata(ComponentWithEverything))
|
|
|
|
.toContain(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',
|
2016-03-08 16:36:48 -05:00
|
|
|
changeDetection: ChangeDetectionStrategy.CheckAlways,
|
2015-09-14 18:59:09 -04:00
|
|
|
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
|
|
|
}
|