@@ -25,7 +25,8 @@ import { ReactSDKClient } from './client';
25
25
import { OptimizelyVariation } from './Variation' ;
26
26
27
27
describe ( '<OptimizelyExperiment>' , ( ) => {
28
- const variationKey = 'variationResult' ;
28
+ const variationKey = 'matchingVariation' ;
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
30
let resolver : any ;
30
31
let optimizelyMock : ReactSDKClient ;
31
32
let isReady : boolean ;
@@ -40,12 +41,12 @@ describe('<OptimizelyExperiment>', () => {
40
41
} ) ;
41
42
42
43
optimizelyMock = ( {
43
- onReady : jest . fn ( ) . mockImplementation ( config => onReadyPromise ) ,
44
- activate : jest . fn ( ) . mockImplementation ( experimentKey => variationKey ) ,
45
- onUserUpdate : jest . fn ( ) . mockImplementation ( handler => ( ) => { } ) ,
44
+ onReady : jest . fn ( ) . mockImplementation ( ( ) => onReadyPromise ) ,
45
+ activate : jest . fn ( ) . mockImplementation ( ( ) => variationKey ) ,
46
+ onUserUpdate : jest . fn ( ) . mockImplementation ( ( ) => ( ) => { } ) ,
46
47
notificationCenter : {
47
- addNotificationListener : jest . fn ( ) . mockImplementation ( ( type , handler ) => { } ) ,
48
- removeNotificationListener : jest . fn ( ) . mockImplementation ( id => { } ) ,
48
+ addNotificationListener : jest . fn ( ) . mockImplementation ( ( ) => { } ) ,
49
+ removeNotificationListener : jest . fn ( ) . mockImplementation ( ( ) => { } ) ,
49
50
} ,
50
51
user : {
51
52
id : 'testuser' ,
@@ -54,7 +55,7 @@ describe('<OptimizelyExperiment>', () => {
54
55
isReady : jest . fn ( ) . mockImplementation ( ( ) => isReady ) ,
55
56
getIsReadyPromiseFulfilled : ( ) => true ,
56
57
getIsUsingSdkKey : ( ) => true ,
57
- onForcedVariationsUpdate : jest . fn ( ) . mockReturnValue ( ( ) => { } ) ,
58
+ onForcedVariationsUpdate : jest . fn ( ) . mockReturnValue ( ( ) => { } ) ,
58
59
} as unknown ) as ReactSDKClient ;
59
60
} ) ;
60
61
@@ -96,7 +97,7 @@ describe('<OptimizelyExperiment>', () => {
96
97
</ OptimizelyProvider >
97
98
) ;
98
99
99
- await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'variationResult ' ) ) ;
100
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'matchingVariation ' ) ) ;
100
101
101
102
expect ( optimizelyMock . activate ) . toHaveBeenCalledWith ( 'experiment1' , undefined , undefined ) ;
102
103
} ) ;
@@ -150,7 +151,7 @@ describe('<OptimizelyExperiment>', () => {
150
151
< OptimizelyVariation variation = "otherVariation" >
151
152
< span data-testid = "variation-key" > other variation</ span >
152
153
</ OptimizelyVariation >
153
- < OptimizelyVariation variation = "variationResult " >
154
+ < OptimizelyVariation variation = "matchingVariation " >
154
155
< span data-testid = "variation-key" > correct variation</ span >
155
156
</ OptimizelyVariation >
156
157
< OptimizelyVariation default >
@@ -203,7 +204,7 @@ describe('<OptimizelyExperiment>', () => {
203
204
< OptimizelyVariation default >
204
205
< span data-testid = "variation-key" > default variation</ span >
205
206
</ OptimizelyVariation >
206
- < OptimizelyVariation variation = "variationResult " >
207
+ < OptimizelyVariation variation = "matchingVariation " >
207
208
< span data-testid = "variation-key" > matching variation</ span >
208
209
</ OptimizelyVariation >
209
210
</ OptimizelyExperiment >
@@ -246,6 +247,91 @@ describe('<OptimizelyExperiment>', () => {
246
247
await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'default variation' ) ) ;
247
248
} ) ;
248
249
250
+ describe ( 'a OptimizelyVariation with default & variation props' , ( ) => {
251
+ it ( 'should render default with NO matching variations ' , async ( ) => {
252
+ const { container } = render (
253
+ < OptimizelyProvider optimizely = { optimizelyMock } >
254
+ < OptimizelyExperiment experiment = "experiment1" >
255
+ < OptimizelyVariation default variation = "nonMatchingVariation" >
256
+ < span data-testid = "variation-key" > default & non matching variation </ span >
257
+ </ OptimizelyVariation >
258
+ < OptimizelyVariation variation = "anotherNonMatchingVariation" >
259
+ < span data-testid = "variation-key" > another non-matching variation</ span >
260
+ </ OptimizelyVariation >
261
+ </ OptimizelyExperiment >
262
+ </ OptimizelyProvider >
263
+ ) ;
264
+
265
+ // while it's waiting for onReady()
266
+ expect ( container . innerHTML ) . toBe ( '' ) ;
267
+
268
+ // Simulate client becoming ready
269
+ resolver . resolve ( { success : true } ) ;
270
+
271
+ await optimizelyMock . onReady ( ) ;
272
+
273
+ await waitFor ( ( ) =>
274
+ expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'default & non matching variation' )
275
+ ) ;
276
+ } ) ;
277
+
278
+ it ( 'should render matching variation with a default & non-matching ' , async ( ) => {
279
+ const { container } = render (
280
+ < OptimizelyProvider optimizely = { optimizelyMock } >
281
+ < OptimizelyExperiment experiment = "experiment1" >
282
+ < OptimizelyVariation default variation = "nonMatchingVariation" >
283
+ < span data-testid = "variation-key" > default & non matching variation </ span >
284
+ </ OptimizelyVariation >
285
+ < OptimizelyVariation variation = "matchingVariation" >
286
+ < span data-testid = "variation-key" > matching variation</ span >
287
+ </ OptimizelyVariation >
288
+ </ OptimizelyExperiment >
289
+ </ OptimizelyProvider >
290
+ ) ;
291
+
292
+ // while it's waiting for onReady()
293
+ expect ( container . innerHTML ) . toBe ( '' ) ;
294
+
295
+ // Simulate client becoming ready
296
+ resolver . resolve ( { success : true } ) ;
297
+
298
+ await optimizelyMock . onReady ( ) ;
299
+
300
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'matching variation' ) ) ;
301
+ } ) ;
302
+ } ) ;
303
+
304
+ it ( 'should render the last default variation when multiple default props present' , async ( ) => {
305
+ const { container } = render (
306
+ < OptimizelyProvider optimizely = { optimizelyMock } >
307
+ < OptimizelyExperiment experiment = "experiment1" >
308
+ < OptimizelyVariation default variation = "nonMatchingVariation1" >
309
+ < span data-testid = "variation-key" > non-matching variation 1</ span >
310
+ </ OptimizelyVariation >
311
+ < OptimizelyVariation variation = "nonMatchingVariation2" >
312
+ < span data-testid = "variation-key" > non-matching variation 2</ span >
313
+ </ OptimizelyVariation >
314
+ < OptimizelyVariation default variation = "nonMatchingVariation3" >
315
+ < span data-testid = "variation-key" > non-matching variation 3</ span >
316
+ </ OptimizelyVariation >
317
+ < OptimizelyVariation variation = "nonMatchingVariation4" >
318
+ < span data-testid = "variation-key" > non-matching variation 4</ span >
319
+ </ OptimizelyVariation >
320
+ </ OptimizelyExperiment >
321
+ </ OptimizelyProvider >
322
+ ) ;
323
+
324
+ // while it's waiting for onReady()
325
+ expect ( container . innerHTML ) . toBe ( '' ) ;
326
+
327
+ // Simulate client becoming ready
328
+ resolver . resolve ( { success : true } ) ;
329
+
330
+ await optimizelyMock . onReady ( ) ;
331
+
332
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'non-matching variation 3' ) ) ;
333
+ } ) ;
334
+
249
335
it ( 'should render an empty string when no default or matching variation is provided' , async ( ) => {
250
336
const { container } = render (
251
337
< OptimizelyProvider optimizely = { optimizelyMock } >
@@ -296,7 +382,7 @@ describe('<OptimizelyExperiment>', () => {
296
382
297
383
expect ( optimizelyMock . activate ) . toHaveBeenCalledWith ( 'experiment1' , 'james123' , { betaUser : true } ) ;
298
384
299
- await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'variationResult ' ) ) ;
385
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'matchingVariation ' ) ) ;
300
386
} ) ;
301
387
302
388
it ( 'should pass the values for clientReady and didTimeout' , async ( ) => {
@@ -319,7 +405,7 @@ describe('<OptimizelyExperiment>', () => {
319
405
await optimizelyMock . onReady ( ) ;
320
406
321
407
expect ( optimizelyMock . activate ) . toHaveBeenCalledWith ( 'experiment1' , undefined , undefined ) ;
322
- await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'variationResult |true|false' ) ) ;
408
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'matchingVariation |true|false' ) ) ;
323
409
} ) ;
324
410
325
411
describe ( 'when the onReady() promise return { success: false }' , ( ) => {
@@ -371,7 +457,7 @@ describe('<OptimizelyExperiment>', () => {
371
457
372
458
expect ( optimizelyMock . activate ) . toHaveBeenCalledWith ( 'experiment1' , undefined , undefined ) ;
373
459
374
- await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'variationResult ' ) ) ;
460
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'matchingVariation ' ) ) ;
375
461
376
462
// capture the OPTIMIZELY_CONFIG_UPDATE function
377
463
// change the return value of activate
@@ -406,7 +492,7 @@ describe('<OptimizelyExperiment>', () => {
406
492
await act ( async ( ) => await optimizelyMock . onReady ( ) ) ;
407
493
expect ( optimizelyMock . activate ) . toHaveBeenCalledWith ( 'experiment1' , undefined , undefined ) ;
408
494
409
- await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'variationResult ' ) ) ;
495
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'variation-key' ) ) . toHaveTextContent ( 'matchingVariation ' ) ) ;
410
496
411
497
// capture the onUserUpdate function
412
498
const updateFn = ( optimizelyMock . onUserUpdate as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
@@ -442,7 +528,7 @@ describe('<OptimizelyExperiment>', () => {
442
528
< OptimizelyVariation variation = "otherVariation" >
443
529
< span data-testid = "variation-key" > other variation</ span >
444
530
</ OptimizelyVariation >
445
- < OptimizelyVariation variation = "variationResult " >
531
+ < OptimizelyVariation variation = "matchingVariation " >
446
532
< span data-testid = "variation-key" > correct variation</ span >
447
533
</ OptimizelyVariation >
448
534
< OptimizelyVariation default >
0 commit comments