@@ -229,6 +229,111 @@ describe('render', () => {
229
229
. to . have . been . calledOnce
230
230
. and . to . have . been . calledBefore ( Test . prototype . render ) ;
231
231
} ) ;
232
+
233
+ it ( 'should pass context to grandchildren' , ( ) => {
234
+ const CONTEXT = { a :'a' } ;
235
+ const PROPS = { b :'b' } ;
236
+
237
+ class Outer extends Component {
238
+ getChildContext ( ) {
239
+ return CONTEXT ;
240
+ }
241
+ render ( props ) {
242
+ return < div > < Inner { ...props } /> </ div > ;
243
+ }
244
+ }
245
+ spy ( Outer . prototype , 'getChildContext' ) ;
246
+
247
+ class Inner extends Component {
248
+ render ( props , state , context ) {
249
+ return < div > { context && context . a } </ div > ;
250
+ }
251
+ }
252
+ spy ( Inner . prototype , 'render' ) ;
253
+
254
+ render ( < Outer /> ) ;
255
+
256
+ expect ( Outer . prototype . getChildContext ) . to . have . been . calledOnce ;
257
+ expect ( Inner . prototype . render ) . to . have . been . calledWith ( { } , { } , CONTEXT ) ;
258
+
259
+ CONTEXT . foo = 'bar' ;
260
+ render ( < Outer { ...PROPS } /> ) ;
261
+
262
+ expect ( Outer . prototype . getChildContext ) . to . have . been . calledTwice ;
263
+ expect ( Inner . prototype . render ) . to . have . been . calledWith ( PROPS , { } , CONTEXT ) ;
264
+ } ) ;
265
+
266
+ it ( 'should pass context to direct children' , ( ) => {
267
+ const CONTEXT = { a :'a' } ;
268
+ const PROPS = { b :'b' } ;
269
+
270
+ class Outer extends Component {
271
+ getChildContext ( ) {
272
+ return CONTEXT ;
273
+ }
274
+ render ( props ) {
275
+ return < Inner { ...props } /> ;
276
+ }
277
+ }
278
+ spy ( Outer . prototype , 'getChildContext' ) ;
279
+
280
+ class Inner extends Component {
281
+ render ( props , state , context ) {
282
+ return < div > { context && context . a } </ div > ;
283
+ }
284
+ }
285
+ spy ( Inner . prototype , 'render' ) ;
286
+
287
+ render ( < Outer /> ) ;
288
+
289
+ expect ( Outer . prototype . getChildContext ) . to . have . been . calledOnce ;
290
+ expect ( Inner . prototype . render ) . to . have . been . calledWith ( { } , { } , CONTEXT ) ;
291
+
292
+ CONTEXT . foo = 'bar' ;
293
+ render ( < Outer { ...PROPS } /> ) ;
294
+
295
+ expect ( Outer . prototype . getChildContext ) . to . have . been . calledTwice ;
296
+ expect ( Inner . prototype . render ) . to . have . been . calledWith ( PROPS , { } , CONTEXT ) ;
297
+
298
+ // make sure render() could make use of context.a
299
+ expect ( Inner . prototype . render ) . to . have . returned ( match ( { children :[ 'a' ] } ) ) ;
300
+ } ) ;
301
+
302
+ it ( 'should preserve existing context properties when creating child contexts' , ( ) => {
303
+ let outerContext = { outer :true } ,
304
+ innerContext = { inner :true } ;
305
+ class Outer extends Component {
306
+ getChildContext ( ) {
307
+ return { outerContext } ;
308
+ }
309
+ render ( ) {
310
+ return < div > < Inner /> </ div > ;
311
+ }
312
+ }
313
+
314
+ class Inner extends Component {
315
+ getChildContext ( ) {
316
+ return { innerContext } ;
317
+ }
318
+ render ( ) {
319
+ return < InnerMost /> ;
320
+ }
321
+ }
322
+
323
+ class InnerMost extends Component {
324
+ render ( ) {
325
+ return < strong > test</ strong > ;
326
+ }
327
+ }
328
+
329
+ spy ( Inner . prototype , 'render' ) ;
330
+ spy ( InnerMost . prototype , 'render' ) ;
331
+
332
+ render ( < Outer /> ) ;
333
+
334
+ expect ( Inner . prototype . render ) . to . have . been . calledWith ( { } , { } , { outerContext } ) ;
335
+ expect ( InnerMost . prototype . render ) . to . have . been . calledWith ( { } , { } , { outerContext, innerContext } ) ;
336
+ } ) ;
232
337
} ) ;
233
338
234
339
describe ( 'High-order components' , ( ) => {
0 commit comments