Skip to content

Commit

Permalink
[Tests] component detection: Add testing scaffolding
Browse files Browse the repository at this point in the history
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
  • Loading branch information
duncanbeevers authored and ljharb committed Nov 26, 2021
1 parent 32f2e24 commit 4f413d4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

### Changed
* [Refactor] [`no-arrow-function-lifecycle`], [`no-unused-class-component-methods`]: use report/messages convention (@ljharb)
* [Tests] component detection: Add testing scaffolding ([#3149][] @duncanbeevers)

[#3149]: https://github.com/yannickcr/eslint-plugin-react/pull/3149

## [7.27.1] - 2021.11.18

Expand Down
78 changes: 78 additions & 0 deletions tests/util/Component.js
@@ -0,0 +1,78 @@
'use strict';

const assert = require('assert');
const eslint = require('eslint');
const Components = require('../../lib/util/Components');
const parsers = require('../helpers/parsers');

const ruleTester = new eslint.RuleTester({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
});

describe('Components', () => {
describe('static detect', () => {
function testComponentsDetect(test, done) {
const rule = Components.detect((context, components, util) => ({
'Program:exit'() {
done(context, components, util);
},
}));

const tests = {
valid: parsers.all([Object.assign({}, test, {
settings: {
react: {
version: 'detect',
},
},
})]),
invalid: [],
};
ruleTester.run(test.code, rule, tests);
}

it('should detect Stateless Function Component', () => {
testComponentsDetect({
code: `import React from 'react'
function MyStatelessComponent() {
return <React.Fragment />;
}`,
}, (_context, components) => {
assert.equal(components.length(), 1, 'MyStatelessComponent should be detected component');
Object.values(components.list()).forEach((component) => {
assert.equal(
component.node.id.name,
'MyStatelessComponent',
'MyStatelessComponent should be detected component'
);
});
});
});

it('should detect Class Components', () => {
testComponentsDetect({
code: `import React from 'react'
class MyClassComponent extends React.Component {
render() {
return <React.Fragment />;
}
}`,
}, (_context, components) => {
assert(components.length() === 1, 'MyClassComponent should be detected component');
Object.values(components.list()).forEach((component) => {
assert.equal(
component.node.id.name,
'MyClassComponent',
'MyClassComponent should be detected component'
);
});
});
});
});
});

0 comments on commit 4f413d4

Please sign in to comment.