11import React from 'react' ;
22import { flatten , unique , compact } from 'underscore' ;
3- import ReactWrapperComponent from './ReactWrapperComponent' ;
3+ import createWrapperComponent from './ReactWrapperComponent' ;
44import {
55 instHasClassName ,
66 childrenOfInst ,
@@ -45,7 +45,7 @@ function filterWhereUnwrapped(wrapper, predicate) {
4545 */
4646export default class ReactWrapper {
4747
48- constructor ( nodes , root ) {
48+ constructor ( nodes , root , options = { } ) {
4949 if ( ! global . window && ! global . document ) {
5050 throw new Error (
5151 `It looks like you called \`mount()\` without a jsdom document being loaded. ` +
@@ -54,10 +54,12 @@ export default class ReactWrapper {
5454 }
5555
5656 if ( ! root ) {
57+ const ReactWrapperComponent = createWrapperComponent ( nodes , options ) ;
5758 this . component = renderIntoDocument (
5859 < ReactWrapperComponent
5960 Component = { nodes . type }
6061 props = { nodes . props }
62+ context = { options . context }
6163 />
6264 ) ;
6365 this . root = this ;
@@ -76,6 +78,7 @@ export default class ReactWrapper {
7678 }
7779 this . length = this . nodes . length ;
7880 }
81+ this . options = options ;
7982 }
8083
8184 /**
@@ -147,7 +150,7 @@ export default class ReactWrapper {
147150 if ( this . root !== this ) {
148151 throw new Error ( 'ReactWrapper::setProps() can only be called on the root' ) ;
149152 }
150- this . component . setProps ( props ) ;
153+ this . component . setChildProps ( props ) ;
151154 return this ;
152155 }
153156
@@ -171,6 +174,29 @@ export default class ReactWrapper {
171174 return this ;
172175 }
173176
177+ /**
178+ * A method that sets the context of the root component, and re-renders. Useful for when you are
179+ * wanting to test how the component behaves over time with changing contexts.
180+ *
181+ * NOTE: can only be called on a wrapper instance that is also the root instance.
182+ *
183+ * @param {Object } context object
184+ * @returns {ReactWrapper }
185+ */
186+ setContext ( context ) {
187+ if ( this . root !== this ) {
188+ throw new Error ( 'ReactWrapper::setContext() can only be called on the root' ) ;
189+ }
190+ if ( ! this . options . context ) {
191+ throw new Error (
192+ 'ShallowWrapper::setContext() can only be called on a wrapper that was originally passed ' +
193+ 'a context option'
194+ ) ;
195+ }
196+ this . component . setChildContext ( context ) ;
197+ return this ;
198+ }
199+
174200 /**
175201 * Whether or not a given react element exists in the mount render tree.
176202 *
0 commit comments