@@ -226,20 +226,20 @@ export default function (fetchMock, assert) {
226226 t . true ( Date . now ( ) - startTime >= 400 , 'It should emit SDK_READY too but after syncing with the cloud.' ) ;
227227 t . equal ( client . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
228228 } ) ;
229- client . ready ( ) . then ( ( ) => {
229+ client . whenReady ( ) . then ( ( ) => {
230230 t . true ( Date . now ( ) - startTime >= 400 , 'It should resolve ready promise after syncing with the cloud.' ) ;
231231 t . equal ( client . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
232232 } ) ;
233233 client2 . on ( client2 . Event . SDK_READY , ( ) => {
234234 t . true ( Date . now ( ) - startTime >= 700 , 'It should emit SDK_READY too but after syncing with the cloud.' ) ;
235235 t . equal ( client2 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
236236 } ) ;
237- client2 . ready ( ) . then ( ( ) => {
237+ client2 . whenReady ( ) . then ( ( ) => {
238238 t . true ( Date . now ( ) - startTime >= 700 , 'It should resolve ready promise after syncing with the cloud.' ) ;
239239 t . equal ( client2 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
240240 } ) ;
241241 client3 . on ( client3 . Event . SDK_READY , ( ) => {
242- client3 . ready ( ) . then ( ( ) => {
242+ client3 . whenReady ( ) . then ( ( ) => {
243243 t . true ( Date . now ( ) - startTime >= 1000 , 'It should resolve ready promise after syncing with the cloud.' ) ;
244244 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
245245
@@ -254,7 +254,7 @@ export default function (fetchMock, assert) {
254254 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
255255 } ) ;
256256 client3 . on ( client3 . Event . SDK_READY_TIMED_OUT , ( ) => {
257- client3 . ready ( ) . catch ( ( ) => {
257+ client3 . whenReady ( ) . catch ( ( ) => {
258258 t . true ( Date . now ( ) - startTime >= 850 , 'It should reject ready promise before syncing memberships data with the cloud.' ) ;
259259 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with memberships data from cache.' ) ;
260260 } ) ;
@@ -269,7 +269,7 @@ export default function (fetchMock, assert) {
269269 events : 'https://events.baseurl/readyFromCacheWithData3'
270270 } ;
271271 localStorage . clear ( ) ;
272- t . plan ( 12 * 2 + 5 ) ;
272+ t . plan ( 12 * 2 + 14 ) ;
273273
274274 fetchMock . get ( testUrls . sdk + '/splitChanges?s=1.3&since=25&rbSince=-1' , ( ) => {
275275 t . equal ( localStorage . getItem ( 'readyFromCache_3.SPLITIO.split.always_on' ) , alwaysOnSplitInverted , 'feature flags must not be cleaned from cache' ) ;
@@ -285,8 +285,16 @@ export default function (fetchMock, assert) {
285285 return new Promise ( res => { setTimeout ( ( ) => res ( { status : 200 , body : { 'ms' : { } } , headers : { } } ) , 1000 ) ; } ) ; // Third client memberships will come after 1s
286286 } ) ;
287287 fetchMock . get ( testUrls . sdk + '/memberships/nicolas4%40split.io' , { 'ms' : { } } ) ;
288- fetchMock . postOnce ( testUrls . events + '/testImpressions/bulk' , 200 ) ;
289- fetchMock . postOnce ( testUrls . events + '/testImpressions/count' , 200 ) ;
288+ fetchMock . postOnce ( testUrls . events + '/testImpressions/bulk' , ( _ , opts ) => {
289+ const payload = JSON . parse ( opts . body ) ;
290+ t . equal ( payload . length , 1 , 'Only one flag was evaluated' ) ;
291+ t . equal ( payload [ 0 ] . i . length , 14 , '14 impressions were queued, one per getTreatment call' ) ;
292+ t . equal ( payload [ 0 ] . i . filter ( ( imp ) => imp . t === 'control' ) . length , 2 , '2 impressions were queued for control (not ready from cache)' ) ;
293+ t . equal ( payload [ 0 ] . i . filter ( ( imp ) => imp . t === 'off' ) . length , 4 , '4 impressions were queued for off (ready from cache)' ) ;
294+ t . equal ( payload [ 0 ] . i . filter ( ( imp ) => imp . t === 'on' ) . length , 8 , '8 impressions were queued for on (ready)' ) ;
295+
296+ return 200 ;
297+ } ) ;
290298
291299 localStorage . setItem ( 'some_user_item' , 'user_item' ) ;
292300 localStorage . setItem ( 'readyFromCache_3.SPLITIO.splits.till' , 25 ) ;
@@ -301,6 +309,9 @@ export default function (fetchMock, assert) {
301309 type : 'LOCALSTORAGE' ,
302310 prefix : 'readyFromCache_3'
303311 } ,
312+ sync : {
313+ impressionsMode : 'DEBUG'
314+ } ,
304315 startup : {
305316 readyTimeout : 0.85
306317 } ,
@@ -320,6 +331,8 @@ export default function (fetchMock, assert) {
320331
321332 client . on ( client . Event . SDK_READY_FROM_CACHE , ( ) => {
322333 t . true ( Date . now ( ) - startTime < 400 , 'It should emit SDK_READY_FROM_CACHE on every client if there was data in the cache and we subscribe on time. Should be considerably faster than actual readiness from the cloud.' ) ;
334+ t . false ( client . __getStatus ( ) . isReady , 'It should not be ready yet' ) ;
335+
323336 t . equal ( client . getTreatment ( 'always_on' ) , 'off' , 'It should evaluate treatments with data from cache instead of control due to Input Validation' ) ;
324337
325338 const client4 = splitio . client ( '[email protected] ' ) ; @@ -342,20 +355,27 @@ export default function (fetchMock, assert) {
342355 t . true ( Date . now ( ) - startTime >= 400 , 'It should emit SDK_READY too but after syncing with the cloud.' ) ;
343356 t . equal ( client . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
344357 } ) ;
345- client . ready ( ) . then ( ( ) => {
358+ client . whenReadyFromCache ( ) . then ( ( isReady ) => {
359+ t . false ( isReady , 'It should be ready from cache before ready (syncing with the cloud).' ) ;
360+ t . true ( Date . now ( ) - startTime < 50 , 'It should resolve ready from cache promise almost immediately.' ) ;
361+ } ) ;
362+ client . whenReady ( ) . then ( ( ) => {
346363 t . true ( Date . now ( ) - startTime >= 400 , 'It should resolve ready promise after syncing with the cloud.' ) ;
347364 t . equal ( client . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
348365 } ) ;
349366 client2 . on ( client2 . Event . SDK_READY , ( ) => {
350367 t . true ( Date . now ( ) - startTime >= 700 , 'It should emit SDK_READY too but after syncing with the cloud.' ) ;
351368 t . equal ( client2 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
352369 } ) ;
353- client2 . ready ( ) . then ( ( ) => {
370+ client2 . whenReadyFromCache ( ) . then ( ( isReady ) => {
371+ t . false ( isReady , 'It should be ready from cache before ready (syncing with the cloud).' ) ;
372+ } ) ;
373+ client2 . whenReady ( ) . then ( ( ) => {
354374 t . true ( Date . now ( ) - startTime >= 700 , 'It should resolve ready promise after syncing with the cloud.' ) ;
355375 t . equal ( client2 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
356376 } ) ;
357377 client3 . on ( client3 . Event . SDK_READY , ( ) => {
358- client3 . ready ( ) . then ( ( ) => {
378+ client3 . whenReady ( ) . then ( ( ) => {
359379 t . true ( Date . now ( ) - startTime >= 1000 , 'It should resolve ready promise after syncing with the cloud.' ) ;
360380 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
361381
@@ -370,7 +390,7 @@ export default function (fetchMock, assert) {
370390 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
371391 } ) ;
372392 client3 . on ( client3 . Event . SDK_READY_TIMED_OUT , ( ) => {
373- client3 . ready ( ) . catch ( ( ) => {
393+ client3 . whenReady ( ) . catch ( ( ) => {
374394 t . true ( Date . now ( ) - startTime >= 850 , 'It should reject ready promise before syncing memberships data with the cloud.' ) ;
375395 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with memberships data from cache.' ) ;
376396 } ) ;
@@ -452,20 +472,24 @@ export default function (fetchMock, assert) {
452472 t . true ( nearlyEqual ( Date . now ( ) - startTime , CLIENT_READY_MS ) , 'It should emit SDK_READY after syncing with the cloud.' ) ;
453473 t . equal ( client . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
454474 } ) ;
455- client . ready ( ) . then ( ( ) => {
475+ client . whenReadyFromCache ( ) . then ( ( isReady ) => {
476+ t . true ( isReady , 'It should be ready from cache and ready.' ) ;
477+ t . true ( nearlyEqual ( Date . now ( ) - startTime , CLIENT_READY_MS ) , 'It should resolve ready from cache promise after syncing with the cloud.' ) ;
478+ } ) ;
479+ client . whenReady ( ) . then ( ( ) => {
456480 t . true ( nearlyEqual ( Date . now ( ) - startTime , CLIENT_READY_MS ) , 'It should resolve ready promise after syncing with the cloud.' ) ;
457481 t . equal ( client . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
458482 } ) ;
459483 client2 . on ( client2 . Event . SDK_READY , ( ) => {
460484 t . true ( nearlyEqual ( Date . now ( ) - startTime , CLIENT2_READY_MS ) , 'It should emit SDK_READY after syncing with the cloud.' ) ;
461485 t . equal ( client2 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
462486 } ) ;
463- client2 . ready ( ) . then ( ( ) => {
487+ client2 . whenReady ( ) . then ( ( ) => {
464488 t . true ( nearlyEqual ( Date . now ( ) - startTime , CLIENT2_READY_MS ) , 'It should resolve ready promise after syncing with the cloud.' ) ;
465489 t . equal ( client2 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
466490 } ) ;
467491 client3 . on ( client3 . Event . SDK_READY , ( ) => {
468- client3 . ready ( ) . then ( ( ) => {
492+ client3 . whenReady ( ) . then ( ( ) => {
469493 t . true ( nearlyEqual ( Date . now ( ) - startTime , CLIENT3_READY_MS ) , 'It should resolve ready promise after syncing with the cloud.' ) ;
470494 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
471495
@@ -482,7 +506,7 @@ export default function (fetchMock, assert) {
482506 t . equal ( client3 . getTreatment ( 'always_on' ) , 'on' , 'It should evaluate treatments with updated data after syncing with the cloud.' ) ;
483507 } ) ;
484508 client3 . on ( client3 . Event . SDK_READY_TIMED_OUT , ( ) => {
485- client3 . ready ( ) . catch ( ( ) => {
509+ client3 . whenReady ( ) . catch ( ( ) => {
486510 t . true ( Date . now ( ) - startTime >= 850 , 'It should reject ready promise before syncing memberships data with the cloud.' ) ;
487511 t . equal ( client3 . getTreatment ( 'always_on' ) , 'control' , 'It should not evaluate treatments with memberships data from cache.' ) ;
488512 } ) ;
@@ -898,7 +922,7 @@ export default function (fetchMock, assert) {
898922 t . true ( client . __getStatus ( ) . isReady , 'Client should emit SDK_READY_FROM_CACHE alongside SDK_READY, because clearOnInit is true' ) ;
899923 } ) ;
900924
901- await client . ready ( ) ;
925+ await client . whenReady ( ) ;
902926
903927 t . true ( console . log . calledWithMatch ( 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache' ) , 'It should log a message about cleaning up cache' ) ;
904928
0 commit comments