@@ -82,7 +82,7 @@ describe('useSplitClient', () => {
82
82
83
83
// eslint-disable-next-line react/prop-types
84
84
const InnerComponent = ( { splitKey, attributesClient, testSwitch } ) => {
85
- useSplitClient ( { splitKey, trafficType : 'user' , attributes : attributesClient } ) ;
85
+ useSplitClient ( { splitKey, trafficType : 'user' , attributes : attributesClient } ) ;
86
86
testSwitch ( done , splitKey ) ;
87
87
return null ;
88
88
} ;
@@ -98,13 +98,13 @@ describe('useSplitClient', () => {
98
98
testAttributesBinding ( Component ) ;
99
99
} ) ;
100
100
101
- test ( 'useSplitClient must update on SDK events' , ( ) => {
101
+ test ( 'must update on SDK events' , ( ) => {
102
102
const outerFactory = SplitSdk ( sdkBrowser ) ;
103
103
const mainClient = outerFactory . client ( ) as any ;
104
104
const user2Client = outerFactory . client ( 'user_2' ) as any ;
105
105
106
106
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 ;
108
108
let countNestedComponent = 0 ;
109
109
110
110
render (
@@ -150,8 +150,8 @@ describe('useSplitClient', () => {
150
150
{ ( ) => { countSplitClientUser2WithUpdate ++ ; return null } }
151
151
</ SplitClient >
152
152
{ React . createElement ( ( ) => {
153
- useSplitClient ( { splitKey : 'user_2' , updateOnSdkUpdate : true } ) ;
154
- countUseSplitClientUser2WithUpdate ++ ;
153
+ useSplitClient ( { splitKey : 'user_2' , updateOnSdkTimedout : true } ) ;
154
+ countUseSplitClientUser2WithTimeout ++ ;
155
155
return null ;
156
156
} ) }
157
157
< SplitClient splitKey = { 'user_2' } updateOnSdkUpdate = { true } >
@@ -190,6 +190,7 @@ describe('useSplitClient', () => {
190
190
act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_READY ) ) ;
191
191
act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ;
192
192
act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_READY_FROM_CACHE ) ) ;
193
+ act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_READY_TIMED_OUT ) ) ;
193
194
act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_READY ) ) ;
194
195
act ( ( ) => user2Client . __emitter__ . emit ( Event . SDK_UPDATE ) ) ;
195
196
@@ -214,12 +215,35 @@ describe('useSplitClient', () => {
214
215
// If SplitClient and useSplitClient retrieve a different client than the context and have updateOnSdkUpdate = true,
215
216
// they render when the context renders and when the new client is ready, ready from cache and updates.
216
217
expect ( countSplitClientUser2WithUpdate ) . toEqual ( countSplitContext + 3 ) ;
217
- expect ( countUseSplitClientUser2WithUpdate ) . toEqual ( countSplitContext + 3 ) ;
218
+ expect ( countUseSplitClientUser2WithTimeout ) . toEqual ( countSplitContext + 3 ) ;
218
219
219
220
expect ( countNestedComponent ) . toEqual ( 4 ) ;
220
221
} ) ;
221
222
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' , ( ) => {
223
247
const outerFactory = SplitSdk ( sdkBrowser ) ;
224
248
const mainClient = outerFactory . client ( ) as any ;
225
249
@@ -240,19 +264,24 @@ describe('useSplitClient', () => {
240
264
}
241
265
242
266
const wrapper = render ( < Component /> ) ;
267
+ expect ( rendersCount ) . toBe ( 1 ) ;
243
268
244
269
act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_READY ) ) ; // trigger re-render
270
+ expect ( rendersCount ) . toBe ( 2 ) ;
271
+
245
272
act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // do not trigger re-render because updateOnSdkUpdate is false by default
246
273
expect ( rendersCount ) . toBe ( 2 ) ;
247
274
248
275
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 ) ;
250
277
278
+ act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // trigger re-render because updateOnSdkUpdate is true now
251
279
expect ( rendersCount ) . toBe ( 4 ) ;
252
280
253
281
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 ) ;
255
283
284
+ act ( ( ) => mainClient . __emitter__ . emit ( Event . SDK_UPDATE ) ) ; // do not trigger re-render because updateOnSdkUpdate is false now
256
285
expect ( rendersCount ) . toBe ( 5 ) ;
257
286
} ) ;
258
287
0 commit comments