1
1
import { assertSuccess , assertAnnotated } from './testHelper' ;
2
+ import { sprintf } from 'sprintf-js' ;
3
+ import { Rule } from '../src/noInputRenameRule' ;
2
4
3
5
const ruleName = 'no-input-rename' ;
4
6
5
7
const getMessage = ( className : string , propertyName : string ) : string => {
6
- return `In the class " ${ className } ", the directive input property " ${ propertyName } " should not be renamed.` ;
8
+ return sprintf ( Rule . FAILURE_STRING , className , propertyName ) ;
7
9
} ;
8
10
9
11
describe ( ruleName , ( ) => {
10
12
describe ( 'failure' , ( ) => {
11
13
describe ( 'Component' , ( ) => {
12
- it ( 'should fail when a input property is renamed' , ( ) => {
14
+ it ( 'should fail when an input property is renamed' , ( ) => {
13
15
const source = `
14
- @Component
16
+ @Component({
17
+ selector: 'foo'
18
+ })
15
19
class TestComponent {
16
- @Input('labelAttribute ') label: string;
17
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20
+ @Input('bar ') label: string;
21
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18
22
}
19
23
` ;
20
24
assertAnnotated ( {
@@ -26,10 +30,12 @@ describe(ruleName, () => {
26
30
27
31
it ( 'should fail when input property is fake renamed' , ( ) => {
28
32
const source = `
29
- @Component
33
+ @Component({
34
+ selector: 'foo'
35
+ })
30
36
class TestComponent {
31
- @Input('label ') label: string;
32
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37
+ @Input('foo ') label: string;
38
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
39
}
34
40
` ;
35
41
assertAnnotated ( {
@@ -41,9 +47,11 @@ describe(ruleName, () => {
41
47
} ) ;
42
48
43
49
describe ( 'Directive' , ( ) => {
44
- it ( 'should fail when a input property is renamed' , ( ) => {
50
+ it ( 'should fail when an input property is renamed' , ( ) => {
45
51
const source = `
46
- @Directive
52
+ @Directive({
53
+ selector: '[foo]
54
+ })
47
55
class TestDirective {
48
56
@Input('labelText') label: string;
49
57
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -56,19 +64,36 @@ describe(ruleName, () => {
56
64
} ) ;
57
65
} ) ;
58
66
59
- it ( " should fail when input property is renamed and it's different from directive's selector" , ( ) => {
67
+ it ( ' should fail when an input property has the same name that the alias' , ( ) => {
60
68
const source = `
61
69
@Directive({
62
- selector: '[label], label2'
70
+ selector: '[label]
63
71
})
64
72
class TestDirective {
65
- @Input('label') labelText: string;
66
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
+ @Input('label') label: string;
74
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75
+ }
76
+ ` ;
77
+ assertAnnotated ( {
78
+ ruleName,
79
+ message : getMessage ( 'TestDirective' , 'label' ) ,
80
+ source
81
+ } ) ;
82
+ } ) ;
83
+
84
+ it ( 'should fail when an input property has the same name that the alias' , ( ) => {
85
+ const source = `
86
+ @Directive({
87
+ selector: '[foo]
88
+ })
89
+ class TestDirective {
90
+ @Input('label') label: string;
91
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67
92
}
68
93
` ;
69
94
assertAnnotated ( {
70
95
ruleName,
71
- message : getMessage ( 'TestDirective' , 'labelText ' ) ,
96
+ message : getMessage ( 'TestDirective' , 'label ' ) ,
72
97
source
73
98
} ) ;
74
99
} ) ;
@@ -77,7 +102,7 @@ describe(ruleName, () => {
77
102
78
103
describe ( 'success' , ( ) => {
79
104
describe ( 'Component' , ( ) => {
80
- it ( 'should succeed when a input property is not renamed' , ( ) => {
105
+ it ( 'should succeed when an input property is not renamed' , ( ) => {
81
106
const source = `
82
107
@Component
83
108
class TestComponent {
@@ -89,13 +114,37 @@ describe(ruleName, () => {
89
114
} ) ;
90
115
91
116
describe ( 'Directive' , ( ) => {
92
- it ( ' should succeed when the directive name is also an input property' , ( ) => {
117
+ it ( " should succeed when the directive's selector is also an input metadata property" , ( ) => {
93
118
const source = `
94
119
@Directive({
95
- selector: '[label ], label2'
120
+ selector: '[foo ], label2'
96
121
})
97
122
class TestDirective {
98
- @Input('labelText') label: string;
123
+ @Input('foo') bar: string;
124
+ }
125
+ ` ;
126
+ assertSuccess ( ruleName , source ) ;
127
+ } ) ;
128
+
129
+ it ( "should succeed when the directive's selector is also an input metadata property" , ( ) => {
130
+ const source = `
131
+ @Directive({
132
+ selector: '[foo], myselector'
133
+ })
134
+ class TestDirective {
135
+ @Input('myselector') bar: string;
136
+ }
137
+ ` ;
138
+ assertSuccess ( ruleName , source ) ;
139
+ } ) ;
140
+
141
+ it ( "should succeed when the directive's selector is also an input property" , ( ) => {
142
+ const source = `
143
+ @Directive({
144
+ selector: '[foo], label2'
145
+ })
146
+ class TestDirective {
147
+ @Input() foo: string;
99
148
}
100
149
` ;
101
150
assertSuccess ( ruleName , source ) ;
0 commit comments