Skip to content

Commit 3e20919

Browse files
committed
test(trading): verifyAddressThunk
1 parent 139b75d commit 3e20919

File tree

2 files changed

+238
-7
lines changed

2 files changed

+238
-7
lines changed

Diff for: suite-common/trading/src/thunks/__tests__/tradingThunk.test.ts

+237-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { combineReducers } from '@reduxjs/toolkit';
33
import { createReducerWithExtraDeps, createThunk } from '@suite-common/redux-utils';
44
import { configureMockStore, extraDependenciesMock } from '@suite-common/test-utils';
55
import { confirmAddressOnDeviceThunk, selectSelectedDevice } from '@suite-common/wallet-core';
6-
import { AddressDisplayOptions } from '@suite-common/wallet-types';
6+
import { Account, AddressDisplayOptions } from '@suite-common/wallet-types';
77

88
import { tradingBuyActions } from '../../actions/buyActions';
99
import { accounts } from '../../reducers/__fixtures__/account';
@@ -26,6 +26,10 @@ jest.mock('@suite-common/wallet-core', () => ({
2626
}));
2727

2828
describe('Testing trading thunks', () => {
29+
afterEach(() => {
30+
jest.clearAllMocks();
31+
});
32+
2933
it('testing verifyAddressThunk - save verified address', async () => {
3034
const store = configureMockStore({
3135
extra: {},
@@ -43,7 +47,7 @@ describe('Testing trading thunks', () => {
4347
});
4448

4549
const account = accounts[0];
46-
const address = account.addresses?.unused[0].address;
50+
const addressData = account.addresses?.unused[0];
4751

4852
(selectSelectedDevice as jest.Mock).mockImplementation(() => ({
4953
connected: true,
@@ -60,15 +64,242 @@ describe('Testing trading thunks', () => {
6064
await store.dispatch(
6165
tradingThunks.verifyAddressThunk({
6266
account,
63-
address,
64-
path: account.addresses?.unused[0].path,
67+
address: addressData?.address,
68+
path: addressData?.path,
6569
tradingAction: tradingBuyActions.verifyAddress.type,
6670
}),
6771
);
6872

6973
expect(store.getActions().length).toEqual(6);
70-
expect(store.getState().wallet.trading.buy.addressVerified).toEqual(address);
74+
expect(store.getState().wallet.trading.buy.addressVerified).toEqual(addressData?.address);
75+
});
76+
77+
it('testing verifyAddressThunk - device not found', async () => {
78+
const store = configureMockStore({
79+
extra: {},
80+
reducer: combineReducers({
81+
wallet: combineReducers({
82+
trading: tradingReducer,
83+
}),
84+
suite: mockedSuiteReducer(extraDependenciesMock),
85+
}),
86+
preloadedState: {
87+
wallet: {
88+
trading: initialState,
89+
},
90+
},
91+
});
92+
93+
const account = accounts[0];
94+
const addressData = account.addresses?.unused[0];
95+
96+
(selectSelectedDevice as jest.Mock).mockImplementation(() => undefined);
97+
98+
await store.dispatch(
99+
tradingThunks.verifyAddressThunk({
100+
account,
101+
address: addressData?.address,
102+
path: addressData?.path,
103+
tradingAction: tradingBuyActions.verifyAddress.type,
104+
}),
105+
);
106+
107+
expect(store.getActions().length).toEqual(2);
108+
expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined);
109+
});
110+
111+
it('testing verifyAddressThunk - path or address not defined', async () => {
112+
const store = configureMockStore({
113+
extra: {},
114+
reducer: combineReducers({
115+
wallet: combineReducers({
116+
trading: tradingReducer,
117+
}),
118+
suite: mockedSuiteReducer(extraDependenciesMock),
119+
}),
120+
preloadedState: {
121+
wallet: {
122+
trading: initialState,
123+
},
124+
},
125+
});
126+
127+
const account = {
128+
...accounts[0],
129+
addresses: {
130+
unused: [],
131+
},
132+
} as unknown as Account;
133+
const addressData = undefined;
134+
135+
(selectSelectedDevice as jest.Mock).mockImplementation(() => ({
136+
connected: true,
137+
available: true,
138+
useEmptyPassphrase: true,
139+
}));
140+
141+
await store.dispatch(
142+
tradingThunks.verifyAddressThunk({
143+
account,
144+
address: addressData,
145+
path: addressData,
146+
tradingAction: tradingBuyActions.verifyAddress.type,
147+
}),
148+
);
149+
150+
expect(store.getActions().length).toEqual(2);
151+
expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined);
152+
});
153+
154+
it('testing verifyAddressThunk - device is not available', async () => {
155+
const store = configureMockStore({
156+
extra: {},
157+
reducer: combineReducers({
158+
wallet: combineReducers({
159+
trading: tradingReducer,
160+
}),
161+
suite: mockedSuiteReducer(extraDependenciesMock),
162+
}),
163+
preloadedState: {
164+
wallet: {
165+
trading: initialState,
166+
},
167+
},
168+
});
169+
170+
const account = accounts[0];
171+
const addressData = account.addresses?.unused[0];
172+
173+
(selectSelectedDevice as jest.Mock).mockImplementation(() => ({
174+
connected: true,
175+
available: false,
176+
useEmptyPassphrase: true,
177+
}));
178+
179+
await store.dispatch(
180+
tradingThunks.verifyAddressThunk({
181+
account,
182+
address: addressData?.address,
183+
path: addressData?.path,
184+
tradingAction: tradingBuyActions.verifyAddress.type,
185+
}),
186+
);
187+
188+
const actionModal = store
189+
.getActions()
190+
.find(action => action.type === '@modal/open-user-context');
191+
192+
expect(actionModal).toEqual({
193+
type: '@modal/open-user-context',
194+
payload: {
195+
type: 'unverified-address-proceed',
196+
value: addressData?.address,
197+
},
198+
});
199+
expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined);
200+
});
201+
202+
it('testing verifyAddressThunk - device is not connected', async () => {
203+
const store = configureMockStore({
204+
extra: {},
205+
reducer: combineReducers({
206+
wallet: combineReducers({
207+
trading: tradingReducer,
208+
}),
209+
suite: mockedSuiteReducer(extraDependenciesMock),
210+
}),
211+
preloadedState: {
212+
wallet: {
213+
trading: initialState,
214+
},
215+
},
216+
});
217+
218+
const account = accounts[0];
219+
const addressData = account.addresses?.unused[0];
220+
221+
(selectSelectedDevice as jest.Mock).mockImplementation(() => ({
222+
connected: false,
223+
available: true,
224+
useEmptyPassphrase: true,
225+
}));
226+
227+
await store.dispatch(
228+
tradingThunks.verifyAddressThunk({
229+
account,
230+
address: addressData?.address,
231+
path: addressData?.path,
232+
tradingAction: tradingBuyActions.verifyAddress.type,
233+
}),
234+
);
235+
236+
const actionModal = store
237+
.getActions()
238+
.find(action => action.type === '@modal/open-user-context');
239+
240+
expect(actionModal).toEqual({
241+
type: '@modal/open-user-context',
242+
payload: {
243+
type: 'unverified-address-proceed',
244+
value: addressData?.address,
245+
},
246+
});
247+
expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined);
71248
});
72249

73-
// TODO: add more tests
250+
it('testing verifyAddressThunk - a confirmation of address on device is not successful', async () => {
251+
const store = configureMockStore({
252+
extra: {},
253+
reducer: combineReducers({
254+
wallet: combineReducers({
255+
trading: tradingReducer,
256+
}),
257+
suite: mockedSuiteReducer(extraDependenciesMock),
258+
}),
259+
preloadedState: {
260+
wallet: {
261+
trading: initialState,
262+
},
263+
},
264+
});
265+
266+
const account = accounts[0];
267+
const addressData = account.addresses?.unused[0];
268+
269+
(selectSelectedDevice as jest.Mock).mockImplementation(() => ({
270+
connected: true,
271+
available: true,
272+
useEmptyPassphrase: true,
273+
}));
274+
275+
const error = 'error message';
276+
(confirmAddressOnDeviceThunk as unknown as jest.Mock).mockImplementation(
277+
createThunk('@suite/device/confirmAddressOnDeviceThunk', () => ({
278+
success: false,
279+
payload: {
280+
error,
281+
code: 'error-code',
282+
},
283+
})),
284+
);
285+
286+
await store.dispatch(
287+
tradingThunks.verifyAddressThunk({
288+
account,
289+
address: addressData?.address,
290+
path: addressData?.path,
291+
tradingAction: tradingBuyActions.verifyAddress.type,
292+
}),
293+
);
294+
295+
const actionToest = store
296+
.getActions()
297+
.find(action => action.type === '@common/in-app-notifications/addToast');
298+
299+
expect(actionToest?.type).toEqual('@common/in-app-notifications/addToast');
300+
expect(actionToest?.payload?.type).toEqual('verify-address-error');
301+
expect(actionToest?.payload?.error).toEqual(error);
302+
303+
expect(store.getState().wallet.trading.buy.addressVerified).toEqual(undefined);
304+
});
74305
});

Diff for: suite-common/trading/src/thunks/tradingThunks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const verifyAddressThunk = createThunk(
3030
{ dispatch, getState },
3131
) => {
3232
const device = selectSelectedDevice(getState());
33-
if (!device || !account) return;
33+
if (!device) return;
3434
const accountAddress = getUnusedAddressFromAccount(account);
3535
address = address ?? accountAddress.address;
3636
path = path ?? accountAddress.path;

0 commit comments

Comments
 (0)