forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noLifeCycleCallRule.spec.ts
116 lines (102 loc) · 3.4 KB
/
noLifeCycleCallRule.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
import { lifecycleHooksMethods, LifecycleHooksMethods } from '../src/noLifeCycleCallRule';
import { assertAnnotated, assertSuccess } from './testHelper';
type Metadata = 'Component' | 'Directive' | 'Injectable' | 'Pipe';
type MetadataPair = {
[key in Metadata]: typeof lifecycleHooksMethods
};
type MetadataFakePair = {
[key in Metadata]?: Set<string>
};
const className = 'Test';
const ruleName = 'no-life-cycle-call';
const metadataPairs: MetadataPair = {
Component: lifecycleHooksMethods,
Directive: lifecycleHooksMethods,
Injectable: new Set<LifecycleHooksMethods>([
'ngOnDestroy'
]),
Pipe: new Set<LifecycleHooksMethods>([
'ngOnDestroy'
])
};
const metadataKeys = Object.keys(metadataPairs);
const prefix = 'prefix';
const suffix = 'Suffix';
const metadataFakePairs: MetadataFakePair = {};
for (const metadataKey of metadataKeys) {
metadataFakePairs[metadataKey] = new Set<string>();
metadataPairs[metadataKey].forEach(lifecycleHookMethod => {
metadataFakePairs[metadataKey]
.add(`${prefix}${lifecycleHookMethod}`)
.add(`${lifecycleHookMethod}${suffix}`)
.add(`${prefix}${lifecycleHookMethod}${suffix}`);
});
}
describe(ruleName, () => {
describe('failure', () => {
for (const metadataKey of metadataKeys) {
describe(metadataKey, () => {
metadataPairs[metadataKey].forEach(lifecycleHookMethod => {
const lifecycleHookMethodCall = `this.${lifecycleHookMethod}()`;
const totalTildes = '~'.repeat(lifecycleHookMethodCall.length);
const source = `
@${metadataKey}()
class ${className} implements ${lifecycleHookMethod.slice(2)} {
${lifecycleHookMethod}() { }
${className.toLowerCase()}() {
${lifecycleHookMethodCall}
${totalTildes}
}
}
`;
it(`should fail when explicitly calling ${lifecycleHookMethodCall}`, () => {
assertAnnotated({
ruleName,
message: `Avoid explicitly calls to lifecycle hooks in class "${className}"`,
source
});
});
});
});
}
});
describe('success', () => {
for (const metadataKey of metadataKeys) {
describe(metadataKey, () => {
metadataFakePairs[metadataKey].forEach(fakeMethod => {
const source = `
@${metadataKey}()
class ${className} {
${fakeMethod}() { }
${className.toLowerCase()}() {
this.${fakeMethod}();
}
}
`;
it(`should pass when calling ${fakeMethod} method`, () => {
assertSuccess(ruleName, source);
});
});
});
}
// call super lifecycle hook
for (const metadataKey of metadataKeys) {
describe(metadataKey, () => {
metadataPairs[metadataKey].forEach(lifecycleHookMethod => {
const lifecycleHookMethodCall = `super.${lifecycleHookMethod}()`;
const source = `
@${metadataKey}()
class ${className} implements ${lifecycleHookMethod.slice(2)} {
${lifecycleHookMethod}() {
${lifecycleHookMethodCall}
}
}
`;
it(`should pass when explicitly calling ${lifecycleHookMethodCall}`, () => {
assertSuccess(ruleName, source);
});
});
});
}
});
});