@@ -7,6 +7,7 @@ const ONYX_KEYS = {
77 COLLECTION : {
88 TEST_KEY : 'test_' ,
99 TEST_CONNECT_COLLECTION : 'test_connect_collection_' ,
10+ TEST_POLICY : 'test_policy_' ,
1011 } ,
1112} ;
1213
@@ -470,11 +471,10 @@ describe('Onyx', () => {
470471 } ) ;
471472
472473 it ( 'should return all collection keys as a single object when waitForCollectionCallback = true' , ( ) => {
473- const valuesReceived = { } ;
474- const mockCallback = jest . fn ( data => valuesReceived [ data . ID ] = data . value ) ;
474+ const mockCallback = jest . fn ( ) ;
475475
476- // GIVEN some collection data
477- const collectionData = {
476+ // GIVEN some initial collection data
477+ const initialCollectionData = {
478478 test_connect_collection_1 : {
479479 ID : 123 ,
480480 value : 'one' ,
@@ -488,10 +488,11 @@ describe('Onyx', () => {
488488 value : 'three' ,
489489 } ,
490490 } ;
491- Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST_CONNECT_COLLECTION , collectionData ) ;
491+
492+ Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST_CONNECT_COLLECTION , initialCollectionData ) ;
492493 return waitForPromisesToResolve ( )
493494 . then ( ( ) => {
494- // WHEN we call Onyx. connect with the waitForCollectionCallback = true param
495+ // WHEN we connect to that collection with waitForCollectionCallback = true
495496 connectionID = Onyx . connect ( {
496497 key : ONYX_KEYS . COLLECTION . TEST_CONNECT_COLLECTION ,
497498 waitForCollectionCallback : true ,
@@ -500,11 +501,40 @@ describe('Onyx', () => {
500501 return waitForPromisesToResolve ( ) ;
501502 } )
502503 . then ( ( ) => {
503- // THEN the callback should be triggered only once
504+ // THEN we expect the callback to be called only once and the initial stored value to be initialCollectionData
504505 expect ( mockCallback . mock . calls . length ) . toBe ( 1 ) ;
506+ expect ( mockCallback . mock . calls [ 0 ] [ 0 ] ) . toEqual ( initialCollectionData ) ;
507+ } ) ;
508+ } ) ;
509+
510+ it ( 'should return all collection keys as a single object when updating a collection key with waitForCollectionCallback = true' , ( ) => {
511+ const mockCallback = jest . fn ( ) ;
512+ const collectionUpdate = {
513+ test_policy_1 : { ID : 234 , value : 'one' } ,
514+ test_policy_2 : { ID : 123 , value : 'two' } ,
515+ } ;
516+
517+ // GIVEN an Onyx.connect call with waitForCollectionCallback=true
518+ connectionID = Onyx . connect ( {
519+ key : ONYX_KEYS . COLLECTION . TEST_POLICY ,
520+ waitForCollectionCallback : true ,
521+ callback : mockCallback ,
522+ } ) ;
523+ return waitForPromisesToResolve ( )
524+ . then ( ( ) => {
525+ // WHEN mergeCollection is called with an updated collection
526+ Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST_POLICY , collectionUpdate ) ;
527+ return waitForPromisesToResolve ( ) ;
528+ } )
529+ . then ( ( ) => {
530+ // THEN we expect the callback to have called twice, once for the initial connect call + once for the collection update
531+ expect ( mockCallback . mock . calls . length ) . toBe ( 2 ) ;
532+
533+ // AND the value for the first call should be null since the collection was not initialized at that point
534+ expect ( mockCallback . mock . calls [ 0 ] [ 0 ] ) . toBe ( null ) ;
505535
506- // AND all the collection data should be returned as a single object
507- expect ( mockCallback . mock . calls [ 0 ] [ 0 ] ) . toEqual ( collectionData ) ;
536+ // AND the value for the second call should be collectionUpdate since the collection was updated
537+ expect ( mockCallback . mock . calls [ 1 ] [ 0 ] ) . toEqual ( collectionUpdate ) ;
508538 } ) ;
509539 } ) ;
510540} ) ;
0 commit comments