@@ -260,6 +260,7 @@ describe("auth duck", () => {
260260 selectAccount : useAuthenticationStore . getState ( ) . selectAccount ,
261261 setNavigationRef : useAuthenticationStore . getState ( ) . setNavigationRef ,
262262 signIn : useAuthenticationStore . getState ( ) . signIn ,
263+ initializeNetwork : useAuthenticationStore . getState ( ) . initializeNetwork ,
263264 } ;
264265
265266 beforeEach ( ( ) => {
@@ -280,6 +281,7 @@ describe("auth duck", () => {
280281 useAuthenticationStore . getState ( ) . getAllAccounts = jest . fn ( ) ;
281282 useAuthenticationStore . getState ( ) . selectAccount = jest . fn ( ) ;
282283 useAuthenticationStore . getState ( ) . clearError = jest . fn ( ) ;
284+ useAuthenticationStore . getState ( ) . initializeNetwork = jest . fn ( ) ;
283285
284286 // Reset the store state
285287 act ( ( ) => {
@@ -1457,6 +1459,53 @@ describe("auth duck", () => {
14571459 STORAGE_KEYS . COLLECTIBLES_LIST ,
14581460 ) ;
14591461 } ) ;
1462+
1463+ it ( "should call resetRoot to AUTH_STACK on logout(true) even though ...initialState clears navigationRef" , async ( ) => {
1464+ const { result } = renderHook ( ( ) => useAuthenticationStore ( ) ) ;
1465+
1466+ ( dataStorage . getItem as jest . Mock ) . mockImplementation ( ( key ) => {
1467+ if ( key === STORAGE_KEYS . ACCOUNT_LIST ) {
1468+ return Promise . resolve ( JSON . stringify ( [ mockAccount ] ) ) ;
1469+ }
1470+ return Promise . resolve ( null ) ;
1471+ } ) ;
1472+
1473+ act ( ( ) => {
1474+ useAuthenticationStore . setState ( {
1475+ authStatus : AUTH_STATUS . AUTHENTICATED ,
1476+ account : mockAccount ,
1477+ logout : originalStoreMethods . logout ,
1478+ setNavigationRef : originalStoreMethods . setNavigationRef ,
1479+ } ) ;
1480+ } ) ;
1481+
1482+ act ( ( ) => {
1483+ result . current . setNavigationRef ( mockNavigationRef ) ;
1484+ } ) ;
1485+
1486+ // Confirm navigationRef is in the store before calling logout
1487+ expect ( useAuthenticationStore . getState ( ) . navigationRef ) . toBe (
1488+ mockNavigationRef ,
1489+ ) ;
1490+
1491+ await act ( async ( ) => {
1492+ result . current . logout ( true ) ;
1493+ await new Promise ( ( resolve ) => {
1494+ setTimeout ( resolve , 300 ) ;
1495+ } ) ;
1496+ } ) ;
1497+
1498+ // Key regression: navigationRef is captured before ...initialState clears
1499+ // it to null, so resetRoot must still be called with AUTH_STACK.
1500+ expect ( mockNavigationRef . resetRoot ) . toHaveBeenCalledWith ( {
1501+ index : 0 ,
1502+ routes : [ { name : ROOT_NAVIGATOR_ROUTES . AUTH_STACK } ] ,
1503+ } ) ;
1504+ // State should reflect NOT_AUTHENTICATED after deletion
1505+ expect ( useAuthenticationStore . getState ( ) . authStatus ) . toBe (
1506+ AUTH_STATUS . NOT_AUTHENTICATED ,
1507+ ) ;
1508+ } ) ;
14601509 } ) ;
14611510
14621511 describe ( "getAuthStatus with LOCKED state" , ( ) => {
@@ -1877,4 +1926,51 @@ describe("auth duck", () => {
18771926 } ) ;
18781927 } ) ;
18791928 } ) ;
1929+
1930+ describe ( "initializeNetwork" , ( ) => {
1931+ it ( "should load TESTNET from ACTIVE_NETWORK storage and update network in store" , async ( ) => {
1932+ const { result } = renderHook ( ( ) => useAuthenticationStore ( ) ) ;
1933+
1934+ ( dataStorage . getItem as jest . Mock ) . mockImplementation ( ( key ) => {
1935+ if ( key === STORAGE_KEYS . ACTIVE_NETWORK ) {
1936+ return Promise . resolve ( NETWORKS . TESTNET ) ;
1937+ }
1938+ return Promise . resolve ( null ) ;
1939+ } ) ;
1940+
1941+ act ( ( ) => {
1942+ useAuthenticationStore . setState ( {
1943+ network : NETWORKS . PUBLIC ,
1944+ initializeNetwork : originalStoreMethods . initializeNetwork ,
1945+ } ) ;
1946+ } ) ;
1947+
1948+ await act ( async ( ) => {
1949+ await result . current . initializeNetwork ( ) ;
1950+ } ) ;
1951+
1952+ expect ( result . current . network ) . toBe ( NETWORKS . TESTNET ) ;
1953+ } ) ;
1954+
1955+ it ( "should keep PUBLIC network when ACTIVE_NETWORK is not in storage" , async ( ) => {
1956+ const { result } = renderHook ( ( ) => useAuthenticationStore ( ) ) ;
1957+
1958+ ( dataStorage . getItem as jest . Mock ) . mockImplementation ( ( ) =>
1959+ Promise . resolve ( null ) ,
1960+ ) ;
1961+
1962+ act ( ( ) => {
1963+ useAuthenticationStore . setState ( {
1964+ network : NETWORKS . PUBLIC ,
1965+ initializeNetwork : originalStoreMethods . initializeNetwork ,
1966+ } ) ;
1967+ } ) ;
1968+
1969+ await act ( async ( ) => {
1970+ await result . current . initializeNetwork ( ) ;
1971+ } ) ;
1972+
1973+ expect ( result . current . network ) . toBe ( NETWORKS . PUBLIC ) ;
1974+ } ) ;
1975+ } ) ;
18801976} ) ;
0 commit comments