6
6
*
7
7
*/
8
8
9
- import { expression , statement , parse } from '../../../tests/utils' ;
9
+ import {
10
+ expression ,
11
+ statement ,
12
+ parse ,
13
+ noopImporter ,
14
+ makeMockImporter ,
15
+ } from '../../../tests/utils' ;
10
16
import isReactComponentClass from '../isReactComponentClass' ;
11
17
12
18
describe ( 'isReactComponentClass' , ( ) => {
19
+ const mockImporter = makeMockImporter ( {
20
+ component : statement ( `
21
+ export default React.Component;
22
+ import React from 'react';
23
+ ` ) . get ( 'declaration' ) ,
24
+
25
+ pureComponent : statement ( `
26
+ export default React.PureComponent;
27
+ import React from 'react';
28
+ ` ) . get ( 'declaration' ) ,
29
+ } ) ;
30
+
13
31
describe ( 'render method' , ( ) => {
14
32
it ( 'ignores class declarations with a render method without superclass' , ( ) => {
15
33
const def = statement ( 'class Foo { render() {}}' ) ;
16
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
34
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
17
35
} ) ;
18
36
19
37
it ( 'ignores class expression with a render method without superclass' , ( ) => {
20
38
const def = expression ( 'class { render() {}}' ) ;
21
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
39
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
22
40
} ) ;
23
41
24
42
it ( 'ignores static render methods' , ( ) => {
25
43
const def = statement ( 'class Foo { static render() {}}' ) ;
26
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
44
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
27
45
} ) ;
28
46
29
47
it ( 'ignores dynamic render methods' , ( ) => {
30
48
const def = statement ( 'class Foo { static [render]() {}}' ) ;
31
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
49
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
32
50
} ) ;
33
51
34
52
it ( 'ignores getter or setter render methods' , ( ) => {
35
53
let def = statement ( 'class Foo { get render() {}}' ) ;
36
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
54
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
37
55
38
56
def = statement ( 'class Foo { set render(value) {}}' ) ;
39
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
57
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
40
58
} ) ;
41
59
} ) ;
42
60
@@ -51,7 +69,7 @@ describe('isReactComponentClass', () => {
51
69
class Foo extends Bar {}
52
70
` ) . get ( 'body' , 1 ) ;
53
71
54
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
72
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
55
73
} ) ;
56
74
} ) ;
57
75
@@ -62,7 +80,7 @@ describe('isReactComponentClass', () => {
62
80
class Foo extends React.Component {}
63
81
` ) . get ( 'body' , 1 ) ;
64
82
65
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
83
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
66
84
} ) ;
67
85
68
86
it ( 'accepts class expressions extending React.Component' , ( ) => {
@@ -71,7 +89,7 @@ describe('isReactComponentClass', () => {
71
89
var Foo = class extends React.Component {}
72
90
` ) . get ( 'body' , 1 , 'declarations' , 0 , 'init' ) ;
73
91
74
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
92
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
75
93
} ) ;
76
94
77
95
it ( 'resolves the super class reference' , ( ) => {
@@ -81,7 +99,7 @@ describe('isReactComponentClass', () => {
81
99
class Foo extends C {}
82
100
` ) . get ( 'body' , 2 ) ;
83
101
84
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
102
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
85
103
} ) ;
86
104
87
105
it ( 'does not accept references to other modules' , ( ) => {
@@ -90,7 +108,7 @@ describe('isReactComponentClass', () => {
90
108
class Foo extends Component {}
91
109
` ) . get ( 'body' , 1 ) ;
92
110
93
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
111
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
94
112
} ) ;
95
113
96
114
it ( 'does not consider super class if render method is present' , ( ) => {
@@ -99,7 +117,16 @@ describe('isReactComponentClass', () => {
99
117
class Foo extends Component { render() {} }
100
118
` ) . get ( 'body' , 1 ) ;
101
119
102
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
120
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
121
+ } ) ;
122
+
123
+ it ( 'can resolve Component from an intermediate module' , ( ) => {
124
+ const def = parse ( `
125
+ import RC from 'component';
126
+ class Foo extends RC {}
127
+ ` ) . get ( 'body' , 1 ) ;
128
+
129
+ expect ( isReactComponentClass ( def , mockImporter ) ) . toBe ( true ) ;
103
130
} ) ;
104
131
} ) ;
105
132
@@ -110,7 +137,7 @@ describe('isReactComponentClass', () => {
110
137
class Foo extends React.PureComponent {}
111
138
` ) . get ( 'body' , 1 ) ;
112
139
113
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
140
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
114
141
} ) ;
115
142
116
143
it ( 'accepts class expressions extending React.PureComponent' , ( ) => {
@@ -119,7 +146,7 @@ describe('isReactComponentClass', () => {
119
146
var Foo = class extends React.PureComponent {}
120
147
` ) . get ( 'body' , 1 , 'declarations' , 0 , 'init' ) ;
121
148
122
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
149
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
123
150
} ) ;
124
151
125
152
it ( 'resolves the super class reference' , ( ) => {
@@ -129,7 +156,7 @@ describe('isReactComponentClass', () => {
129
156
class Foo extends C {}
130
157
` ) . get ( 'body' , 2 ) ;
131
158
132
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
159
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
133
160
} ) ;
134
161
135
162
it ( 'does not accept references to other modules' , ( ) => {
@@ -138,7 +165,7 @@ describe('isReactComponentClass', () => {
138
165
class Foo extends PureComponent {}
139
166
` ) . get ( 'body' , 1 ) ;
140
167
141
- expect ( isReactComponentClass ( def ) ) . toBe ( false ) ;
168
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( false ) ;
142
169
} ) ;
143
170
144
171
it ( 'does not consider super class if render method is present' , ( ) => {
@@ -147,7 +174,16 @@ describe('isReactComponentClass', () => {
147
174
class Foo extends PureComponent { render() {} }
148
175
` ) . get ( 'body' , 1 ) ;
149
176
150
- expect ( isReactComponentClass ( def ) ) . toBe ( true ) ;
177
+ expect ( isReactComponentClass ( def , noopImporter ) ) . toBe ( true ) ;
178
+ } ) ;
179
+
180
+ it ( 'can resolve PureComponent from an intermediate module' , ( ) => {
181
+ const def = parse ( `
182
+ import PC from 'pureComponent';
183
+ class Foo extends PC {}
184
+ ` ) . get ( 'body' , 1 ) ;
185
+
186
+ expect ( isReactComponentClass ( def , mockImporter ) ) . toBe ( true ) ;
151
187
} ) ;
152
188
} ) ;
153
189
} ) ;
0 commit comments