11import { assertSuccess , assertAnnotated } from './testHelper' ;
2+ import { sprintf } from 'sprintf-js' ;
3+ import { Rule } from '../src/noInputRenameRule' ;
24
35const ruleName = 'no-input-rename' ;
46
57const 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 ) ;
79} ;
810
911describe ( ruleName , ( ) => {
1012 describe ( 'failure' , ( ) => {
1113 describe ( 'Component' , ( ) => {
12- it ( 'should fail when a input property is renamed' , ( ) => {
14+ it ( 'should fail when an input property is renamed' , ( ) => {
1315 const source = `
14- @Component
16+ @Component({
17+ selector: 'foo'
18+ })
1519 class TestComponent {
16- @Input('labelAttribute ') label: string;
17- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+ @Input('bar ') label: string;
21+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1822 }
1923 ` ;
2024 assertAnnotated ( {
@@ -26,10 +30,12 @@ describe(ruleName, () => {
2630
2731 it ( 'should fail when input property is fake renamed' , ( ) => {
2832 const source = `
29- @Component
33+ @Component({
34+ selector: 'foo'
35+ })
3036 class TestComponent {
31- @Input('label ') label: string;
32- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37+ @Input('foo ') label: string;
38+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3339 }
3440 ` ;
3541 assertAnnotated ( {
@@ -41,9 +47,11 @@ describe(ruleName, () => {
4147 } ) ;
4248
4349 describe ( 'Directive' , ( ) => {
44- it ( 'should fail when a input property is renamed' , ( ) => {
50+ it ( 'should fail when an input property is renamed' , ( ) => {
4551 const source = `
46- @Directive
52+ @Directive({
53+ selector: '[foo]
54+ })
4755 class TestDirective {
4856 @Input('labelText') label: string;
4957 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -56,19 +64,36 @@ describe(ruleName, () => {
5664 } ) ;
5765 } ) ;
5866
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' , ( ) => {
6068 const source = `
6169 @Directive({
62- selector: '[label], label2'
70+ selector: '[label]
6371 })
6472 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+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6792 }
6893 ` ;
6994 assertAnnotated ( {
7095 ruleName,
71- message : getMessage ( 'TestDirective' , 'labelText ' ) ,
96+ message : getMessage ( 'TestDirective' , 'label ' ) ,
7297 source
7398 } ) ;
7499 } ) ;
@@ -77,7 +102,7 @@ describe(ruleName, () => {
77102
78103 describe ( 'success' , ( ) => {
79104 describe ( 'Component' , ( ) => {
80- it ( 'should succeed when a input property is not renamed' , ( ) => {
105+ it ( 'should succeed when an input property is not renamed' , ( ) => {
81106 const source = `
82107 @Component
83108 class TestComponent {
@@ -89,13 +114,37 @@ describe(ruleName, () => {
89114 } ) ;
90115
91116 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" , ( ) => {
93118 const source = `
94119 @Directive({
95- selector: '[label ], label2'
120+ selector: '[foo ], label2'
96121 })
97122 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;
99148 }
100149 ` ;
101150 assertSuccess ( ruleName , source ) ;
0 commit comments