/
missing_control_flow_directive_spec.ts
149 lines (143 loc) Β· 6.63 KB
/
missing_control_flow_directive_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
142
143
144
145
146
147
148
149
/**
* @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 ts from 'typescript';
import {ErrorCode, ngErrorCode} from '../../../../../diagnostics';
import {absoluteFrom, getSourceFileOrError} from '../../../../../file_system';
import {runInEachFileSystem} from '../../../../../file_system/testing';
import {getSourceCodeForDiagnostic} from '../../../../../testing';
import {getClass, setup} from '../../../../testing';
import {factory as missingControlFlowDirectiveCheck, KNOWN_CONTROL_FLOW_DIRECTIVES} from '../../../checks/missing_control_flow_directive';
import {ExtendedTemplateCheckerImpl} from '../../../src/extended_template_checker';
runInEachFileSystem(() => {
describe('MissingControlFlowDirectiveCheck', () => {
KNOWN_CONTROL_FLOW_DIRECTIVES.forEach((correspondingImport, directive) => {
['div', 'ng-template', 'ng-container', 'ng-content'].forEach(element => {
it(`should produce a warning when the '${directive}' directive is not imported ` +
`(when used on the '${element}' element)`,
() => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<${element} *${directive}="exp"></${element}>`,
},
declarations: [{
name: 'TestCmp',
type: 'directive',
selector: `[test-cmp]`,
isStandalone: true,
}]
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
expect(diags.length).toBe(1);
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
expect(diags[0].code).toBe(ngErrorCode(ErrorCode.MISSING_CONTROL_FLOW_DIRECTIVE));
expect(diags[0].messageText)
.toBe(
`The \`*${directive}\` directive was used in the template, ` +
`but neither the \`${
correspondingImport}\` directive nor the \`CommonModule\` was imported. ` +
`Please make sure that either the \`${
correspondingImport}\` directive or the \`CommonModule\` ` +
`is included in the \`@Component.imports\` array of this component.`);
expect(getSourceCodeForDiagnostic(diags[0])).toBe(directive);
});
it(`should *not* produce a warning when the '${directive}' directive is not imported ` +
`into a non-standalone component scope (when used on the '${element}' element)`,
() => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<${element} *${directive}="exp"></${element}>`,
},
declarations: [{
name: 'TestCmp',
type: 'directive',
selector: `[test-cmp]`,
isStandalone: false,
}]
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
// No diagnostic messages are expected.
expect(diags.length).toBe(0);
});
it(`should *not* produce a warning when the '${directive}' directive is imported ` +
`(when used on the '${element}' element)`,
() => {
const className = directive.charAt(0).toUpperCase() + directive.substr(1);
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<${element} *${directive}="exp"></${element}>`,
},
source: `
export class TestCmp {}
export class ${className} {}
`,
declarations: [
{
type: 'directive',
name: className,
selector: `[${directive}]`,
},
{
name: 'TestCmp',
type: 'directive',
selector: `[test-cmp]`,
isStandalone: true,
}
],
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
// No diagnostic messages are expected.
expect(diags.length).toBe(0);
});
});
});
it(`should *not* produce a warning for other missing structural directives`, () => {
const fileName = absoluteFrom('/main.ts');
const {program, templateTypeChecker} = setup([{
fileName,
templates: {
'TestCmp': `<div *foo="exp"></div>`,
},
declarations: [{
name: 'TestCmp',
type: 'directive',
selector: `[test-cmp]`,
isStandalone: true,
}]
}]);
const sf = getSourceFileOrError(program, fileName);
const component = getClass(sf, 'TestCmp');
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
templateTypeChecker, program.getTypeChecker(), [missingControlFlowDirectiveCheck],
{strictNullChecks: true} /* options */);
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
// No diagnostic messages are expected.
expect(diags.length).toBe(0);
});
});
});