From 86b12f35be9bea33e235841a74005176718a0f92 Mon Sep 17 00:00:00 2001 From: Martin Homola Date: Mon, 10 Feb 2025 16:51:47 +0100 Subject: [PATCH] test(trading): verifyAddressThunk --- .../src/thunks/__tests__/tradingThunk.test.ts | 243 +++++++++++++++++- .../trading/src/thunks/tradingThunks.ts | 2 +- 2 files changed, 238 insertions(+), 7 deletions(-) diff --git a/suite-common/trading/src/thunks/__tests__/tradingThunk.test.ts b/suite-common/trading/src/thunks/__tests__/tradingThunk.test.ts index 1f90378ad8d..a6f2a0d6683 100644 --- a/suite-common/trading/src/thunks/__tests__/tradingThunk.test.ts +++ b/suite-common/trading/src/thunks/__tests__/tradingThunk.test.ts @@ -3,7 +3,7 @@ import { combineReducers } from '@reduxjs/toolkit'; import { createReducerWithExtraDeps, createThunk } from '@suite-common/redux-utils'; import { configureMockStore, extraDependenciesMock } from '@suite-common/test-utils'; import { confirmAddressOnDeviceThunk, selectSelectedDevice } from '@suite-common/wallet-core'; -import { AddressDisplayOptions } from '@suite-common/wallet-types'; +import { Account, AddressDisplayOptions } from '@suite-common/wallet-types'; import { tradingBuyActions } from '../../actions/buyActions'; import { accounts } from '../../reducers/__fixtures__/account'; @@ -26,6 +26,10 @@ jest.mock('@suite-common/wallet-core', () => ({ })); describe('Testing trading thunks', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + it('testing verifyAddressThunk - save verified address', async () => { const store = configureMockStore({ extra: {}, @@ -43,7 +47,7 @@ describe('Testing trading thunks', () => { }); const account = accounts[0]; - const address = account.addresses?.unused[0].address; + const addressData = account.addresses?.unused[0]; (selectSelectedDevice as jest.Mock).mockImplementation(() => ({ connected: true, @@ -60,15 +64,242 @@ describe('Testing trading thunks', () => { await store.dispatch( tradingThunks.verifyAddressThunk({ account, - address, - path: account.addresses?.unused[0].path, + address: addressData?.address, + path: addressData?.path, tradingAction: tradingBuyActions.verifyAddress.type, }), ); expect(store.getActions().length).toEqual(6); - expect(store.getState().wallet.trading.buy.addressVerified).toEqual(address); + expect(store.getState().wallet.trading.buy.addressVerified).toEqual(addressData?.address); + }); + + it('testing verifyAddressThunk - device not found', async () => { + const store = configureMockStore({ + extra: {}, + reducer: combineReducers({ + wallet: combineReducers({ + trading: tradingReducer, + }), + suite: mockedSuiteReducer(extraDependenciesMock), + }), + preloadedState: { + wallet: { + trading: initialState, + }, + }, + }); + + const account = accounts[0]; + const addressData = account.addresses?.unused[0]; + + (selectSelectedDevice as jest.Mock).mockImplementation(() => undefined); + + await store.dispatch( + tradingThunks.verifyAddressThunk({ + account, + address: addressData?.address, + path: addressData?.path, + tradingAction: tradingBuyActions.verifyAddress.type, + }), + ); + + expect(store.getActions().length).toEqual(2); + expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined); + }); + + it('testing verifyAddressThunk - path or address not defined', async () => { + const store = configureMockStore({ + extra: {}, + reducer: combineReducers({ + wallet: combineReducers({ + trading: tradingReducer, + }), + suite: mockedSuiteReducer(extraDependenciesMock), + }), + preloadedState: { + wallet: { + trading: initialState, + }, + }, + }); + + const account = { + ...accounts[0], + addresses: { + unused: [], + }, + } as unknown as Account; + const addressData = undefined; + + (selectSelectedDevice as jest.Mock).mockImplementation(() => ({ + connected: true, + available: true, + useEmptyPassphrase: true, + })); + + await store.dispatch( + tradingThunks.verifyAddressThunk({ + account, + address: addressData, + path: addressData, + tradingAction: tradingBuyActions.verifyAddress.type, + }), + ); + + expect(store.getActions().length).toEqual(2); + expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined); + }); + + it('testing verifyAddressThunk - device is not available', async () => { + const store = configureMockStore({ + extra: {}, + reducer: combineReducers({ + wallet: combineReducers({ + trading: tradingReducer, + }), + suite: mockedSuiteReducer(extraDependenciesMock), + }), + preloadedState: { + wallet: { + trading: initialState, + }, + }, + }); + + const account = accounts[0]; + const addressData = account.addresses?.unused[0]; + + (selectSelectedDevice as jest.Mock).mockImplementation(() => ({ + connected: true, + available: false, + useEmptyPassphrase: true, + })); + + await store.dispatch( + tradingThunks.verifyAddressThunk({ + account, + address: addressData?.address, + path: addressData?.path, + tradingAction: tradingBuyActions.verifyAddress.type, + }), + ); + + const actionModal = store + .getActions() + .find(action => action.type === '@modal/open-user-context'); + + expect(actionModal).toEqual({ + type: '@modal/open-user-context', + payload: { + type: 'unverified-address-proceed', + value: addressData?.address, + }, + }); + expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined); + }); + + it('testing verifyAddressThunk - device is not connected', async () => { + const store = configureMockStore({ + extra: {}, + reducer: combineReducers({ + wallet: combineReducers({ + trading: tradingReducer, + }), + suite: mockedSuiteReducer(extraDependenciesMock), + }), + preloadedState: { + wallet: { + trading: initialState, + }, + }, + }); + + const account = accounts[0]; + const addressData = account.addresses?.unused[0]; + + (selectSelectedDevice as jest.Mock).mockImplementation(() => ({ + connected: false, + available: true, + useEmptyPassphrase: true, + })); + + await store.dispatch( + tradingThunks.verifyAddressThunk({ + account, + address: addressData?.address, + path: addressData?.path, + tradingAction: tradingBuyActions.verifyAddress.type, + }), + ); + + const actionModal = store + .getActions() + .find(action => action.type === '@modal/open-user-context'); + + expect(actionModal).toEqual({ + type: '@modal/open-user-context', + payload: { + type: 'unverified-address-proceed', + value: addressData?.address, + }, + }); + expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined); }); - // TODO: add more tests + it('testing verifyAddressThunk - a confirmation of address on device is not successful', async () => { + const store = configureMockStore({ + extra: {}, + reducer: combineReducers({ + wallet: combineReducers({ + trading: tradingReducer, + }), + suite: mockedSuiteReducer(extraDependenciesMock), + }), + preloadedState: { + wallet: { + trading: initialState, + }, + }, + }); + + const account = accounts[0]; + const addressData = account.addresses?.unused[0]; + + (selectSelectedDevice as jest.Mock).mockImplementation(() => ({ + connected: true, + available: true, + useEmptyPassphrase: true, + })); + + const error = 'error message'; + (confirmAddressOnDeviceThunk as unknown as jest.Mock).mockImplementation( + createThunk('@suite/device/confirmAddressOnDeviceThunk', () => ({ + success: false, + payload: { + error, + code: 'error-code', + }, + })), + ); + + await store.dispatch( + tradingThunks.verifyAddressThunk({ + account, + address: addressData?.address, + path: addressData?.path, + tradingAction: tradingBuyActions.verifyAddress.type, + }), + ); + + const actionToest = store + .getActions() + .find(action => action.type === '@common/in-app-notifications/addToast'); + + expect(actionToest?.type).toEqual('@common/in-app-notifications/addToast'); + expect(actionToest?.payload?.type).toEqual('verify-address-error'); + expect(actionToest?.payload?.error).toEqual(error); + + expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined); + }); }); diff --git a/suite-common/trading/src/thunks/tradingThunks.ts b/suite-common/trading/src/thunks/tradingThunks.ts index 38bfc22927b..82daf527463 100644 --- a/suite-common/trading/src/thunks/tradingThunks.ts +++ b/suite-common/trading/src/thunks/tradingThunks.ts @@ -30,7 +30,7 @@ const verifyAddressThunk = createThunk( { dispatch, getState }, ) => { const device = selectSelectedDevice(getState()); - if (!device || !account) return; + if (!device) return; const accountAddress = getUnusedAddressFromAccount(account); address = address ?? accountAddress.address; path = path ?? accountAddress.path;