8000 [Tests] component detection: Add testing scaffolding · jsx-eslint/eslint-plugin-react@8b98e60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b98e60

Browse files
duncanbeeversljharb
authored andcommitted
[Tests] component detection: Add testing scaffolding
Test detection of Class Components and Stateless Function Components Lay scaffolding for other flavors of tests including further component types, pragma detection, and utils functions
1 parent 32f2e24 commit 8b98e60

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77

88
### Changed
99
* [Refactor] [`no-arrow-function-lifecycle`], [`no-unused-class-component-methods`]: use report/messages convention (@ljharb)
10+
* [Tests] component detection: Add testing scaffolding ([#3149][] @duncanbeevers)
11+
12+
[#3149]: https://github.com/yannickcr/eslint-plugin-react/pull/3149
1013

1114
## [7.27.1] - 2021.11.18
1215

tests/util/Component.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const eslint = require('eslint');
5+
const values = require('object.values');
6+
7+
const Components = require('../../lib/util/Components');
8+
const parsers = require('../helpers/parsers');
9+
10+
const ruleTester = new eslint.RuleTester({
11+
parserOptions: {
12+
ecmaVersion: 2018,
13+
sourceType: 'module',
14+
ecmaFeatures: {
15+
jsx: true,
16+
},
17+
},
18+
});
19+
20+
describe('Components', () => {
21+
describe('static detect', () => {
22+
function testComponentsDetect(test, done) {
23+
const rule = Components.detect((context, components, util) => ({
24+
'Program:exit'() {
25+
done(context, components, util);
26+
},
27+
}));
28+
29+
const tests = {
30+
valid: parsers.all([Object.assign({}, test, {
31+
settings: {
32+
react: {
33+
version: 'detect',
34+
},
35+
},
36+
})]),
37+
invalid: [],
38+
};
39+
ruleTester.run(test.code, rule, tests);
40+
}
41+
42+
it('should detect Stateless Function Component', () => {
43+
testComponentsDetect({
44+
code: `import React from 'react'
45+
function MyStatelessComponent() {
46+
return <React.Fragment />;
47+
}`,
48+
}, (_context, components) => {
49+
assert.equal(components.length(), 1, 'MyStatelessComponent should be detected component');
50+
values(components.list()).forEach((component) => {
51+
assert.equal(
52+
component.node.id.name,
53+
'MyStatelessComponent',
54+
'MyStatelessComponent should be detected component'
55+
);
56+
});
57+
});
58+
});
59+
60+
it('should detect Class Components', () => {
61+
testComponentsDetect({
62+
code: `import React from 'react'
63+
class MyClassComponent extends React.Component {
64+
render() {
65+
return <React.Fragment />;
66+
}
67+
}`,
68+
}, (_context, components) => {
69+
assert(components.length() === 1, 'MyClassComponent should be detected component');
70+
values(components.list()).forEach((component) => {
71+
assert.equal(
72+
component.node.id.name,
73+
'MyClassComponent',
74+
'MyClassComponent should be detected component'
75+
);
76+
});
77+
});
78+
});
79+
});
80+
});

0 commit comments

Comments
 (0)
0