@@ -6,6 +6,7 @@ const ONYX_KEYS = {
66 COLLECTION : {
77 TEST_KEY : 'test_' ,
88 } ,
9+ SINGLE_KEY : 'single' ,
910} ;
1011
1112describe ( 'Collection hydration with connect() followed by immediate set()' , ( ) => {
@@ -15,6 +16,7 @@ describe('Collection hydration with connect() followed by immediate set()', () =
1516 await StorageMock . setItem ( `${ ONYX_KEYS . COLLECTION . TEST_KEY } 1` , { id : 1 , title : 'Test One' } ) ;
1617 await StorageMock . setItem ( `${ ONYX_KEYS . COLLECTION . TEST_KEY } 2` , { id : 2 , title : 'Test Two' } ) ;
1718 await StorageMock . setItem ( `${ ONYX_KEYS . COLLECTION . TEST_KEY } 3` , { id : 3 , title : 'Test Three' } ) ;
19+ await StorageMock . setItem ( ONYX_KEYS . SINGLE_KEY , { title : 'old' } ) ;
1820
1921 // ===== Session 2 =====
2022 // App restarts. Onyx.init() calls getAllKeys() which populates storageKeys
@@ -86,6 +88,47 @@ describe('Collection hydration with connect() followed by immediate set()', () =
8688 expect ( deliveredKeys ) . toContain ( `${ ONYX_KEYS . COLLECTION . TEST_KEY } 3` ) ;
8789 } ) ;
8890
91+ test ( 'single key: set() with non-shallow-equal value should not be overwritten by stale hydration' , async ( ) => {
92+ const mockCallback = jest . fn ( ) ;
93+
94+ Onyx . connect ( {
95+ key : ONYX_KEYS . SINGLE_KEY ,
96+ callback : mockCallback ,
97+ } ) ;
98+
99+ // Immediately update the key with a non-shallow-equal
100+ Onyx . set ( ONYX_KEYS . SINGLE_KEY , { title : 'new' } ) ;
101+
102+ await waitForPromisesToResolve ( ) ;
103+
104+ // The LAST value delivered to the subscriber must be the fresh one, not the stale storage value
105+ const lastValue = mockCallback . mock . calls [ mockCallback . mock . calls . length - 1 ] [ 0 ] ;
106+ expect ( lastValue ) . toEqual ( { title : 'new' } ) ;
107+ } ) ;
108+
109+ test ( 'collection key: set() with non-shallow-equal value should not be regressed by hydration multiGet' , async ( ) => {
110+ const mockCallback = jest . fn ( ) ;
111+
112+ Onyx . connect ( {
113+ key : ONYX_KEYS . COLLECTION . TEST_KEY ,
114+ waitForCollectionCallback : true ,
115+ callback : mockCallback ,
116+ } ) ;
117+
118+ // Update key 1 with a non-shallow-equal value while hydration multiGet is in-flight
119+ Onyx . set ( `${ ONYX_KEYS . COLLECTION . TEST_KEY } 1` , { id : 1 , title : 'Freshly Updated' } ) ;
120+
121+ await waitForPromisesToResolve ( ) ;
122+
123+ const lastCall = mockCallback . mock . calls [ mockCallback . mock . calls . length - 1 ] [ 0 ] ;
124+
125+ // The final collection snapshot must have the fresh value, not the stale storage one
126+ expect ( lastCall [ `${ ONYX_KEYS . COLLECTION . TEST_KEY } 1` ] ) . toEqual ( { id : 1 , title : 'Freshly Updated' } ) ;
127+ // Other members should still be present from storage
128+ expect ( lastCall [ `${ ONYX_KEYS . COLLECTION . TEST_KEY } 2` ] ) . toEqual ( { id : 2 , title : 'Test Two' } ) ;
129+ expect ( lastCall [ `${ ONYX_KEYS . COLLECTION . TEST_KEY } 3` ] ) . toEqual ( { id : 3 , title : 'Test Three' } ) ;
130+ } ) ;
131+
89132 test ( 'waitForCollectionCallback=false should deliver all collection members from storage' , async ( ) => {
90133 const mockCallback = jest . fn ( ) ;
91134
0 commit comments