@@ -116,6 +116,44 @@ describe('ReactLazy', () => {
116116 expect ( root ) . toMatchRenderedOutput ( 'Hi again' ) ;
117117 } ) ;
118118
119+ it ( 'renders a lazy context provider' , async ( ) => {
120+ const Context = React . createContext ( 'default' ) ;
121+ function ConsumerText ( ) {
122+ return < Text text = { React . useContext ( Context ) } /> ;
123+ }
124+ // Context.Provider === Context, so we can lazy-load the context itself
125+ const LazyProvider = lazy ( ( ) => fakeImport ( Context ) ) ;
126+
127+ const root = ReactTestRenderer . create (
128+ < Suspense fallback = { < Text text = "Loading..." /> } >
129+ < LazyProvider value = "Hi" >
130+ < ConsumerText />
131+ </ LazyProvider >
132+ </ Suspense > ,
133+ {
134+ unstable_isConcurrent : true ,
135+ } ,
136+ ) ;
137+
138+ await waitForAll ( [ 'Loading...' ] ) ;
139+ expect ( root ) . not . toMatchRenderedOutput ( 'Hi' ) ;
140+
141+ await act ( ( ) => resolveFakeImport ( Context ) ) ;
142+ assertLog ( [ 'Hi' ] ) ;
143+ expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
144+
145+ // Should not suspend on update
146+ root . update (
147+ < Suspense fallback = { < Text text = "Loading..." /> } >
148+ < LazyProvider value = "Hi again" >
149+ < ConsumerText />
150+ </ LazyProvider >
151+ </ Suspense > ,
152+ ) ;
153+ await waitForAll ( [ 'Hi again' ] ) ;
154+ expect ( root ) . toMatchRenderedOutput ( 'Hi again' ) ;
155+ } ) ;
156+
119157 it ( 'can resolve synchronously without suspending' , async ( ) => {
120158 const LazyText = lazy ( ( ) => ( {
121159 then ( cb ) {
@@ -858,13 +896,20 @@ describe('ReactLazy', () => {
858896 ) ;
859897 } ) ;
860898
861- it ( 'throws with a useful error when wrapping Context with lazy()' , async ( ) => {
862- const Context = React . createContext ( null ) ;
863- const BadLazy = lazy ( ( ) => fakeImport ( Context ) ) ;
899+ it ( 'renders a lazy context provider without value prop' , async ( ) => {
900+ // Context providers work when wrapped in lazy()
901+ const Context = React . createContext ( 'default' ) ;
902+ const LazyProvider = lazy ( ( ) => fakeImport ( Context ) ) ;
903+
904+ function ConsumerText ( ) {
905+ return < Text text = { React . useContext ( Context ) } /> ;
906+ }
864907
865908 const root = ReactTestRenderer . create (
866909 < Suspense fallback = { < Text text = "Loading..." /> } >
867- < BadLazy />
910+ < LazyProvider value = "provided" >
911+ < ConsumerText />
912+ </ LazyProvider >
868913 </ Suspense > ,
869914 {
870915 unstable_isConcurrent : true ,
@@ -873,16 +918,9 @@ describe('ReactLazy', () => {
873918
874919 await waitForAll ( [ 'Loading...' ] ) ;
875920
876- await resolveFakeImport ( Context ) ;
877- root . update (
878- < Suspense fallback = { < Text text = "Loading..." /> } >
879- < BadLazy />
880- </ Suspense > ,
881- ) ;
882- await waitForThrow (
883- 'Element type is invalid. Received a promise that resolves to: Context. ' +
884- 'Lazy element type must resolve to a class or function.' ,
885- ) ;
921+ await act ( ( ) => resolveFakeImport ( Context ) ) ;
922+ assertLog ( [ 'provided' ] ) ;
923+ expect ( root ) . toMatchRenderedOutput ( 'provided' ) ;
886924 } ) ;
887925
888926 it ( 'throws with a useful error when wrapping Context.Consumer with lazy()' , async ( ) => {
0 commit comments