/
account.test.js
380 lines (320 loc) · 11.4 KB
/
account.test.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import { expect } from 'chai';
import { spy, stub } from 'sinon';
import actionTypes from '../constants/actions';
import {
accountUpdated,
accountLoggedOut,
secondPassphraseRegistered,
delegateRegistered,
removePassphrase,
passphraseUsed,
loadDelegate,
loadAccount,
accountDataUpdated,
updateTransactionsIfNeeded,
} from './account';
import { errorAlertDialogDisplayed } from './dialog';
import { delegateRegisteredFailure } from './delegate';
import * as accountApi from '../utils/api/account';
import * as delegateApi from '../utils/api/delegate';
import Fees from '../constants/fees';
import transactionTypes from '../constants/transactionTypes';
import networks from '../constants/networks';
import accounts from '../../test/constants/accounts';
import * as peersActions from './peers';
import * as transactionsActions from './transactions';
describe('actions: account', () => {
describe('accountUpdated', () => {
it('should create an action to set values to account', () => {
const data = {
passphrase: 'robust swift grocery peasant forget share enable convince deputy road keep cheap',
};
const expectedAction = {
data,
type: actionTypes.accountUpdated,
};
expect(accountUpdated(data)).to.be.deep.equal(expectedAction);
});
});
describe('accountLoggedOut', () => {
it('should create an action to reset the account', () => {
const expectedAction = {
type: actionTypes.accountLoggedOut,
};
expect(accountLoggedOut()).to.be.deep.equal(expectedAction);
});
});
describe('secondPassphraseRegistered', () => {
let accountApiMock;
const data = {
activePeer: {},
secondPassphrase: 'sample second passphrase',
account: {
publicKey: 'test_public-key',
address: 'test_address',
},
};
const actionFunction = secondPassphraseRegistered(data);
let dispatch;
beforeEach(() => {
accountApiMock = stub(accountApi, 'setSecondPassphrase');
dispatch = spy();
});
afterEach(() => {
accountApiMock.restore();
});
it('should create an action function', () => {
expect(typeof actionFunction).to.be.deep.equal('function');
});
it('should dispatch transactionAdded action if resolved', () => {
accountApiMock.returnsPromise().resolves({ transactionId: '15626650747375562521' });
const expectedAction = {
id: '15626650747375562521',
senderPublicKey: 'test_public-key',
senderId: 'test_address',
amount: 0,
fee: Fees.setSecondPassphrase,
type: transactionTypes.setSecondPassphrase,
};
actionFunction(dispatch);
expect(dispatch).to.have.been
.calledWith({ data: expectedAction, type: actionTypes.transactionAdded });
});
it('should dispatch errorAlertDialogDisplayed action if caught', () => {
accountApiMock.returnsPromise().rejects({ message: 'sample message' });
actionFunction(dispatch);
const expectedAction = errorAlertDialogDisplayed({ text: 'sample message' });
expect(dispatch).to.have.been.calledWith(expectedAction);
});
it('should dispatch errorAlertDialogDisplayed action if caught but no message returned', () => {
accountApiMock.returnsPromise().rejects({});
actionFunction(dispatch);
const expectedAction = errorAlertDialogDisplayed({ text: 'An error occurred while registering your second passphrase. Please try again.' });
expect(dispatch).to.have.been.calledWith(expectedAction);
});
});
describe('delegateRegistered', () => {
let delegateApiMock;
const data = {
activePeer: {},
username: 'test',
passphrase: accounts.genesis.passphrase,
secondPassphrase: null,
account: {
publicKey: 'test_public-key',
address: 'test_address',
},
};
const actionFunction = delegateRegistered(data);
let dispatch;
beforeEach(() => {
delegateApiMock = stub(delegateApi, 'registerDelegate');
dispatch = spy();
});
afterEach(() => {
delegateApiMock.restore();
});
it('should create an action function', () => {
expect(typeof actionFunction).to.be.deep.equal('function');
});
it('should dispatch transactionAdded action if resolved', () => {
delegateApiMock.returnsPromise().resolves({ transactionId: '15626650747375562521' });
const expectedAction = {
id: '15626650747375562521',
senderPublicKey: 'test_public-key',
senderId: 'test_address',
username: data.username,
amount: 0,
fee: Fees.registerDelegate,
type: transactionTypes.registerDelegate,
};
actionFunction(dispatch);
expect(dispatch).to.have.been
.calledWith({ data: expectedAction, type: actionTypes.transactionAdded });
});
it('should dispatch delegateRegisteredFailure action if caught', () => {
delegateApiMock.returnsPromise().rejects({ message: 'sample message.' });
actionFunction(dispatch);
const delegateRegisteredFailureAction = delegateRegisteredFailure({ message: 'sample message.' });
expect(dispatch).to.have.been.calledWith(delegateRegisteredFailureAction);
});
it('should dispatch passphraseUsed action always', () => {
delegateApiMock.returnsPromise().rejects({ message: 'sample message.' });
actionFunction(dispatch);
const passphraseUsedAction = passphraseUsed(accounts.genesis.passphrase);
expect(dispatch).to.have.been.calledWith(passphraseUsedAction);
});
});
describe('loadDelegate', () => {
let delegateApiMock;
let dispatch;
const data = {
activePeer: {},
publicKey: accounts.genesis.publicKey,
};
const actionFunction = loadDelegate(data);
beforeEach(() => {
delegateApiMock = stub(delegateApi, 'getDelegate');
dispatch = spy();
});
afterEach(() => {
delegateApiMock.restore();
});
it('should dispatch updateDelegate with delegate response', () => {
const delegateResponse = { delegate: { ...accounts['delegate candidate'] } };
delegateApiMock.returnsPromise().resolves(delegateResponse);
actionFunction(dispatch);
const updateDelegateAction = {
data: delegateResponse,
type: actionTypes.updateDelegate,
};
expect(dispatch).to.have.been.calledWith(updateDelegateAction);
});
});
describe('removePassphrase', () => {
it('should create an action to remove passphrase', () => {
const data = {
publicKey: accounts.genesis.publicKey,
network: networks.testnet,
address: accounts.genesis.address,
};
const expectedAction = {
data,
type: actionTypes.removePassphrase,
};
expect(removePassphrase(data)).to.be.deep.equal(expectedAction);
});
});
describe('loadAccount', () => {
let getAccountStub;
let transactionsActionsStub;
const dispatch = spy();
beforeEach(() => {
getAccountStub = stub(accountApi, 'getAccount').returnsPromise();
transactionsActionsStub = spy(transactionsActions, 'loadTransactionsFinish');
});
afterEach(() => {
getAccountStub.restore();
transactionsActionsStub.restore();
});
it('should finish transactions load and load delegate if not own account', () => {
getAccountStub.resolves({
balance: 10e8,
publicKey: accounts.genesis.publicKey,
isDelegate: false,
});
const data = {
activePeer: {},
address: accounts.genesis.address,
transactionsResponse: { count: 0, transactions: [] },
isSameAccount: false,
};
loadAccount(data)(dispatch);
expect(transactionsActionsStub).to.have.been.calledWith({
confirmed: [],
count: 0,
balance: 10e8,
address: accounts.genesis.address,
});
});
it('should finish transactions load and should not load delegate if own account', () => {
getAccountStub.resolves({
balance: 10e8,
publicKey: accounts.genesis.publicKey,
isDelegate: true,
delegate: 'delegate information',
});
const data = {
activePeer: {},
address: accounts.genesis.address,
transactionsResponse: { count: 0, transactions: [] },
isSameAccount: true,
};
loadAccount(data)(dispatch);
expect(transactionsActionsStub).to.have.been.calledWith({
confirmed: [],
count: 0,
balance: 10e8,
address: accounts.genesis.address,
delegate: 'delegate information',
});
});
});
describe('accountDataUpdated', () => {
let peersActionsStub;
let getAccountStub;
let transactionsActionsStub;
const dispatch = spy();
beforeEach(() => {
peersActionsStub = spy(peersActions, 'activePeerUpdate');
getAccountStub = stub(accountApi, 'getAccount').returnsPromise();
transactionsActionsStub = spy(transactionsActions, 'transactionsUpdated');
});
afterEach(() => {
getAccountStub.restore();
peersActionsStub.restore();
transactionsActionsStub.restore();
});
it(`should call account API methods on ${actionTypes.newBlockCreated} action when online`, () => {
getAccountStub.resolves({ balance: 10e8 });
const data = {
windowIsFocused: false,
peers: { data: {} },
transactions: {
pending: [{
id: 12498250891724098,
}],
confirmed: [],
account: { address: 'test_address', balance: 0 },
},
account: { address: accounts.genesis.address, balance: 0 },
};
accountDataUpdated(data)(dispatch);
expect(dispatch).to.have.callCount(3);
expect(peersActionsStub).to.have.not.been.calledWith({ online: false, code: 'EUNAVAILABLE' });
});
it(`should call account API methods on ${actionTypes.newBlockCreated} action when offline`, () => {
getAccountStub.rejects({ error: { code: 'EUNAVAILABLE' } });
const data = {
windowIsFocused: true,
peers: { data: {} },
transactions: {
pending: [{ id: 12498250891724098 }],
confirmed: [],
account: { address: 'test_address', balance: 0 },
},
account: { address: accounts.genesis.address },
};
accountDataUpdated(data)(dispatch);
expect(peersActionsStub).to.have.been.calledWith({ online: false, code: 'EUNAVAILABLE' });
});
});
describe('updateTransactionsIfNeeded', () => {
let transactionsActionsStub;
const dispatch = spy();
beforeEach(() => {
transactionsActionsStub = spy(transactionsActions, 'transactionsUpdated');
});
afterEach(() => {
transactionsActionsStub.restore();
});
it('should update transactions when window is in focus', () => {
const data = {
activePeer: {},
transactions: { confirmed: [{ confirmations: 10 }], pending: [] },
account: { address: accounts.genesis.address },
};
updateTransactionsIfNeeded(data, true)(dispatch);
expect(transactionsActionsStub).to.have.been.calledWith();
});
it('should update transactions when there are no recent transactions', () => {
const data = {
activePeer: {},
transactions: { confirmed: [{ confirmations: 10000 }], pending: [] },
account: { address: accounts.genesis.address },
};
updateTransactionsIfNeeded(data, false)(dispatch);
expect(transactionsActionsStub).to.have.been.calledWith();
});
});
});