This repository was archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathWalletTest.js
177 lines (120 loc) · 5.34 KB
/
WalletTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as Utils from 'web3-utils';
import Wallet from '../../../src/models/Wallet';
import Account from '../../../src/models/Account';
import Accounts from '../../../src/Accounts';
// Mocks
jest.mock('Utils');
jest.mock('../../../src/models/Account');
jest.mock('../../../src/Accounts');
/**
* Wallet test
*/
describe('WalletTest', () => {
let wallet, accountsMock;
beforeEach(() => {
new Accounts();
accountsMock = Accounts.mock.instances[0];
wallet = new Wallet(Utils, accountsMock);
});
it('constructor check', () => {
expect(wallet.utils).toEqual(Utils);
expect(wallet.accountsModule).toEqual(accountsMock);
expect(wallet.accounts).toEqual({});
expect(wallet.accountsIndex).toEqual(0);
expect(wallet.defaultKeyName).toEqual('web3js_wallet');
});
it('calls the length property and returns the accountsIndex', () => {
wallet.accountsIndex = 99;
expect(wallet.length).toEqual(99);
});
it('calls create and returns the expected value', () => {
Utils.randomHex.mockReturnValueOnce('asdf');
Account.from.mockReturnValueOnce({address: '0x0', privateKey: '0x0'});
expect(wallet.create(1)).toEqual(wallet);
expect(Utils.randomHex).toHaveBeenCalledWith(32);
expect(Account.from).toHaveBeenCalledWith('asdf', accountsMock);
expect(wallet.accountsIndex).toEqual(1);
});
it('calls add with a Account object and returns the expected value', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0';
expect(wallet.add(accountMock)).toEqual(accountMock);
expect(wallet.accounts[accountMock.address]).toEqual(accountMock);
expect(wallet.accounts[0]).toEqual(accountMock);
expect(wallet.accounts[accountMock.address.toLowerCase()]).toEqual(accountMock);
});
it('calls add with an already existing account and returns the expected value', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0';
wallet.accounts['0x0'] = accountMock;
expect(wallet.add(accountMock)).toEqual(accountMock);
expect(wallet.accounts[accountMock.address]).toEqual(accountMock);
});
it('calls add with a privateKey and returns the expected value', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0';
Account.fromPrivateKey.mockReturnValueOnce(accountMock);
expect(wallet.add('0x0')).toEqual(accountMock);
expect(Account.fromPrivateKey).toHaveBeenCalledWith('0x0', accountsMock);
expect(wallet.accounts[accountMock.address]).toEqual(accountMock);
expect(wallet.accounts[0]).toEqual(accountMock);
expect(wallet.accounts[accountMock.address.toLowerCase()]).toEqual(accountMock);
});
it('calls get returns the expected value', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0';
wallet.accounts['0x0'] = accountMock;
expect(wallet.get('0x0')).toEqual(accountMock);
});
it('calls remove and returns true', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0A';
wallet.accounts['0xA'] = accountMock;
wallet.accounts['0xa'] = accountMock;
expect(wallet.remove('0xA')).toEqual(true);
expect(wallet.accountsIndex).toEqual(0);
});
it('calls remove and returns false', () => {
expect(wallet.remove(0)).toEqual(false);
});
it('calls clear and returns the expect value', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0';
expect(wallet.clear()).toEqual(wallet);
expect(wallet.accountsIndex).toEqual(0);
});
it('calls encrypt and returns the expect value', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0';
accountMock.encrypt.mockReturnValueOnce(true);
wallet.accounts[0] = accountMock;
expect(wallet.encrypt('pw', {})).toEqual([true]);
expect(accountMock.encrypt).toHaveBeenCalledWith('pw', {});
expect(wallet.accountsIndex).toEqual(0);
});
it('calls decrypt and returns the expected value', () => {
new Account();
const accountMock = Account.mock.instances[0];
accountMock.address = '0x0';
Account.fromV3Keystore.mockReturnValueOnce(accountMock);
expect(wallet.decrypt([true], 'pw')).toEqual(wallet);
expect(Account.fromV3Keystore).toHaveBeenCalledWith(true, 'pw', false, accountsMock);
expect(wallet.accounts[accountMock.address]).toEqual(accountMock);
expect(wallet.accounts[0]).toEqual(accountMock);
expect(wallet.accounts[accountMock.address.toLowerCase()]).toEqual(accountMock);
});
it('calls decrypt and throws an error', () => {
Account.fromV3Keystore.mockReturnValueOnce(false);
expect(() => {
wallet.decrypt([true], 'pw');
}).toThrow('Couldn\'t decrypt accounts. Password wrong?');
expect(Account.fromV3Keystore).toHaveBeenCalledWith(true, 'pw', false, accountsMock);
});
});