@@ -82,7 +82,7 @@ describe('useSplitClient', () => {
8282
8383 // eslint-disable-next-line react/prop-types
8484 const InnerComponent = ( { splitKey, attributesClient, testSwitch } ) => {
85- useSplitClient ( { splitKey, trafficType : 'user' , attributes : attributesClient } ) ;
85+ useSplitClient ( { splitKey, trafficType : 'user' , attributes : attributesClient } ) ;
8686 testSwitch ( done , splitKey ) ;
8787 return null ;
8888 } ;
@@ -98,13 +98,13 @@ describe('useSplitClient', () => {
9898 testAttributesBinding ( Component ) ;
9999 } ) ;
100100
101- test ( 'useSplitClient must update on SDK events' , ( ) => {
101+ test ( 'must update on SDK events' , ( ) => {
102102 const outerFactory = SplitSdk ( sdkBrowser ) ;
103103 const mainClient = outerFactory . client ( ) as any ;
104104 const user2Client = outerFactory . client ( 'user_2' ) as any ;
105105
106106 let countSplitContext = 0 , countSplitClient = 0 , countSplitClientUser2 = 0 , countUseSplitClient = 0 , countUseSplitClientUser2 = 0 ;
107- let countSplitClientWithUpdate = 0 , countUseSplitClientWithUpdate = 0 , countSplitClientUser2WithUpdate = 0 , countUseSplitClientUser2WithUpdate = 0 ;
107+ let countSplitClientWithUpdate = 0 , countUseSplitClientWithUpdate = 0 , countSplitClientUser2WithUpdate = 0 , countUseSplitClientUser2WithTimeout = 0 ;
108108 let countNestedComponent = 0 ;
109109
110110 render (
@@ -150,8 +150,8 @@ describe('useSplitClient', () => {
150150 { ( ) => { countSplitClientUser2WithUpdate ++ ; return null } }
151151 </ SplitClient >
152152 { React . createElement ( ( ) => {
153- useSplitClient ( { splitKey : 'user_2' , updateOnSdkUpdate : true } ) ;
154- countUseSplitClientUser2WithUpdate ++ ;
153+ useSplitClient ( { splitKey : 'user_2' , updateOnSdkTimedout : true } ) ;
154+ countUseSplitClientUser2WithTimeout ++ ;
155155 return null ;
156156 } ) }
157157 < SplitClient splitKey = { 'user_2' } updateOnSdkUpdate = { true } >
@@ -190,6 +190,7 @@ describe('useSplitClient', () => {
190190 act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_READY ) ) ;
191191 act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ;
192192 act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_READY_FROM_CACHE ) ) ;
193+ act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_READY_TIMED_OUT ) ) ;
193194 act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_READY ) ) ;
194195 act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_UPDATE ) ) ;
195196
@@ -214,12 +215,35 @@ describe('useSplitClient', () => {
214215 // If SplitClient and useSplitClient retrieve a different client than the context and have updateOnSdkUpdate = true,
215216 // they render when the context renders and when the new client is ready, ready from cache and updates.
216217 expect ( countSplitClientUser2WithUpdate ) . toEqual ( countSplitContext + 3 ) ;
217- expect ( countUseSplitClientUser2WithUpdate ) . toEqual ( countSplitContext + 3 ) ;
218+ expect ( countUseSplitClientUser2WithTimeout ) . toEqual ( countSplitContext + 3 ) ;
218219
219220 expect ( countNestedComponent ) . toEqual ( 4 ) ;
220221 } ) ;
221222
222- test ( 'useSplitClient must support changes in update props' , ( ) => {
223+ // Remove this test once side effects are moved to the useSplitClient effect.
224+ test ( 'must update on SDK events between the render phase (hook call) and commit phase (effect call)' , ( ) => {
225+ const outerFactory = SplitSdk ( sdkBrowser ) ;
226+ let count = 0 ;
227+
228+ render (
229+ < SplitFactory factory = { outerFactory } >
230+ { React . createElement ( ( ) => {
231+ useSplitClient ( { splitKey : 'some_user' } ) ;
232+ count ++ ;
233+
234+ // side effect in the render phase
235+ const client = outerFactory . client ( 'some_user' ) as any ;
236+ if ( ! client . __getStatus ( ) . isReady ) client . __emitter__ . emit ( Event . SDK_READY ) ;
237+
238+ return null ;
239+ } ) }
240+ </ SplitFactory >
241+ )
242+
243+ expect ( count ) . toEqual ( 2 ) ;
244+ } ) ;
245+
246+ test ( 'must support changes in update props' , ( ) => {
223247 const outerFactory = SplitSdk ( sdkBrowser ) ;
224248 const mainClient = outerFactory . client ( ) as any ;
225249
@@ -240,19 +264,24 @@ describe('useSplitClient', () => {
240264 }
241265
242266 const wrapper = render ( < Component /> ) ;
267+ expect ( rendersCount ) . toBe ( 1 ) ;
243268
244269 act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_READY ) ) ; // trigger re-render
270+ expect ( rendersCount ) . toBe ( 2 ) ;
271+
245272 act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // do not trigger re-render because updateOnSdkUpdate is false by default
246273 expect ( rendersCount ) . toBe ( 2 ) ;
247274
248275 wrapper . rerender ( < Component updateOnSdkUpdate = { true } /> ) ; // trigger re-render
249- act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // trigger re-render because updateOnSdkUpdate is true now
276+ expect ( rendersCount ) . toBe ( 3 ) ;
250277
278+ act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // trigger re-render because updateOnSdkUpdate is true now
251279 expect ( rendersCount ) . toBe ( 4 ) ;
252280
253281 wrapper . rerender ( < Component updateOnSdkUpdate = { false } /> ) ; // trigger re-render
254- act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // do not trigger re-render because updateOnSdkUpdate is false now
282+ expect ( rendersCount ) . toBe ( 5 ) ;
255283
284+ act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // do not trigger re-render because updateOnSdkUpdate is false now
256285 expect ( rendersCount ) . toBe ( 5 ) ;
257286 } ) ;
258287
0 commit comments