diff --git a/src/components/CreateWallet.test.tsx b/src/components/CreateWallet.test.tsx index 9958bc93..40c8a0ee 100644 --- a/src/components/CreateWallet.test.tsx +++ b/src/components/CreateWallet.test.tsx @@ -15,13 +15,11 @@ jest.mock('../libs/JmWalletApi', () => ({ postWalletCreate: jest.fn(), })) -const mockedUseNavigate = jest.fn() const mockedUseNavigation = jest.fn() const mockUseLoaderData = jest.fn() jest.mock('react-router-dom', () => { return { ...jest.requireActual('react-router-dom'), - useNavigate: () => mockedUseNavigate, useNavigation: () => mockedUseNavigation, useLoaderData: () => mockUseLoaderData, } diff --git a/src/components/Wallets.test.tsx b/src/components/Wallets.test.tsx index 1baa7307..8d565a53 100644 --- a/src/components/Wallets.test.tsx +++ b/src/components/Wallets.test.tsx @@ -4,7 +4,6 @@ import user from '@testing-library/user-event' import * as apiMock from '../libs/JmWalletApi' import * as loadersMock from './loaders/DataLoaders' -import * as reactRouterDomMock from 'react-router-dom' import Wallets from './Wallets' import { CurrentWallet } from '../context/WalletContext' @@ -24,18 +23,19 @@ jest.mock('../libs/JmWalletApi', () => ({ getWalletLock: jest.fn(), })) -const mockedUseNavigate = jest.fn() -const mockedUseNavigation = jest.fn() -const mockUseLoaderData = { - existingWallets: ['test'], - existingWalletsError: null, +const mockUseNavigate = jest.fn() +let mockUseNavigation = { + state: 'loading', +} +let mockUseLoaderData = { + existingWallets: [''], + existingWalletsError: '', } - jest.mock('react-router-dom', () => { return { ...jest.requireActual('react-router-dom'), - useNavigate: () => mockedUseNavigate, - useNavigation: () => mockedUseNavigation, + useNavigate: () => mockUseNavigate, + useNavigation: () => mockUseNavigation, useLoaderData: () => mockUseLoaderData, } }) @@ -83,13 +83,10 @@ describe('', () => { ok: false, }), ) - ;(loadersMock.allWalletsLoader as jest.Mock).mockReturnValue( - Promise.resolve({ - ok: false, - message: 'error message', - }), - ) - apiMock.Helper.throwError(jest.fn as unknown as Response, t('wallets.error_loading_failed')) + mockUseLoaderData = { + existingWallets: [], + existingWalletsError: t('wallets.error_loading_failed'), + } await act(async () => setup({})) @@ -145,9 +142,6 @@ describe('', () => { json: () => Promise.resolve({ version: '0.9.10dev' }), }), ) - ;(reactRouterDomMock.useNavigation as jest.Mock).mockReturnValue({ - state: 'idle', - }) await act(async () => setup({})) @@ -179,6 +173,13 @@ describe('', () => { json: () => Promise.resolve({ version: '0.9.10dev' }), }), ) + mockUseLoaderData = { + existingWallets: ['wallet0.jmdat', 'wallet1.jmdat'], + existingWalletsError: '', + } + mockUseNavigation = { + state: 'idle', + } await act(async () => setup({})) @@ -263,11 +264,13 @@ describe('', () => { }), }), ) - ;(loadersMock.allWalletsLoader as jest.Mock).mockReturnValue( - Promise.resolve({ - existingWallets: [dummyWalletFileName], - }), - ) + mockUseLoaderData = { + existingWallets: [dummyWalletFileName], + existingWalletsError: '', + } + mockUseNavigation = { + state: 'idle', + } await act(async () => setup({})) @@ -285,7 +288,7 @@ describe('', () => { token: dummyToken, refresh_token: dummyToken, }) - expect(mockedUseNavigate).toHaveBeenCalledWith('/wallet') + expect(mockUseNavigate).toHaveBeenCalledWith('/wallet') }) it('should add alert if unlocking of inactive wallet fails', async () => { @@ -315,6 +318,13 @@ describe('', () => { json: () => Promise.resolve({ message: apiErrorMessage }), }), ) + mockUseLoaderData = { + existingWallets: [dummyWalletFileName], + existingWalletsError: '', + } + mockUseNavigation = { + state: 'idle', + } await act(async () => setup({})) @@ -328,7 +338,7 @@ describe('', () => { await user.click(unlockWalletButton) expect(mockStartWallet).not.toHaveBeenCalled() - expect(mockedUseNavigate).not.toHaveBeenCalled() + expect(mockUseNavigate).not.toHaveBeenCalled() expect(screen.getByText(apiErrorMessage.replace('Wallet', dummyWalletFileName))).toBeInTheDocument() }) @@ -358,6 +368,13 @@ describe('', () => { json: () => Promise.resolve({ walletname: dummyWalletFileName, already_locked: false }), }), ) + mockUseLoaderData = { + existingWallets: [dummyWalletFileName], + existingWalletsError: '', + } + mockUseNavigation = { + state: 'idle', + } await act(async () => setup({ @@ -407,6 +424,13 @@ describe('', () => { json: () => Promise.resolve({ message: apiErrorMessage }), }), ) + mockUseLoaderData = { + existingWallets: [dummyWalletFileName], + existingWalletsError: '', + } + mockUseNavigation = { + state: 'idle', + } await act(async () => setup({ @@ -462,6 +486,13 @@ describe('', () => { json: () => Promise.resolve({ walletname: dummyWalletFileName, already_locked: false }), }), ) + mockUseLoaderData = { + existingWallets: [dummyWalletFileName], + existingWalletsError: '', + } + mockUseNavigation = { + state: 'idle', + } await act(async () => setup({ diff --git a/src/components/Wallets.tsx b/src/components/Wallets.tsx index a2b4df02..2c4a812c 100644 --- a/src/components/Wallets.tsx +++ b/src/components/Wallets.tsx @@ -38,7 +38,7 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }: Wall const navigate = useNavigate() const navigation = useNavigation() const serviceInfo = useServiceInfo() - const { existingWallets, existingWalletsError } = useLoaderData() as AllWalletsLoaderResponse + const getWalletAllResponse = useLoaderData() as AllWalletsLoaderResponse const [unlockingWalletFileName, setUnlockWalletFileName] = useState() const isUnlocking = useMemo(() => unlockingWalletFileName !== undefined, [unlockingWalletFileName]) const [alert, setAlert] = useState() @@ -157,7 +157,7 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }: Wall }, [currentWallet, lockWallet]) useEffect(() => { - if (currentWallet && existingWallets?.wallets?.length > 1) { + if (currentWallet && getWalletAllResponse?.existingWallets?.length > 1) { setAlert({ variant: 'info', message: t('wallets.alert_wallet_open', { currentWalletName: currentWallet.displayName }), @@ -165,18 +165,18 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }: Wall }) } - if (existingWalletsError) { - const message = existingWalletsError || t('wallets.error_loading_failed') + if (getWalletAllResponse.existingWalletsError) { + const message = getWalletAllResponse.existingWalletsError || t('wallets.error_loading_failed') setAlert({ variant: 'danger', message }) } - }, [currentWallet, existingWallets?.wallets?.length, existingWalletsError, t]) + }, [currentWallet, getWalletAllResponse, t]) return ( <>
{serviceInfo?.rescanning === true && ( @@ -185,13 +185,13 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }: Wall )} {alert && } - {navigation.state !== 'idle' ? ( + {navigation.state === 'loading' ? (
) : ( - sortWallets(existingWallets?.wallets ?? [], serviceInfo?.walletFileName).map( + sortWallets(getWalletAllResponse?.existingWallets ?? [], serviceInfo?.walletFileName).map( (walletFileName: Api.WalletFileName, index: number) => { const noneActive = !serviceInfo?.walletFileName const isActive = serviceInfo?.walletFileName === walletFileName @@ -222,16 +222,17 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }: Wall
0, - disabled: !existingWallets || isUnlocking, + 'btn-lg': getWalletAllResponse?.existingWallets?.length === 0, + 'btn-dark': getWalletAllResponse?.existingWallets?.length === 0, + 'btn-outline-dark': + !getWalletAllResponse?.existingWallets || getWalletAllResponse?.existingWallets?.length > 0, + disabled: !getWalletAllResponse || isUnlocking, })} data-testid="new-wallet-btn" > @@ -244,8 +245,8 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }: Wall