Skip to content

Commit

Permalink
add tests for common invalid cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Belco90 committed Apr 25, 2022
1 parent 2429bcc commit 51068b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/rules/jsx-no-leaked-zero.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,4 @@ If you are working in a typed-codebase which encourages you to always use boolea

- [React docs: Inline If with Logical && Operator](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator)
- [Good advice on JSX conditionals - Beware of zero](https://thoughtspile.github.io/2022/01/17/jsx-conditionals/)
- [Twitter: rendering falsy values in React and React Native](https://twitter.com/kadikraman/status/1507654900376875011?s=21&t=elEXXbHhzWthrgKaPRMjNg)
43 changes: 43 additions & 0 deletions tests/lib/rules/jsx-no-leaked-zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,49 @@ ruleTester.run('jsx-no-leaked-zero', rule, {
]),

invalid: parsers.all([
// Common invalid cases with default options
{
code: `
const Example = () => {
return (
<>
{0 && <Something/>}
{'' && <Something/>}
{NaN && <Something/>}
</>
)
}
`,
errors: [
{
message: 'Potential numeric evaluation resulting in an unintentionally rendered `0`',
line: 5,
column: 14,
},
{
message: 'Potential numeric evaluation resulting in an unintentionally rendered `0`',
line: 6,
column: 14,
},
{
message: 'Potential numeric evaluation resulting in an unintentionally rendered `0`',
line: 7,
column: 14,
},
],
output: `
const Example = () => {
return (
<>
{0 ? <Something/> : null}
{'' ? <Something/> : null}
{NaN ? <Something/> : null}
</>
)
}
`,
},

// Invalid tests with both fix strategies (default)
{
code: `
Expand Down

0 comments on commit 51068b3

Please sign in to comment.