77} from 'react-native-harness' ;
88import { Platform } from 'react-native' ;
99import { MMKV , createMMKV , deleteMMKV , existsMMKV } from 'react-native-mmkv' ;
10+ import { getPlatformContext } from '../../packages/react-native-mmkv/src/getMMKVFactory' ;
1011
1112const skipOnWeb = ( reason : string ) : boolean => {
1213 if ( Platform . OS === 'web' ) {
@@ -20,6 +21,12 @@ const waitForNextTick = async () => {
2021 await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
2122} ;
2223
24+ const getNativeCustomPath = ( name : string ) : string | undefined => {
25+ if ( skipOnWeb ( 'custom paths are not supported on Web' ) ) return undefined ;
26+ const baseDirectory = getPlatformContext ( ) . getBaseDirectory ( ) ;
27+ return `${ baseDirectory } /${ name } ` ;
28+ } ;
29+
2330describe ( 'MMKV Core Functionality' , ( ) => {
2431 let storage : MMKV ;
2532
@@ -1186,6 +1193,27 @@ describe('Deleting instances and checking if they exist', () => {
11861193 const exists = existsMMKV ( 'some-non-existing-instance' ) ;
11871194 expect ( exists ) . toStrictEqual ( false ) ;
11881195 } ) ;
1196+
1197+ it ( 'should accept an instance location object' , ( ) => {
1198+ const id = `some-location-instance-${ Date . now ( ) } ` ;
1199+ const storage = createMMKV ( { id } ) ;
1200+ storage . set ( 'value' , 'stored' ) ;
1201+
1202+ const exists = existsMMKV ( { id } ) ;
1203+ expect ( exists ) . toStrictEqual ( true ) ;
1204+ } ) ;
1205+
1206+ it ( 'should check custom path instances with their path' , ( ) => {
1207+ const id = `some-path-instance-${ Date . now ( ) } ` ;
1208+ const path = getNativeCustomPath ( `path-aware-exists-${ Date . now ( ) } ` ) ;
1209+ if ( path == null ) return ;
1210+
1211+ const storage = createMMKV ( { id, path } ) ;
1212+ storage . set ( 'value' , 'stored' ) ;
1213+
1214+ expect ( existsMMKV ( id ) ) . toStrictEqual ( false ) ;
1215+ expect ( existsMMKV ( { id, path } ) ) . toStrictEqual ( true ) ;
1216+ } ) ;
11891217 } ) ;
11901218
11911219 describe ( 'Deleting an instance' , ( ) => {
@@ -1214,6 +1242,36 @@ describe('Deleting instances and checking if they exist', () => {
12141242 const wasDeleted = deleteMMKV ( 'some-non-existing-instance' ) ;
12151243 expect ( wasDeleted ) . toStrictEqual ( false ) ;
12161244 } ) ;
1245+
1246+ it ( 'should accept an instance location object' , ( ) => {
1247+ const id = `some-location-instance-${ Date . now ( ) } ` ;
1248+ const storage = createMMKV ( { id } ) ;
1249+ storage . set ( 'value' , 'stored' ) ;
1250+
1251+ const wasDeleted = deleteMMKV ( { id } ) ;
1252+ expect ( wasDeleted ) . toStrictEqual ( true ) ;
1253+ expect ( existsMMKV ( { id } ) ) . toStrictEqual ( false ) ;
1254+ } ) ;
1255+
1256+ it ( 'should delete custom path instances with their path' , ( ) => {
1257+ const id = `some-path-instance-${ Date . now ( ) } ` ;
1258+ const path = getNativeCustomPath ( `path-aware-delete-${ Date . now ( ) } ` ) ;
1259+ if ( path == null ) return ;
1260+
1261+ const storage = createMMKV ( { id, path } ) ;
1262+ storage . set ( 'value' , 'stored' ) ;
1263+
1264+ expect ( existsMMKV ( { id, path } ) ) . toStrictEqual ( true ) ;
1265+ expect ( deleteMMKV ( { id, path } ) ) . toStrictEqual ( true ) ;
1266+ expect ( existsMMKV ( { id, path } ) ) . toStrictEqual ( false ) ;
1267+ } ) ;
1268+
1269+ it ( 'should reject custom paths on Web' , ( ) => {
1270+ if ( Platform . OS !== 'web' ) return ;
1271+
1272+ expect ( ( ) => existsMMKV ( { id : 'some-instance' , path : '/tmp' } ) ) . toThrow ( ) ;
1273+ expect ( ( ) => deleteMMKV ( { id : 'some-instance' , path : '/tmp' } ) ) . toThrow ( ) ;
1274+ } ) ;
12171275 } ) ;
12181276} ) ;
12191277
0 commit comments