@@ -15,6 +15,7 @@ import type Store from 'react-devtools-shared/src/devtools/store';
15
15
describe ( 'InspectedElementContext' , ( ) => {
16
16
let React ;
17
17
let ReactDOM ;
18
+ let PropTypes ;
18
19
let TestRenderer : ReactTestRenderer ;
19
20
let bridge : FrontendBridge ;
20
21
let store : Store ;
@@ -40,6 +41,7 @@ describe('InspectedElementContext', () => {
40
41
41
42
React = require ( 'react' ) ;
42
43
ReactDOM = require ( 'react-dom' ) ;
44
+ PropTypes = require ( 'prop-types' ) ;
43
45
TestUtils = require ( 'react-dom/test-utils' ) ;
44
46
TestRenderer = utils . requireTestRenderer ( ) ;
45
47
@@ -114,6 +116,119 @@ describe('InspectedElementContext', () => {
114
116
done ( ) ;
115
117
} ) ;
116
118
119
+ it ( 'should have hasLegacyContext flag set to either "true" or "false" depending on which context API is used.' , async done => {
120
+ const contextData = {
121
+ bool : true ,
122
+ } ;
123
+
124
+ // Legacy Context API.
125
+ class LegacyContextProvider extends React . Component < any > {
126
+ static childContextTypes = {
127
+ bool : PropTypes . bool ,
128
+ } ;
129
+ getChildContext ( ) {
130
+ return contextData ;
131
+ }
132
+ render ( ) {
133
+ return this . props . children ;
134
+ }
135
+ }
136
+ class LegacyContextConsumer extends React . Component < any > {
137
+ static contextTypes = {
138
+ bool : PropTypes . bool ,
139
+ } ;
140
+ render ( ) {
141
+ return null ;
142
+ }
143
+ }
144
+
145
+ // Modern Context API
146
+ const BoolContext = React . createContext ( contextData . bool ) ;
147
+ BoolContext . displayName = 'BoolContext' ;
148
+
149
+ class ModernContextType extends React . Component < any > {
150
+ static contextType = BoolContext ;
151
+ render ( ) {
152
+ return null ;
153
+ }
154
+ }
155
+
156
+ const ModernContext = React . createContext ( ) ;
157
+ ModernContext . displayName = 'ModernContext' ;
158
+
159
+ const container = document . createElement ( 'div' ) ;
160
+ await utils . actAsync ( ( ) =>
161
+ ReactDOM . render (
162
+ < React . Fragment >
163
+ < LegacyContextProvider >
164
+ < LegacyContextConsumer />
165
+ </ LegacyContextProvider >
166
+ < BoolContext . Consumer > { value => null } </ BoolContext . Consumer >
167
+ < ModernContextType />
168
+ < ModernContext . Provider value = { contextData } >
169
+ < ModernContext . Consumer > { value => null } </ ModernContext . Consumer >
170
+ </ ModernContext . Provider >
171
+ </ React . Fragment > ,
172
+ container ,
173
+ ) ,
174
+ ) ;
175
+
176
+ const ids = [
177
+ {
178
+ // <LegacyContextConsumer />
179
+ id : ( ( store . getElementIDAtIndex ( 1 ) : any ) : number ) ,
180
+ shouldHaveLegacyContext : true ,
181
+ } ,
182
+ {
183
+ // <BoolContext.Consumer>
184
+ id : ( ( store . getElementIDAtIndex ( 2 ) : any ) : number ) ,
185
+ shouldHaveLegacyContext : false ,
186
+ } ,
187
+ {
188
+ // <ModernContextType />
189
+ id : ( ( store . getElementIDAtIndex ( 3 ) : any ) : number ) ,
190
+ shouldHaveLegacyContext : false ,
191
+ } ,
192
+ {
193
+ // <ModernContext.Consumer>
194
+ id : ( ( store . getElementIDAtIndex ( 5 ) : any ) : number ) ,
195
+ shouldHaveLegacyContext : false ,
196
+ } ,
197
+ ] ;
198
+
199
+ function Suspender ( { target, shouldHaveLegacyContext} ) {
200
+ const { getInspectedElement} = React . useContext ( InspectedElementContext ) ;
201
+ const inspectedElement = getInspectedElement ( target ) ;
202
+
203
+ expect ( inspectedElement . context ) . not . toBe ( null ) ;
204
+ expect ( inspectedElement . hasLegacyContext ) . toBe ( shouldHaveLegacyContext ) ;
205
+
206
+ return null ;
207
+ }
208
+
209
+ for ( let i = 0 ; i < ids . length ; i ++ ) {
210
+ const { id , shouldHaveLegacyContext } = ids [ i ] ;
211
+
212
+ await utils . actAsync (
213
+ ( ) =>
214
+ TestRenderer . create (
215
+ < Contexts
216
+ defaultSelectedElementID = { id }
217
+ defaultSelectedElementIndex = { 0 } >
218
+ < React . Suspense fallback = { null } >
219
+ < Suspender
220
+ target = { id }
221
+ shouldHaveLegacyContext = { shouldHaveLegacyContext }
222
+ />
223
+ </ React . Suspense >
224
+ </ Contexts > ,
225
+ ) ,
226
+ false ,
227
+ ) ;
228
+ }
229
+ done ( ) ;
230
+ } ) ;
231
+
117
232
it ( 'should poll for updates for the currently selected element' , async done => {
118
233
const Example = ( ) => null ;
119
234
0 commit comments