Fix: isLoggedIn memory leak (#2952)

* Fix memory leak from isLoggedIn

* Fix tests with mocking for clearAuthCache

---------

Co-authored-by: Johannes Klein <johannes.t.klein@gmail.com>
This commit is contained in:
Amanda Bullington
2025-06-11 10:04:54 -07:00
committed by GitHub
parent a049d5022d
commit 3c4ec368f5
5 changed files with 99 additions and 21 deletions

View File

@@ -1,3 +1,20 @@
let clearAuthCache = () => {}; // Default no-op function
// Try to get clearAuthCache function safely
try {
const authModule = require( "components/LoginSignUp/AuthenticationService.ts" );
if ( authModule && typeof authModule.clearAuthCache === "function" ) {
// eslint-disable-next-line prefer-destructuring
clearAuthCache = authModule.clearAuthCache;
} else if ( authModule.default && typeof authModule.default.clearAuthCache === "function" ) {
// eslint-disable-next-line prefer-destructuring
clearAuthCache = authModule.default.clearAuthCache;
}
} catch ( error ) {
console.warn( "Could not import clearAuthCache, using no-op function", error );
}
class RNSInfo {
static stores = new Map();
@@ -11,6 +28,15 @@ class RNSInfo {
if ( typeof s !== "string" ) { throw new Error( "Invalid string:", s ); }
}
static clearAllStores = jest.fn( () => {
RNSInfo.stores.clear();
clearAuthCache();
} );
static clearAuthCache = jest.fn( () => {
clearAuthCache();
} );
static getItem = jest.fn( async ( k, o ) => {
RNSInfo.validateString( k );
@@ -27,11 +53,8 @@ class RNSInfo {
let mappedValues = [];
if ( service?.size ) {
// for ( const [k, v] of service.entries() ) {
// mappedValues.push( { key: k, value: v, service: serviceName } );
// }
mappedValues = service.entries( ).map(
( key, value ) => ( { key, value, service: serviceName } )
mappedValues = Array.from( service.entries() ).map(
( [key, value] ) => ( { key, value, service: serviceName } )
);
}
@@ -52,6 +75,8 @@ class RNSInfo {
service.set( k, v );
clearAuthCache( );
return null;
} );
@@ -63,6 +88,8 @@ class RNSInfo {
if ( service ) { service.delete( k ); }
clearAuthCache( );
return null;
} );