forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathdirective_lifecycle_spec.ts
141 lines (113 loc) · 4.39 KB
/
directive_lifecycle_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* @license
* Copyright Google LLC 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 {hasLifecycleHook as hasLifecycleHookImpl, LifecycleHooks as Hooks} from '@angular/compiler/src/lifecycle_reflector';
import {SimpleChanges} from '@angular/core';
import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_reflector';
function hasLifecycleHook(hook: Hooks, directive: any): boolean {
return hasLifecycleHookImpl(new JitReflector(), hook, directive);
}
{
describe('Create Directive', () => {
describe('lifecycle', () => {
describe('ngOnChanges', () => {
it('should be true when the directive has the ngOnChanges method', () => {
expect(hasLifecycleHook(Hooks.OnChanges, DirectiveWithOnChangesMethod)).toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.OnChanges, DirectiveNoHooks)).toBe(false);
});
});
describe('ngOnDestroy', () => {
it('should be true when the directive has the ngOnDestroy method', () => {
expect(hasLifecycleHook(Hooks.OnDestroy, DirectiveWithOnDestroyMethod)).toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.OnDestroy, DirectiveNoHooks)).toBe(false);
});
});
describe('ngOnInit', () => {
it('should be true when the directive has the ngOnInit method', () => {
expect(hasLifecycleHook(Hooks.OnInit, DirectiveWithOnInitMethod)).toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.OnInit, DirectiveNoHooks)).toBe(false);
});
});
describe('ngDoCheck', () => {
it('should be true when the directive has the ngDoCheck method', () => {
expect(hasLifecycleHook(Hooks.DoCheck, DirectiveWithOnCheckMethod)).toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.DoCheck, DirectiveNoHooks)).toBe(false);
});
});
describe('ngAfterContentInit', () => {
it('should be true when the directive has the ngAfterContentInit method', () => {
expect(hasLifecycleHook(Hooks.AfterContentInit, DirectiveWithAfterContentInitMethod))
.toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.AfterContentInit, DirectiveNoHooks)).toBe(false);
});
});
describe('ngAfterContentChecked', () => {
it('should be true when the directive has the ngAfterContentChecked method', () => {
expect(
hasLifecycleHook(Hooks.AfterContentChecked, DirectiveWithAfterContentCheckedMethod))
.toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.AfterContentChecked, DirectiveNoHooks)).toBe(false);
});
});
describe('ngAfterViewInit', () => {
it('should be true when the directive has the ngAfterViewInit method', () => {
expect(hasLifecycleHook(Hooks.AfterViewInit, DirectiveWithAfterViewInitMethod))
.toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.AfterViewInit, DirectiveNoHooks)).toBe(false);
});
});
describe('ngAfterViewChecked', () => {
it('should be true when the directive has the ngAfterViewChecked method', () => {
expect(hasLifecycleHook(Hooks.AfterViewChecked, DirectiveWithAfterViewCheckedMethod))
.toBe(true);
});
it('should be false otherwise', () => {
expect(hasLifecycleHook(Hooks.AfterViewChecked, DirectiveNoHooks)).toBe(false);
});
});
});
});
}
class DirectiveNoHooks {}
class DirectiveWithOnChangesMethod {
ngOnChanges(_: SimpleChanges) {}
}
class DirectiveWithOnInitMethod {
ngOnInit() {}
}
class DirectiveWithOnCheckMethod {
ngDoCheck() {}
}
class DirectiveWithOnDestroyMethod {
ngOnDestroy() {}
}
class DirectiveWithAfterContentInitMethod {
ngAfterContentInit() {}
}
class DirectiveWithAfterContentCheckedMethod {
ngAfterContentChecked() {}
}
class DirectiveWithAfterViewInitMethod {
ngAfterViewInit() {}
}
class DirectiveWithAfterViewCheckedMethod {
ngAfterViewChecked() {}
}