Skip to content

Commit

Permalink
test(trading): verifyAddressThunk
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Feb 10, 2025
1 parent c080226 commit 86b12f3
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 7 deletions.
243 changes: 237 additions & 6 deletions suite-common/trading/src/thunks/__tests__/tradingThunk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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: {},
Expand All @@ -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,
Expand All @@ -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);
});
});
2 changes: 1 addition & 1 deletion suite-common/trading/src/thunks/tradingThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 86b12f3

Please sign in to comment.