@@ -594,18 +594,19 @@ describe('Onyx', () => {
594594 } ) ;
595595 } ) ;
596596
597- it ( 'should overwrite an array key nested inside an object when using merge on a collection ' , ( ) => {
597+ it ( 'should overwrite an array key nested inside an object when using mergeCollection ' , ( ) => {
598598 let testKeyValue : unknown ;
599599 connection = Onyx . connect ( {
600600 key : ONYX_KEYS . COLLECTION . TEST_KEY ,
601+ waitForCollectionCallback : true ,
601602 initWithStoredValues : false ,
602603 callback : ( value ) => {
603604 testKeyValue = value ;
604605 } ,
605606 } ) ;
606607
607- Onyx . merge ( ONYX_KEYS . COLLECTION . TEST_KEY , { test_1 : { something : [ 1 , 2 , 3 ] } } ) ;
608- return Onyx . merge ( ONYX_KEYS . COLLECTION . TEST_KEY , { test_1 : { something : [ 4 ] } } ) . then ( ( ) => {
608+ Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST_KEY , { test_1 : { something : [ 1 , 2 , 3 ] } } ) ;
609+ return Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST_KEY , { test_1 : { something : [ 4 ] } } ) . then ( ( ) => {
609610 expect ( testKeyValue ) . toEqual ( { test_1 : { something : [ 4 ] } } ) ;
610611 } ) ;
611612 } ) ;
@@ -3006,6 +3007,32 @@ describe('Onyx', () => {
30063007 expect ( cache . get ( ONYX_KEYS . RAM_ONLY_WITH_INITIAL_VALUE ) ) . toEqual ( 'default' ) ;
30073008 } ) ;
30083009 } ) ;
3010+
3011+ describe ( 'collection key protection' , ( ) => {
3012+ it ( 'set with a collection key should log an alert and not store data' , async ( ) => {
3013+ const logInfoSpy = jest . spyOn ( Logger , 'logInfo' ) ;
3014+
3015+ await Onyx . set ( ONYX_KEYS . COLLECTION . TEST_KEY as unknown as typeof ONYX_KEYS . TEST_KEY , { str : 'should not be stored' } as unknown as string ) ;
3016+
3017+ expect ( logInfoSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( '"set"' ) ) ;
3018+ expect ( logInfoSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( ONYX_KEYS . COLLECTION . TEST_KEY ) ) ;
3019+ expect ( cache . get ( ONYX_KEYS . COLLECTION . TEST_KEY ) ) . toBeUndefined ( ) ;
3020+
3021+ logInfoSpy . mockRestore ( ) ;
3022+ } ) ;
3023+
3024+ it ( 'merge with a collection key should log an alert and not store data' , async ( ) => {
3025+ const logInfoSpy = jest . spyOn ( Logger , 'logInfo' ) ;
3026+
3027+ await Onyx . merge ( ONYX_KEYS . COLLECTION . TEST_KEY as unknown as typeof ONYX_KEYS . TEST_KEY , { str : 'should not be stored' } as unknown as string ) ;
3028+
3029+ expect ( logInfoSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( '"merge"' ) ) ;
3030+ expect ( logInfoSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( ONYX_KEYS . COLLECTION . TEST_KEY ) ) ;
3031+ expect ( cache . get ( ONYX_KEYS . COLLECTION . TEST_KEY ) ) . toBeUndefined ( ) ;
3032+
3033+ logInfoSpy . mockRestore ( ) ;
3034+ } ) ;
3035+ } ) ;
30093036} ) ;
30103037
30113038// Separate describe block for Onyx.init to control initialization during each test.
@@ -3111,6 +3138,7 @@ describe('Onyx.init', () => {
31113138 expect ( cache . get ( `${ ONYX_KEYS . COLLECTION . TEST_KEY } entry1` ) ) . toEqual ( 'test_1' ) ;
31123139 } ) ;
31133140 } ) ;
3141+
31143142} ) ;
31153143
31163144// Separate describe block to control Onyx.init() per-test so we can pre-seed storage before init.
0 commit comments