@@ -127,6 +127,87 @@ describe('Set data while storage is clearing', () => {
127127 } ) ;
128128 } ) ;
129129
130+ it ( 'should preserve all collection members when a collection key is passed to keysToPreserve' , ( ) => {
131+ expect . assertions ( 6 ) ;
132+
133+ const collectionItemKey1 = `${ ONYX_KEYS . COLLECTION . TEST } 1` ;
134+ const collectionItemKey2 = `${ ONYX_KEYS . COLLECTION . TEST } 2` ;
135+
136+ // Given that Onyx has a collection with two items
137+ return (
138+ Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST , {
139+ [ collectionItemKey1 ] : { id : 1 , name : 'first' } ,
140+ [ collectionItemKey2 ] : { id : 2 , name : 'second' } ,
141+ } as GenericCollection )
142+ // When clear is called with the collection prefix as a key to preserve
143+ . then ( ( ) => Onyx . clear ( [ ONYX_KEYS . COLLECTION . TEST ] ) )
144+ . then ( ( ) => waitForPromisesToResolve ( ) )
145+ . then ( ( ) => {
146+ // Then both collection members are preserved in the cache and storage
147+ expect ( cache . get ( collectionItemKey1 ) ) . toEqual ( { id : 1 , name : 'first' } ) ;
148+ expect ( cache . get ( collectionItemKey2 ) ) . toEqual ( { id : 2 , name : 'second' } ) ;
149+
150+ return Promise . all ( [ StorageMock . getItem ( collectionItemKey1 ) , StorageMock . getItem ( collectionItemKey2 ) ] ) ;
151+ } )
152+ . then ( ( [ storedValue1 , storedValue2 ] ) => {
153+ expect ( storedValue1 ) . toEqual ( { id : 1 , name : 'first' } ) ;
154+ expect ( storedValue2 ) . toEqual ( { id : 2 , name : 'second' } ) ;
155+
156+ // And non-collection keys are still cleared (default key reset to default)
157+ expect ( cache . get ( ONYX_KEYS . DEFAULT_KEY ) ) . toBe ( DEFAULT_VALUE ) ;
158+ return expect ( StorageMock . getItem ( ONYX_KEYS . DEFAULT_KEY ) ) . resolves . toBe ( DEFAULT_VALUE ) ;
159+ } )
160+ ) ;
161+ } ) ;
162+
163+ it ( 'should preserve collection members and still clear regular keys not in keysToPreserve' , ( ) => {
164+ expect . assertions ( 4 ) ;
165+
166+ const collectionItemKey1 = `${ ONYX_KEYS . COLLECTION . TEST } 1` ;
167+
168+ // Given that Onyx has both a collection item and a regular key set
169+ return (
170+ Promise . all ( [ Onyx . set ( ONYX_KEYS . REGULAR_KEY , SET_VALUE ) , Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST , { [ collectionItemKey1 ] : 'value' } as GenericCollection ) ] )
171+ // When clear is called preserving only the collection
172+ . then ( ( ) => Onyx . clear ( [ ONYX_KEYS . COLLECTION . TEST ] ) )
173+ . then ( ( ) => waitForPromisesToResolve ( ) )
174+ . then ( ( ) => {
175+ // Then the collection member is preserved
176+ expect ( cache . get ( collectionItemKey1 ) ) . toBe ( 'value' ) ;
177+ return expect ( StorageMock . getItem ( collectionItemKey1 ) ) . resolves . toBe ( 'value' ) ;
178+ } )
179+ . then ( ( ) => {
180+ // And the regular key is cleared
181+ expect ( cache . get ( ONYX_KEYS . REGULAR_KEY ) ) . toBeUndefined ( ) ;
182+ return expect ( StorageMock . getItem ( ONYX_KEYS . REGULAR_KEY ) ) . resolves . toBeNull ( ) ;
183+ } )
184+ ) ;
185+ } ) ;
186+
187+ it ( 'should preserve both collection keys and individual keys when both are passed to keysToPreserve' , ( ) => {
188+ expect . assertions ( 4 ) ;
189+
190+ const collectionItemKey1 = `${ ONYX_KEYS . COLLECTION . TEST } 1` ;
191+
192+ // Given that Onyx has a collection item and a regular key set
193+ return (
194+ Promise . all ( [ Onyx . set ( ONYX_KEYS . REGULAR_KEY , SET_VALUE ) , Onyx . mergeCollection ( ONYX_KEYS . COLLECTION . TEST , { [ collectionItemKey1 ] : 'value' } as GenericCollection ) ] )
195+ // When clear is called preserving both the collection and the regular key
196+ . then ( ( ) => Onyx . clear ( [ ONYX_KEYS . COLLECTION . TEST , ONYX_KEYS . REGULAR_KEY ] ) )
197+ . then ( ( ) => waitForPromisesToResolve ( ) )
198+ . then ( ( ) => {
199+ // Then both the collection member and the regular key are preserved
200+ expect ( cache . get ( collectionItemKey1 ) ) . toBe ( 'value' ) ;
201+ expect ( cache . get ( ONYX_KEYS . REGULAR_KEY ) ) . toBe ( SET_VALUE ) ;
202+ return Promise . all ( [ StorageMock . getItem ( collectionItemKey1 ) , StorageMock . getItem ( ONYX_KEYS . REGULAR_KEY ) ] ) ;
203+ } )
204+ . then ( ( [ storedCollectionValue , storedRegularValue ] ) => {
205+ expect ( storedCollectionValue ) . toBe ( 'value' ) ;
206+ expect ( storedRegularValue ) . toBe ( SET_VALUE ) ;
207+ } )
208+ ) ;
209+ } ) ;
210+
130211 it ( 'should only trigger the connection callback once when using wait for collection callback' , ( ) => {
131212 expect . assertions ( 4 ) ;
132213
0 commit comments