-
Notifications
You must be signed in to change notification settings - Fork 96
/
transactions.test.js
238 lines (209 loc) · 7.46 KB
/
transactions.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
import { expect } from 'chai';
import sinon from 'sinon';
import actionTypes from '../constants/actions';
import txFilters from './../constants/transactionFilters';
import { sent, transactionsRequested, loadTransaction, transactionsUpdated } from './transactions';
import * as transactionsApi from '../utils/api/transactions';
import * as delegateApi from '../utils/api/delegate';
import accounts from '../../test/constants/accounts';
import transactionTypes from '../constants/transactionTypes';
import Fees from '../constants/fees';
import { toRawLsk } from '../utils/lsk';
describe('actions: transactions', () => {
describe('transactionsUpdated', () => {
let transactionsApiMock;
const data = {
activePeer: {},
address: '15626650747375562521',
limit: 20,
offset: 0,
filter: txFilters.all,
};
const actionFunction = transactionsUpdated(data);
let dispatch;
beforeEach(() => {
transactionsApiMock = sinon.stub(transactionsApi, 'getTransactions');
dispatch = sinon.spy();
});
afterEach(() => {
transactionsApiMock.restore();
});
it('should dispatch transactionsUpdated action if resolved', () => {
transactionsApiMock.returnsPromise().resolves({ data: [], meta: { count: '0' } });
const expectedAction = {
count: 0,
confirmed: [],
};
actionFunction(dispatch);
expect(dispatch).to.have.been.calledWith({
data: expectedAction,
type: actionTypes.transactionsUpdated,
});
});
});
describe('transactionsRequested', () => {
let transactionsApiMock;
const data = {
activePeer: {},
address: '15626650747375562521L',
limit: 20,
offset: 0,
filter: txFilters.all,
};
const actionFunction = transactionsRequested(data);
let dispatch;
beforeEach(() => {
transactionsApiMock = sinon.stub(transactionsApi, 'getTransactions');
dispatch = sinon.spy();
});
afterEach(() => {
transactionsApiMock.restore();
});
it('should create an action function', () => {
expect(typeof actionFunction).to.be.deep.equal('function');
});
it('should dispatch transactionsLoaded action if resolved', () => {
transactionsApiMock.returnsPromise().resolves({ data: [], meta: { count: '0' } });
const expectedAction = {
count: 0,
confirmed: [],
address: data.address,
filter: data.filter,
};
actionFunction(dispatch);
expect(dispatch).to.have.been
.calledWith({ data: expectedAction, type: actionTypes.transactionsLoaded });
});
});
describe('loadTransaction', () => {
let transactionApiMock;
let delegateApiMock;
const data = {
activePeer: {
options: {
name: 'Mainnet',
},
},
address: '15626650747375562521',
limit: 20,
offset: 0,
filter: txFilters.all,
};
const actionFunction = loadTransaction(data);
let dispatch;
beforeEach(() => {
transactionApiMock = sinon.stub(transactionsApi, 'getSingleTransaction');
delegateApiMock = sinon.stub(delegateApi, 'getDelegate');
dispatch = sinon.spy();
});
afterEach(() => {
transactionApiMock.restore();
delegateApiMock.restore();
});
it('should create an action function', () => {
expect(typeof actionFunction).to.be.deep.equal('function');
});
// TODO: enable this when voting functionalities is fixed
it.skip('should dispatch one transactionAddDelegateName action when transaction contains one vote added', () => {
const delegateResponse = { delegate: { username: 'peterpan' } };
const transactionResponse = {
asset: {
votes: [`+${accounts.delegate.publicKey}`],
},
};
transactionApiMock.returnsPromise().resolves({ data: [transactionResponse] });
delegateApiMock.returnsPromise().resolves({ data: delegateResponse });
const expectedActionPayload = {
...delegateResponse,
voteArrayName: 'added',
};
actionFunction(dispatch);
expect(dispatch).to.have.been
.calledWith({ data: transactionResponse, type: actionTypes.transactionLoaded });
expect(dispatch).to.have.been
.calledWith({ data: expectedActionPayload, type: actionTypes.transactionAddDelegateName });
});
// TODO: enable this when voting functionalities is fixed
it.skip('should dispatch one transactionAddDelegateName action when transaction contains one vote deleted', () => {
const delegateResponse = { delegate: { username: 'peterpan' } };
const transactionResponse = { transaction: { votes: { deleted: [accounts.delegate.publicKey] }, count: '0' } };
transactionApiMock.returnsPromise().resolves(transactionResponse);
delegateApiMock.returnsPromise().resolves(delegateResponse);
const expectedActionPayload = {
...delegateResponse,
voteArrayName: 'deleted',
};
actionFunction(dispatch);
expect(dispatch).to.have.been
.calledWith({ data: transactionResponse, type: actionTypes.transactionLoaded });
expect(dispatch).to.have.been
.calledWith({ data: expectedActionPayload, type: actionTypes.transactionAddDelegateName });
});
});
describe('sent', () => {
let transactionsApiMock;
const data = {
activePeer: {},
recipientId: '15833198055097037957L',
amount: 100,
passphrase: 'sample passphrase',
secondPassphrase: null,
account: {
publicKey: 'test_public-key',
address: 'test_address',
},
};
const actionFunction = sent(data);
let dispatch;
beforeEach(() => {
transactionsApiMock = sinon.stub(transactionsApi, 'send');
dispatch = sinon.spy();
});
afterEach(() => {
transactionsApiMock.restore();
});
it('should create an action function', () => {
expect(typeof actionFunction).to.be.deep.equal('function');
});
it('should dispatch transactionAdded action if resolved', () => {
transactionsApiMock.returnsPromise().resolves({ id: '15626650747375562521' });
const expectedAction = {
id: '15626650747375562521',
senderPublicKey: 'test_public-key',
senderId: 'test_address',
recipientId: data.recipientId,
amount: toRawLsk(data.amount),
fee: Fees.send,
type: transactionTypes.send,
};
actionFunction(dispatch);
expect(dispatch).to.have.been
.calledWith({ data: expectedAction, type: actionTypes.transactionAdded });
});
it('should dispatch transactionFailed action if caught', () => {
transactionsApiMock.returnsPromise().rejects({ message: 'sample message' });
actionFunction(dispatch);
const expectedAction = {
data: {
errorMessage: 'sample message.',
},
type: actionTypes.transactionFailed,
};
expect(dispatch).to.have.been.calledWith(expectedAction);
});
it('should dispatch transactionFailed action if caught but no message returned', () => {
transactionsApiMock.returnsPromise().rejects({});
actionFunction(dispatch);
const expectedAction = {
data: {
errorMessage: 'An error occurred while creating the transaction.',
},
type: actionTypes.transactionFailed,
};
expect(dispatch).to.have.been.calledWith(expectedAction);
});
});
// describe('accountLoggedOut', () => {
// it('should create an action to reset the account', () => {
// });
});