Skip to content

Commit ede1fd0

Browse files
committed
tests: added test on checking for new address generation on websocket update
1 parent 51774b3 commit ede1fd0

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

__tests__/wallet/wallet.test.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Mnemonic from 'bitcore-mnemonic';
1010
import { mockAxiosAdapter } from '../__mock_helpers__/axios-adapter.mock';
1111
import HathorWalletServiceWallet from '../../src/wallet/wallet';
1212
import Network from '../../src/models/network';
13-
import { GetAddressesObject, WsTransaction, CreateWalletAuthData } from '../../src/wallet/types';
13+
import { GetAddressesObject, WsTransaction, CreateWalletAuthData, AddressInfoObject } from '../../src/wallet/types';
1414
import config from '../../src/config';
1515
import {
1616
buildSuccessTxByIdTokenDataResponse,
@@ -102,6 +102,97 @@ test('getAddressAtIndex', async () => {
102102
await expect(wallet.getAddressAtIndex(0)).rejects.toThrow('Error getting wallet addresses.');
103103
});
104104

105+
describe('onNewTx', () => {
106+
const requestPassword = jest.fn();
107+
const network = new Network('testnet');
108+
const seed = defaultWalletSeed;
109+
110+
it('should call getNewAddresses if an output address is in newAddresses', async () => {
111+
const wallet = new HathorWalletServiceWallet({
112+
requestPassword,
113+
seed,
114+
network,
115+
});
116+
117+
const testAddress = 'testAddress1';
118+
(wallet as any).newAddresses = [{ address: testAddress, index: 0, addressPath: "m/0'/0/0" }] as AddressInfoObject[];
119+
120+
const getNewAddressesSpy = jest.spyOn(wallet as any, 'getNewAddresses').mockResolvedValue(undefined);
121+
122+
const newTx: WsTransaction = {
123+
tx_id: 'tx1',
124+
nonce: 0,
125+
timestamp: 0,
126+
signal_bits: 0,
127+
version: 1,
128+
weight: 1,
129+
parents: [],
130+
inputs: [],
131+
outputs: [
132+
{
133+
value: 100n,
134+
token_data: 0,
135+
script: { type: 'Buffer', data: [] },
136+
token: 'HTR',
137+
decoded: {
138+
type: 'P2PKH',
139+
address: testAddress,
140+
timelock: null,
141+
},
142+
locked: false,
143+
index: 0,
144+
},
145+
],
146+
};
147+
148+
await wallet.onNewTx(newTx);
149+
150+
expect(getNewAddressesSpy).toHaveBeenCalled();
151+
});
152+
153+
it('should not call getNewAddresses if no output address is in newAddresses', async () => {
154+
const wallet = new HathorWalletServiceWallet({
155+
requestPassword,
156+
seed,
157+
network,
158+
});
159+
160+
(wallet as any).newAddresses = [{ address: 'otherAddress', index: 0, addressPath: "m/0'/0/0" }] as AddressInfoObject[];
161+
162+
const getNewAddressesSpy = jest.spyOn(wallet as any, 'getNewAddresses').mockResolvedValue(undefined);
163+
164+
const newTx: WsTransaction = {
165+
tx_id: 'tx2',
166+
nonce: 0,
167+
timestamp: 0,
168+
signal_bits: 0,
169+
version: 1,
170+
weight: 1,
171+
parents: [],
172+
inputs: [],
173+
outputs: [
174+
{
175+
value: 100n,
176+
token_data: 0,
177+
script: { type: 'Buffer', data: [] },
178+
token: 'HTR',
179+
decoded: {
180+
type: 'P2PKH',
181+
address: 'someRandomAddress',
182+
timelock: null,
183+
},
184+
locked: false,
185+
index: 0,
186+
},
187+
],
188+
};
189+
190+
await wallet.onNewTx(newTx);
191+
192+
expect(getNewAddressesSpy).not.toHaveBeenCalled();
193+
});
194+
});
195+
105196
test('getTxBalance', async () => {
106197
const requestPassword = jest.fn();
107198
const network = new Network('testnet');

0 commit comments

Comments
 (0)