Skip to content

Commit 1811a0e

Browse files
committed
test(rules): add tests to varios rules to check improved function component detection
1 parent 4689be2 commit 1811a0e

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

tests/lib/rules/function-component-definition.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ ruleTester.run('function-component-definition', rule, {
6868
}, {
6969
code: 'var Foo = React.memo(function Foo() { return <p/> })',
7070
options: [{namedComponents: 'function-declaration'}]
71+
}, {
72+
// shouldn't trigger this rule since functions stating with a lowercase
73+
// letter are not considered components
74+
code: `
75+
const selectAvatarByUserId = (state, id) => {
76+
const user = selectUserById(state, id)
77+
return null
78+
}
79+
`,
80+
options: [{namedComponents: 'function-declaration'}]
81+
}, {
82+
// shouldn't trigger this rule since functions stating with a lowercase
83+
// letter are not considered components
84+
code: `
85+
function ensureValidSourceType(sourceType: string) {
86+
switch (sourceType) {
87+
case 'ALBUM':
88+
case 'PLAYLIST':
89+
return sourceType;
90+
default:
91+
return null;
92+
}
93+
}
94+
`,
95+
options: [{namedComponents: 'arrow-function'}],
96+
parser: parsers.TYPESCRIPT_ESLINT
7197
}, {
7298
code: 'function Hello(props: Test) { return <p/> }',
7399
options: [{namedComponents: 'function-declaration'}],

tests/lib/rules/prop-types.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,27 @@ ruleTester.run('prop-types', rule, {
25042504
}
25052505
`,
25062506
parser: parsers.TYPESCRIPT_ESLINT
2507-
}
2507+
},
2508+
// shouldn't trigger this rule since functions stating with a lowercase
2509+
// letter are not considered components
2510+
`
2511+
function noAComponent(props) {
2512+
return <div>{props.text}</div>
2513+
}
2514+
`,
2515+
// shouldn't trigger this rule for 'render' since functions stating with a lowercase
2516+
// letter are not considered components
2517+
`
2518+
const MyComponent = (props) => {
2519+
const render = () => {
2520+
return <test>{props.hello}</test>;
2521+
}
2522+
return render();
2523+
};
2524+
MyComponent.propTypes = {
2525+
hello: PropTypes.string.isRequired,
2526+
};
2527+
`
25082528
],
25092529

25102530
invalid: [

0 commit comments

Comments
 (0)