1
1
import faker from 'faker'
2
2
import _ from 'lodash'
3
3
import React from 'react'
4
+ import ReactIs from 'react-is'
4
5
import ReactDOMServer from 'react-dom/server'
5
6
import * as semanticUIReact from 'semantic-ui-react'
6
7
7
8
import { componentInfoContext } from 'docs/src/utils'
8
- import { assertBodyContains , consoleUtil , sandbox , syntheticEvent } from 'test/utils'
9
- import helpers from './commonHelpers'
9
+ import {
10
+ assertBodyContains ,
11
+ consoleUtil ,
12
+ getComponentName ,
13
+ getComponentProps ,
14
+ sandbox ,
15
+ syntheticEvent ,
16
+ } from 'test/utils'
10
17
import hasValidTypings from './hasValidTypings'
11
18
12
19
/**
@@ -20,7 +27,7 @@ import hasValidTypings from './hasValidTypings'
20
27
* @param {boolean } [options.rendersPortal=false] Does this component render a Portal powered component?
21
28
* @param {Object } [options.requiredProps={}] Props required to render Component without errors or warnings.
22
29
*/
23
- export default ( Component , options = { } ) => {
30
+ export default function isConformant ( Component , options = { } ) {
24
31
const {
25
32
eventTargets = { } ,
26
33
nestingLevel = 0 ,
@@ -29,25 +36,26 @@ export default (Component, options = {}) => {
29
36
rendersFragmentByDefault = false ,
30
37
rendersPortal = false ,
31
38
} = options
32
- const { throwError } = helpers ( 'isConformant' , Component )
39
+ const constructorName = getComponentName ( Component )
33
40
34
- const componentType = typeof Component
35
-
36
- // make sure components are properly exported
37
- if ( componentType !== 'function' ) {
38
- throwError ( `Components should export a class or function, got: ${ componentType } .` )
39
- }
40
-
41
- // tests depend on Component constructor names, enforce them
42
- const constructorName = Component . prototype . constructor . name
43
- if ( ! constructorName ) {
44
- throwError (
45
- [
46
- 'Component is not a named function. This should help identify it:\n\n' ,
47
- `${ ReactDOMServer . renderToStaticMarkup ( < Component /> ) } ` ,
48
- ] . join ( '' ) ,
41
+ it ( 'a valid component should be exported' , ( ) => {
42
+ expect ( ReactIs . isValidElementType ( Component ) ) . to . equal (
43
+ true ,
44
+ `Components should export a class or function, got: ${ typeof Component } .` ,
49
45
)
50
- }
46
+ } )
47
+
48
+ it ( 'a component should be a function/class or "displayName" should be defined' , ( ) => {
49
+ if ( ! constructorName ) {
50
+ throw new Error (
51
+ [
52
+ 'Component is not a named function and does not have a "displayName".' ,
53
+ 'This should help identify it:\n\n' ,
54
+ `${ ReactDOMServer . renderToStaticMarkup ( < Component { ...requiredProps } /> ) } ` ,
55
+ ] . join ( '' ) ,
56
+ )
57
+ }
58
+ } )
51
59
52
60
const info = componentInfoContext . byDisplayName [ constructorName ]
53
61
@@ -192,20 +200,22 @@ export default (Component, options = {}) => {
192
200
}
193
201
194
202
describe ( 'handles props' , ( ) => {
203
+ const componentProps = getComponentProps ( Component )
204
+
195
205
it ( 'defines handled props in Component.handledProps' , ( ) => {
196
- Component . should . have . any . keys ( 'handledProps' )
197
- Component . handledProps . should . be . an ( 'array' )
206
+ componentProps . should . have . any . keys ( 'handledProps' )
207
+ componentProps . handledProps . should . be . an ( 'array' )
198
208
} )
199
209
200
210
it ( 'Component.handledProps includes all handled props' , ( ) => {
201
211
const computedProps = _ . union (
202
- Component . autoControlledProps ,
203
- _ . keys ( Component . defaultProps ) ,
204
- _ . keys ( Component . propTypes ) ,
212
+ componentProps . autoControlledProps ,
213
+ _ . keys ( componentProps . defaultProps ) ,
214
+ _ . keys ( componentProps . propTypes ) ,
205
215
)
206
216
const expectedProps = _ . uniq ( computedProps ) . sort ( )
207
217
208
- Component . handledProps . should . to . deep . equal (
218
+ componentProps . handledProps . should . to . deep . equal (
209
219
expectedProps ,
210
220
'It seems that not all props were defined in Component.handledProps, you need to check that they are equal ' +
211
221
'to the union of Component.autoControlledProps and keys of Component.defaultProps and Component.propTypes' ,
@@ -376,6 +386,7 @@ export default (Component, options = {}) => {
376
386
} )
377
387
} )
378
388
}
389
+
379
390
// ----------------------------------------
380
391
// Test typings
381
392
// ----------------------------------------
0 commit comments