@@ -69,71 +69,145 @@ export function injectFCTrampoline(component: FC, customHooks?: any): FCTrampoli
6969
7070 let renderHookStep = 0 ;
7171
72- // Accessed two times, once directly before class instantiation, and again in some extra logic we don't need to worry about that we hanlde below just in case.
73- Object . defineProperty ( component , "contextType" , {
74- configurable : true ,
75- get : function ( ) {
76- loggingEnabled && logger . debug ( "get contexttype" , this , stubsApplied , renderHookStep ) ;
77- loggingEnabled && console . trace ( "contextType trace" ) ;
78- if ( renderHookStep == 0 ) renderHookStep = 1 ;
79- else if ( renderHookStep == 3 ) renderHookStep = 4 ;
80- return this . _contextType ;
81- } ,
82- set : function ( value ) {
83- this . _contextType = value ;
84- }
85- } ) ;
72+ if ( window . SP_REACTDOM . version . startsWith ( "19." ) ) {
73+ // Accessed two times directly before class instantiation on path A and once on path B
74+ Object . defineProperty ( component , "contextType" , {
75+ configurable : true ,
76+ get : function ( ) {
77+ loggingEnabled && logger . debug ( "get contexttype" , this , this . _contextType , stubsApplied , renderHookStep ) ;
78+ loggingEnabled && console . trace ( "contextType trace" ) ;
79+ if ( renderHookStep == 0 ) {
80+ renderHookStep = 1 ;
81+ }
82+ if ( this . _contextType == null ) {
83+ this . _contextType = { } ;
84+ }
85+ if ( ! this . _contextType . appliedCurrentValueHook ) {
86+ logger . debug ( "applied currentvalue hook" ) ;
87+ this . _contextType . appliedCurrentValueHook = true ;
88+ Object . defineProperty ( this . _contextType , "_currentValue" , {
89+ configurable : true ,
90+ get : function ( ) {
91+ loggingEnabled && logger . debug ( "get currentValue" , this , stubsApplied , renderHookStep ) ;
92+ loggingEnabled && console . trace ( "currentValue trace" ) ;
93+ if ( renderHookStep == 1 ) {
94+ renderHookStep = 2 ;
95+ applyStubsIfNeeded ( ) ;
96+ }
97+ return this . __currentValue ;
98+ } ,
99+ set : function ( value ) {
100+ return this . __currentValue = value ;
101+ }
102+ } ) ;
103+ }
104+ return this . _contextType ;
105+ } ,
106+ set : function ( value ) {
107+ this . _contextType = value ;
108+ }
109+ } ) ;
86110
87- // Always accessed directly after contextType for the path we want to catch.
88- Object . defineProperty ( component , "contextTypes " , {
89- configurable : true ,
90- get : function ( ) {
91- loggingEnabled && logger . debug ( "get contexttypes" , this , stubsApplied , renderHookStep ) ;
92- loggingEnabled && console . trace ( "contextTypes trace" ) ;
93- if ( renderHookStep == 1 ) {
94- renderHookStep = 2 ;
95- applyStubsIfNeeded ( ) ;
96- } ;
97- return this . _contextTypes ;
98- } ,
99- set : function ( value ) {
100- this . _contextTypes = value ;
101- }
102- } ) ;
111+ // Set directly after class is instantiated
112+ Object . defineProperty ( component . prototype , "updater " , {
113+ configurable : true ,
114+ get : function ( ) {
115+ return this . _updater ;
116+ } ,
117+ set : function ( value ) {
118+ loggingEnabled && logger . debug ( "set updater" , this , value , stubsApplied , renderHookStep ) ;
119+ loggingEnabled && console . trace ( "updater trace" ) ;
120+ if ( renderHookStep == 1 || renderHookStep == 2 ) {
121+ renderHookStep = 0 ;
122+ removeStubsIfNeeded ( ) ;
123+ }
124+ return this . _updater = value ;
125+ }
126+ } ) ;
103127
104- // Set directly after class is instantiated
105- Object . defineProperty ( component . prototype , "updater" , {
106- configurable : true ,
107- get : function ( ) {
108- return this . _updater ;
109- } ,
110- set : function ( value ) {
111- loggingEnabled && logger . debug ( "set updater" , this , value , stubsApplied , renderHookStep ) ;
112- loggingEnabled && console . trace ( "updater trace" ) ;
113- if ( renderHookStep == 2 ) {
114- renderHookStep = 0 ;
115- removeStubsIfNeeded ( ) ;
128+ // Prevents the second contextType access from leaving its hooks around
129+ Object . defineProperty ( component , "getDerivedStateFromProps" , {
130+ configurable : true ,
131+ get : function ( ) {
132+ loggingEnabled && logger . debug ( "get getDerivedStateFromProps" , this , stubsApplied , renderHookStep ) ;
133+ loggingEnabled && console . trace ( "getDerivedStateFromProps trace" ) ;
134+ if ( renderHookStep == 1 || renderHookStep == 2 ) {
135+ renderHookStep = 0 ;
136+ removeStubsIfNeeded ( ) ;
137+ }
138+ return this . _getDerivedStateFromProps ;
139+ } ,
140+ set : function ( value ) {
141+ this . _getDerivedStateFromProps = value ;
116142 }
117- return this . _updater = value ;
118- }
119- } ) ;
143+ } ) ;
144+ } else if ( window . SP_REACTDOM . version . startsWith ( "18." ) ) {
145+ // Accessed two times, once directly before class instantiation, and again in some extra logic we don't need to worry about that we hanlde below just in case.
146+ Object . defineProperty ( component , "contextType" , {
147+ configurable : true ,
148+ get : function ( ) {
149+ loggingEnabled && logger . debug ( "get contexttype" , this , stubsApplied , renderHookStep ) ;
150+ loggingEnabled && console . trace ( "contextType trace" ) ;
151+ if ( renderHookStep == 0 ) renderHookStep = 1 ;
152+ else if ( renderHookStep == 3 ) renderHookStep = 4 ;
153+ return this . _contextType ;
154+ } ,
155+ set : function ( value ) {
156+ this . _contextType = value ;
157+ }
158+ } ) ;
120159
121- // Prevents the second contextType+contextTypes access from leaving its hooks around
122- Object . defineProperty ( component , "getDerivedStateFromProps" , {
123- configurable : true ,
124- get : function ( ) {
125- loggingEnabled && logger . debug ( "get getDerivedStateFromProps" , this , stubsApplied , renderHookStep ) ;
126- loggingEnabled && console . trace ( "getDerivedStateFromProps trace" ) ;
127- if ( renderHookStep == 2 ) {
128- renderHookStep = 0 ;
129- removeStubsIfNeeded ( ) ;
160+ // Always accessed directly after contextType for the path we want to catch.
161+ Object . defineProperty ( component , "contextTypes" , {
162+ configurable : true ,
163+ get : function ( ) {
164+ loggingEnabled && logger . debug ( "get contexttypes" , this , stubsApplied , renderHookStep ) ;
165+ loggingEnabled && console . trace ( "contextTypes trace" ) ;
166+ if ( renderHookStep == 1 ) {
167+ renderHookStep = 2 ;
168+ applyStubsIfNeeded ( ) ;
169+ } ;
170+ return this . _contextTypes ;
171+ } ,
172+ set : function ( value ) {
173+ this . _contextTypes = value ;
130174 }
131- return this . _getDerivedStateFromProps ;
132- } ,
133- set : function ( value ) {
134- this . _getDerivedStateFromProps = value ;
135- }
136- } ) ;
175+ } ) ;
176+
177+ // Set directly after class is instantiated
178+ Object . defineProperty ( component . prototype , "updater" , {
179+ configurable : true ,
180+ get : function ( ) {
181+ return this . _updater ;
182+ } ,
183+ set : function ( value ) {
184+ loggingEnabled && logger . debug ( "set updater" , this , value , stubsApplied , renderHookStep ) ;
185+ loggingEnabled && console . trace ( "updater trace" ) ;
186+ if ( renderHookStep == 2 ) {
187+ renderHookStep = 0 ;
188+ removeStubsIfNeeded ( ) ;
189+ }
190+ return this . _updater = value ;
191+ }
192+ } ) ;
193+
194+ // Prevents the second contextType+contextTypes access from leaving its hooks around
195+ Object . defineProperty ( component , "getDerivedStateFromProps" , {
196+ configurable : true ,
197+ get : function ( ) {
198+ loggingEnabled && logger . debug ( "get getDerivedStateFromProps" , this , stubsApplied , renderHookStep ) ;
199+ loggingEnabled && console . trace ( "getDerivedStateFromProps trace" ) ;
200+ if ( renderHookStep == 2 ) {
201+ renderHookStep = 0 ;
202+ removeStubsIfNeeded ( ) ;
203+ }
204+ return this . _getDerivedStateFromProps ;
205+ } ,
206+ set : function ( value ) {
207+ this . _getDerivedStateFromProps = value ;
208+ }
209+ } ) ;
210+ }
137211
138212 return userComponent ;
139213}
0 commit comments