-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
1 parent
32f2e24
commit 8b98e60
Showing
2 changed files
with
83 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const eslint = require('eslint'); | ||
const values = require('object.values'); | ||
|
||
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'); | ||
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'); | ||
values(components.list()).forEach((component) => { | ||
assert.equal( | ||
component.node.id.name, | ||
'MyClassComponent', | ||
'MyClassComponent should be detected component' | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |