-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rule): template-conditional-complexity not reporting failures for…
… '[ngIf]' (#611)
- Loading branch information
1 parent
fedd331
commit 7fc3b09
Showing
2 changed files
with
142 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1,137 @@ | ||
import { assertSuccess, assertAnnotated } from './testHelper'; | ||
import { getFailureMessage, Rule } from '../src/templateConditionalComplexityRule'; | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe('complexity', () => { | ||
describe('success', () => { | ||
it('should work with a lower level of complexity', () => { | ||
let source = ` | ||
const { | ||
metadata: { ruleName } | ||
} = Rule; | ||
|
||
describe(ruleName, () => { | ||
describe('failure', () => { | ||
it('should fail with a higher level of complexity', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '1' || (b === '2' && c.d !== e)"> | ||
<div *ngIf="a === '3' || (b === '3' && c.d !== '1' && e.f !== '6' && q !== g)"> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Enter your card details | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess('template-conditional-complexity', source); | ||
assertAnnotated({ message: getFailureMessage(5, 4), options: [4], ruleName, source }); | ||
}); | ||
|
||
it('should work with a lower level of complexity', () => { | ||
let source = ` | ||
it('should fail with a higher level of complexity and a carrier return', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '1' || b === '2' && c.d !== e"> | ||
<div *ngIf="a === '3' || (b === '3' && c.d !== '1' | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
&& e.f !== '6' && q !== g)"> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Enter your card details | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess('template-conditional-complexity', source); | ||
assertAnnotated({ message: getFailureMessage(5, 3), ruleName, source }); | ||
}); | ||
|
||
it('should work with a level of complexity customisable', () => { | ||
let source = ` | ||
it('should fail with a higher level of complexity with ng-template', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '3' || (b === '3' && c.d !== '1' && e.f !== '6' && q !== g)"> | ||
<ng-template [ngIf]="a === '3' || (b === '3' && c.d !== '1' | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
&& e.f !== '6' && q !== g && x === '1')"> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Enter details | ||
</ng-template> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertAnnotated({ message: getFailureMessage(6, 3), ruleName, source }); | ||
}); | ||
}); | ||
|
||
describe('success', () => { | ||
it('should succeed with a lower level of complexity', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '1' || b === '2' && c.d !== e"> | ||
Enter your card details | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess('template-conditional-complexity', source, [5]); | ||
assertSuccess(ruleName, source); | ||
}); | ||
|
||
it('should work with a level of complexity customisable', () => { | ||
let source = ` | ||
it('should succeed with a lower level of complexity with separated statements', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="(b === '3' && c.d !== '1' && e.f !== '6' && q !== g) || a === '3'"> | ||
<div *ngIf="a === '1' || (b === '2' && c.d !== e)"> | ||
Enter your card details | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess('template-conditional-complexity', source, [5]); | ||
assertSuccess(ruleName, source); | ||
}); | ||
|
||
it('should work with something else', () => { | ||
let source = ` | ||
it('should succeed with a level of complexity customizable', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="isValid;then content else other_content"> | ||
<div *ngIf="a === '3' || (b === '3' && c.d !== '1' && e.f !== '6' && q !== g)"> | ||
Enter your card details | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess('template-conditional-complexity', source, [5]); | ||
assertSuccess(ruleName, source, [5]); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail with a higher level of complexity', () => { | ||
let source = ` | ||
it('should succeed with a level of complexity customizable', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '3' || (b === '3' && c.d !== '1' && e.f !== '6' && q !== g)"> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
<div *ngIf="(b === '3' && c.d !== '1' && e.f !== '6' && q !== g) || a === '3'"> | ||
Enter your card details | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertAnnotated({ | ||
ruleName: 'template-conditional-complexity', | ||
message: | ||
"The condition complexity (cost '5') exceeded the defined limit (cost '4'). The conditional expression should be moved into the component.", | ||
source, | ||
options: [4] | ||
}); | ||
assertSuccess(ruleName, source, [5]); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail with a higher level of complexity and a carrier return', () => { | ||
let source = ` | ||
it('should succeed with non-inlined then template', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '3' || (b === '3' && c.d !== '1' | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
&& e.f !== '6' && q !== g)"> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
<div *ngIf="isValid; then thenBlock; else elseBlock"> | ||
Enter your card details | ||
</div> | ||
<ng-template #thenBlock> | ||
thenBlock | ||
</ng-template> | ||
<ng-template #elseBlock> | ||
elseBlock | ||
</ng-template> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertAnnotated({ | ||
ruleName: 'template-conditional-complexity', | ||
message: | ||
"The condition complexity (cost '5') exceeded the defined limit (cost '3'). The conditional expression should be moved into the component.", | ||
source | ||
}); | ||
assertSuccess(ruleName, source, [5]); | ||
}); | ||
}); | ||
}); |