11/**
2- * Copyright 2025, Optimizely
2+ * Copyright 2025-2026 , Optimizely
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616
1717import { vi , describe , it , expect } from 'vitest' ;
18-
19- vi . mock ( '../../../react_native_optional/async_storage' , ( ) => ( {
20- loadAsyncStorageModule : vi . fn ( ) ,
21- } ) ) ;
22-
23- import { ensureMultiGet , getDefaultAsyncStorage , MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE } from './async-storage' ;
24- import { loadAsyncStorageModule } from '../../../react_native_optional/async_storage' ;
25-
26- describe ( 'getDefaultAsyncStorage' , ( ) => {
27- it ( 'should throw module not found error when async storage is not installed' , ( ) => {
28- vi . mocked ( loadAsyncStorageModule ) . mockReturnValueOnce ( undefined ) ;
29-
30- expect ( ( ) => getDefaultAsyncStorage ( ) ) . toThrow ( MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE ) ;
18+ import Module from 'module' ;
19+
20+ // The shim requires the package when it loads, so each case loads a fresh copy of the shim with the
21+ // package's require replaced by `load`. vi.mock does not intercept the shim's plain require.
22+ const loadShimWith = async ( load : ( ) => unknown ) => {
23+ const M = Module as unknown as { _load : ( request : string , ...rest : unknown [ ] ) => unknown } ;
24+ const originalLoad = M . _load ;
25+ M . _load = function ( this : unknown , request : string , ...rest : unknown [ ] ) {
26+ if ( request === '@react-native-async-storage/async-storage' ) return load ( ) ;
27+ return originalLoad . call ( this , request , ...rest ) ;
28+ } ;
29+ try {
30+ vi . resetModules ( ) ;
31+ return await import ( './async-storage' ) ;
32+ } finally {
33+ M . _load = originalLoad ;
34+ }
35+ } ;
36+
37+ const loadShimWithStorage = ( storage : unknown ) => loadShimWith ( ( ) => ( { default : storage } ) ) ;
38+
39+ describe ( '@react-native-async-storage/async-storage shim' , ( ) => {
40+ it ( 'should export isAvailable as false and an undefined default when the package is not installed' , async ( ) => {
41+ const shim = await loadShimWith ( ( ) => {
42+ throw new Error ( 'Cannot find module' ) ;
43+ } ) ;
44+
45+ expect ( shim . isAvailable ) . toBe ( false ) ;
46+ expect ( shim . default ) . toBeUndefined ( ) ;
3147 } ) ;
3248
33- it ( 'should throw module not found error when the module has no default export' , ( ) => {
34- vi . mocked ( loadAsyncStorageModule ) . mockReturnValueOnce ( { } ) ;
35-
36- expect ( ( ) => getDefaultAsyncStorage ( ) ) . toThrow ( MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE ) ;
37- } ) ;
38-
39- it ( 'should return the default export of the async storage module' , ( ) => {
40- const multiGet = vi . fn ( ) ;
41- const storage = {
42- getItem : vi . fn ( ) ,
43- setItem : vi . fn ( ) ,
44- removeItem : vi . fn ( ) ,
45- getAllKeys : vi . fn ( ) ,
46- clear : vi . fn ( ) ,
47- multiGet,
48- } ;
49- vi . mocked ( loadAsyncStorageModule ) . mockReturnValueOnce ( { default : storage } ) ;
50-
51- const result = getDefaultAsyncStorage ( ) ;
49+ it ( 'should export an undefined default when the package has no default export' , async ( ) => {
50+ const shim = await loadShimWithStorage ( undefined ) ;
5251
53- expect ( result ) . toBe ( storage ) ;
54- expect ( result . multiGet ) . toBe ( multiGet ) ;
52+ expect ( shim . isAvailable ) . toBe ( true ) ;
53+ expect ( shim . default ) . toBeUndefined ( ) ;
5554 } ) ;
56- } ) ;
5755
58- describe ( 'ensureMultiGet' , ( ) => {
59- it ( 'should preserve multiGet when already present (v1/v2)' , ( ) => {
56+ it ( 'should export the package default as-is when multiGet is present (v1/v2)' , async ( ) => {
6057 const multiGet = vi . fn ( ) ;
6158 const storage = {
6259 getItem : vi . fn ( ) ,
@@ -67,9 +64,11 @@ describe('ensureMultiGet', () => {
6764 multiGet,
6865 } ;
6966
70- const result = ensureMultiGet ( storage ) ;
67+ const shim = await loadShimWithStorage ( storage ) ;
7168
72- expect ( result . multiGet ) . toBe ( multiGet ) ;
69+ expect ( shim . isAvailable ) . toBe ( true ) ;
70+ expect ( shim . default ) . toBe ( storage ) ;
71+ expect ( shim . default ?. multiGet ) . toBe ( multiGet ) ;
7372 } ) ;
7473
7574 it ( 'should construct multiGet from getMany when multiGet is absent (v3)' , async ( ) => {
@@ -87,19 +86,19 @@ describe('ensureMultiGet', () => {
8786 } ) ,
8887 } ;
8988
90- const result = ensureMultiGet ( storage ) ;
89+ const result = ( await loadShimWithStorage ( storage ) ) . default ;
9190
92- expect ( result . multiGet ) . toBeDefined ( ) ;
91+ expect ( result ? .multiGet ) . toBeDefined ( ) ;
9392
94- const pairs = await result . multiGet ( [ 'b' , 'missing' , 'a' ] ) ;
93+ const pairs = await result ? .multiGet ( [ 'b' , 'missing' , 'a' ] ) ;
9594 expect ( pairs ) . toEqual ( [
9695 [ 'b' , '2' ] ,
9796 [ 'missing' , null ] ,
9897 [ 'a' , '1' ] ,
9998 ] ) ;
10099 } ) ;
101100
102- it ( 'should not modify the original storage when constructing multiGet (v3)' , ( ) => {
101+ it ( 'should not modify the original storage when constructing multiGet (v3)' , async ( ) => {
103102 const storage = {
104103 getItem : vi . fn ( ) ,
105104 setItem : vi . fn ( ) ,
@@ -109,16 +108,16 @@ describe('ensureMultiGet', () => {
109108 getMany : vi . fn ( ) ,
110109 } ;
111110
112- const result = ensureMultiGet ( storage ) ;
111+ const result = ( await loadShimWithStorage ( storage ) ) . default ;
113112
114113 expect ( result ) . not . toBe ( storage ) ;
115114 expect ( 'multiGet' in storage ) . toBe ( false ) ;
116115 expect ( Object . getPrototypeOf ( result ) ) . toBe ( storage ) ;
117- expect ( result . getItem ) . toBe ( storage . getItem ) ;
118- expect ( result . setItem ) . toBe ( storage . setItem ) ;
119- expect ( result . removeItem ) . toBe ( storage . removeItem ) ;
120- expect ( result . getAllKeys ) . toBe ( storage . getAllKeys ) ;
121- expect ( result . clear ) . toBe ( storage . clear ) ;
116+ expect ( result ? .getItem ) . toBe ( storage . getItem ) ;
117+ expect ( result ? .setItem ) . toBe ( storage . setItem ) ;
118+ expect ( result ? .removeItem ) . toBe ( storage . removeItem ) ;
119+ expect ( result ? .getAllKeys ) . toBe ( storage . getAllKeys ) ;
120+ expect ( result ? .clear ) . toBe ( storage . clear ) ;
122121 } ) ;
123122
124123 it ( 'should return pairs in the requested key order regardless of getMany record order' , async ( ) => {
@@ -137,8 +136,8 @@ describe('ensureMultiGet', () => {
137136 } ) ,
138137 } ;
139138
140- const result = ensureMultiGet ( storage ) ;
141- const pairs = await result . multiGet ( [ 'z' , 'x' , 'y' ] ) ;
139+ const result = ( await loadShimWithStorage ( storage ) ) . default ;
140+ const pairs = await result ? .multiGet ( [ 'z' , 'x' , 'y' ] ) ;
142141
143142 expect ( pairs ) . toEqual ( [
144143 [ 'z' , 'vz' ] ,
0 commit comments