2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
import {hasLifecycleHook as hasLifecycleHookImpl, LifecycleHooks as Hooks} from '@angular/compiler/src/lifecycle_reflector';
|
2016-10-07 20:36:08 -04:00
|
|
|
import {SimpleChanges} from '@angular/core';
|
2017-08-16 12:00:03 -04:00
|
|
|
import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector';
|
2017-05-18 16:46:51 -04:00
|
|
|
|
|
|
|
function hasLifecycleHook(hook: Hooks, directive: any): boolean {
|
|
|
|
return hasLifecycleHookImpl(new JitReflector(), hook, directive);
|
|
|
|
}
|
2015-05-27 11:08:14 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-09-12 22:14:17 -04:00
|
|
|
describe('Create Directive', () => {
|
2015-05-27 11:08:14 -04:00
|
|
|
describe('lifecycle', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngOnChanges', () => {
|
|
|
|
it('should be true when the directive has the ngOnChanges method', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.OnChanges, DirectiveWithOnChangesMethod)).toBe(true);
|
2015-05-27 11:08:14 -04:00
|
|
|
});
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should be false otherwise', () => {
|
|
|
|
expect(hasLifecycleHook(Hooks.OnChanges, DirectiveNoHooks)).toBe(false);
|
|
|
|
});
|
2015-05-27 11:08:14 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngOnDestroy', () => {
|
|
|
|
it('should be true when the directive has the ngOnDestroy method', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.OnDestroy, DirectiveWithOnDestroyMethod)).toBe(true);
|
2015-05-27 11:08:14 -04:00
|
|
|
});
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should be false otherwise', () => {
|
|
|
|
expect(hasLifecycleHook(Hooks.OnDestroy, DirectiveNoHooks)).toBe(false);
|
|
|
|
});
|
2015-05-27 11:08:14 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngOnInit', () => {
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should be true when the directive has the ngOnInit method', () => {
|
|
|
|
expect(hasLifecycleHook(Hooks.OnInit, DirectiveWithOnInitMethod)).toBe(true);
|
|
|
|
});
|
2015-05-27 13:14:37 -04:00
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should be false otherwise', () => {
|
|
|
|
expect(hasLifecycleHook(Hooks.OnInit, DirectiveNoHooks)).toBe(false);
|
|
|
|
});
|
2015-05-27 13:14:37 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngDoCheck', () => {
|
|
|
|
it('should be true when the directive has the ngDoCheck method', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.DoCheck, DirectiveWithOnCheckMethod)).toBe(true);
|
2015-05-27 13:14:37 -04:00
|
|
|
});
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should be false otherwise', () => {
|
|
|
|
expect(hasLifecycleHook(Hooks.DoCheck, DirectiveNoHooks)).toBe(false);
|
|
|
|
});
|
2015-05-27 13:14:37 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngAfterContentInit', () => {
|
|
|
|
it('should be true when the directive has the ngAfterContentInit method', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.AfterContentInit, DirectiveWithAfterContentInitMethod))
|
2015-08-28 21:11:04 -04:00
|
|
|
.toBe(true);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should be false otherwise', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.AfterContentInit, DirectiveNoHooks)).toBe(false);
|
2015-08-28 21:11:04 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngAfterContentChecked', () => {
|
|
|
|
it('should be true when the directive has the ngAfterContentChecked method', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(
|
|
|
|
hasLifecycleHook(Hooks.AfterContentChecked, DirectiveWithAfterContentCheckedMethod))
|
2015-05-27 11:08:14 -04:00
|
|
|
.toBe(true);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should be false otherwise', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.AfterContentChecked, DirectiveNoHooks)).toBe(false);
|
2015-05-27 11:08:14 -04:00
|
|
|
});
|
|
|
|
});
|
2015-08-28 21:11:04 -04:00
|
|
|
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngAfterViewInit', () => {
|
|
|
|
it('should be true when the directive has the ngAfterViewInit method', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.AfterViewInit, DirectiveWithAfterViewInitMethod))
|
2015-08-28 21:11:04 -04:00
|
|
|
.toBe(true);
|
|
|
|
});
|
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
it('should be false otherwise', () => {
|
|
|
|
expect(hasLifecycleHook(Hooks.AfterViewInit, DirectiveNoHooks)).toBe(false);
|
|
|
|
});
|
2015-08-28 21:11:04 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ngAfterViewChecked', () => {
|
|
|
|
it('should be true when the directive has the ngAfterViewChecked method', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.AfterViewChecked, DirectiveWithAfterViewCheckedMethod))
|
2015-08-28 21:11:04 -04:00
|
|
|
.toBe(true);
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should be false otherwise', () => {
|
2016-10-07 20:36:08 -04:00
|
|
|
expect(hasLifecycleHook(Hooks.AfterViewChecked, DirectiveNoHooks)).toBe(false);
|
2015-08-28 21:11:04 -04:00
|
|
|
});
|
|
|
|
});
|
2015-05-27 11:08:14 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveNoHooks {}
|
|
|
|
|
2015-08-28 00:19:56 -04:00
|
|
|
class DirectiveWithOnChangesMethod {
|
2016-10-07 20:36:08 -04:00
|
|
|
ngOnChanges(_: SimpleChanges) {}
|
2015-05-27 11:08:14 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 13:14:37 -04:00
|
|
|
class DirectiveWithOnInitMethod {
|
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
|
|
|
ngOnInit() {}
|
2015-05-27 13:14:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveWithOnCheckMethod {
|
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
|
|
|
ngDoCheck() {}
|
2015-05-27 13:14:37 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 11:08:14 -04:00
|
|
|
class DirectiveWithOnDestroyMethod {
|
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
|
|
|
ngOnDestroy() {}
|
2015-05-27 11:08:14 -04:00
|
|
|
}
|
|
|
|
|
2015-08-28 21:11:04 -04:00
|
|
|
class DirectiveWithAfterContentInitMethod {
|
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
|
|
|
ngAfterContentInit() {}
|
2015-08-28 21:11:04 -04:00
|
|
|
}
|
|
|
|
|
2015-08-28 00:19:56 -04:00
|
|
|
class DirectiveWithAfterContentCheckedMethod {
|
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
|
|
|
ngAfterContentChecked() {}
|
2015-07-04 09:04:50 -04:00
|
|
|
}
|
2015-08-28 21:11:04 -04:00
|
|
|
|
|
|
|
class DirectiveWithAfterViewInitMethod {
|
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
|
|
|
ngAfterViewInit() {}
|
2015-08-28 21:11:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class DirectiveWithAfterViewCheckedMethod {
|
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
|
|
|
ngAfterViewChecked() {}
|
2015-08-28 21:11:04 -04:00
|
|
|
}
|