Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions __tests__/WalletResetConfirmModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';

jest.mock('@react-native-async-storage/async-storage', () => ({
getItem: jest.fn(async () => null),
setItem: jest.fn(async () => {}),
}));

// Mock lucide icons to simplify render tests
jest.mock('lucide-react-native', () => ({
ShieldAlert: () => null,
AlertTriangle: () => null,
KeyRound: () => null,
Users: () => null,
Clock: () => null,
Settings: () => null,
X: () => null,
}));

import { WalletResetConfirmModal } from '../src/components/WalletResetConfirmModal';

describe('WalletResetConfirmModal', () => {
it('renders the destructive confirmation with a clear typed-phrase requirement', () => {
const { getByText, getByLabelText } = render(
<WalletResetConfirmModal visible onConfirm={jest.fn()} onCancel={jest.fn()} />
);

expect(getByText('Reset Wallet')).toBeTruthy();
expect(getByText('Type "confirm reset" to confirm')).toBeTruthy();
expect(getByLabelText('Delete Everything')).toBeTruthy();
});

it('keeps the destructive action disabled until the exact phrase is typed', () => {
const onConfirm = jest.fn();
const { getByLabelText, getByPlaceholderText } = render(
<WalletResetConfirmModal visible onConfirm={onConfirm} onCancel={jest.fn()} />
);

const confirmButton = getByLabelText('Delete Everything');
const input = getByPlaceholderText('confirm reset');

// Wrong / partial phrase keeps the action blocked
fireEvent.changeText(input, 'confirm');
fireEvent.press(confirmButton);
expect(onConfirm).not.toHaveBeenCalled();

fireEvent.changeText(input, 'reset everything');
fireEvent.press(confirmButton);
expect(onConfirm).not.toHaveBeenCalled();
});

it('enables reset only once the exact phrase is entered (case/whitespace-insensitive)', () => {
const onConfirm = jest.fn();
const { getByLabelText, getByPlaceholderText } = render(
<WalletResetConfirmModal visible onConfirm={onConfirm} onCancel={jest.fn()} />
);

const confirmButton = getByLabelText('Delete Everything');
const input = getByPlaceholderText('confirm reset');

fireEvent.changeText(input, ' Confirm Reset ');
fireEvent.press(confirmButton);

expect(onConfirm).toHaveBeenCalledTimes(1);
});

it('exposes an accessible cancel action that does not require the phrase', () => {
const onCancel = jest.fn();
const { getByLabelText } = render(
<WalletResetConfirmModal visible onConfirm={jest.fn()} onCancel={onCancel} />
);

fireEvent.press(getByLabelText('Cancel'));
expect(onCancel).toHaveBeenCalledTimes(1);
});
});
17 changes: 12 additions & 5 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function RootLayout() {
}

function RootContent() {
const { loadWalletFromStorage, publicKey, error, clearWallet } = useWalletStore();
const { loadWalletFromStorage, publicKey, error, clearWallet, walletChecked } = useWalletStore();
const { initializeApp, isInitialized } = useAppStore();
const { colors } = useTheme();
const segments = useSegments();
Expand All @@ -41,20 +41,27 @@ function RootContent() {

useEffect(() => {
if (!isInitialized) return;
// Wait for the initial wallet load to resolve too — `isInitialized` (app
// settings) and `walletChecked` (wallet secret) load independently, and
// deciding on `publicKey` before it's checked would flash-redirect a
// returning signed-in user to the auth screens on cold start.
if (!walletChecked) return;
if (error === 'Failed to restore wallet securely') return; // Stay on error screen

const inAuthGroup = segments[0] === '(auth)';

if (publicKey && inAuthGroup) {
// User is signed in and trying to access auth screens, redirect to main
router.replace('/(tabs)');
} else if (!publicKey && !inAuthGroup && segments[0] !== '(tabs)' && segments[0] !== 'send' && segments[0] !== 'receive' && segments[0] !== 'review-transaction') {
// User is NOT signed in and trying to access main screens, redirect to auth
} else if (!publicKey && !inAuthGroup && segments[0] !== 'send' && segments[0] !== 'receive' && segments[0] !== 'review-transaction') {
// User is NOT signed in and trying to access main screens, redirect to auth.
// This also covers a wallet reset that happens while sitting on a (tabs)
// screen (e.g. Settings) — publicKey going null must redirect from there too.
router.replace('/(auth)');
}
}, [publicKey, isInitialized, segments, error]);
}, [publicKey, isInitialized, walletChecked, segments, error]);

if (!isInitialized) {
if (!isInitialized || !walletChecked) {
return (
<View style={{ flex: 1, backgroundColor: colors.background, justifyContent: 'center', alignItems: 'center' }}>
<ActivityIndicator size="large" color={colors.primary} />
Expand Down
Loading