-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathtransaction.model.test.js
95 lines (82 loc) · 3.01 KB
/
transaction.model.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
import {expect} from 'chai';
import sinon from 'sinon';
import * as utils from '../test/utils';
import models from '../server/models';
const {Transaction} = models;
const userData = utils.data('user1');
const collectiveData = utils.data('collective1');
const transactionsData = utils.data('transactions1').transactions;
describe('transaction model', () => {
let user, host, collective, defaultTransactionData;
beforeEach(() => utils.resetTestDB());
beforeEach('create user', () => models.User.createUserWithCollective(userData).tap(u => user = u));
beforeEach('create host', () => models.User.createUserWithCollective(utils.data('host1')).tap(u => host = u));
beforeEach('create collective2 and add host', () =>
models.Collective.create(collectiveData)
.tap(g => collective = g)
.tap(() => {
defaultTransactionData = {
CreatedByUserId: user.id,
FromCollectiveId: user.CollectiveId,
CollectiveId: collective.id,
HostCollectiveId: host.CollectiveId
};
})
.then(() => collective.addHost(host.collective)));
it('automatically generates uuid', done => {
Transaction.create({
amount: -1000,
...defaultTransactionData
})
.then(transaction => {
expect(transaction.info.uuid).to.match(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
done();
})
.catch(done);
});
it('get the host', (done) => {
Transaction.create({
...defaultTransactionData,
amount: 10000
})
.then(transaction => {
expect(transaction.HostCollectiveId).to.equal(host.CollectiveId);
done();
})
});
it('createFromPayload creates a double entry transaction', () => {
return Transaction.createFromPayload({
transaction: transactionsData[7],
CreatedByUserId: user.id,
FromCollectiveId: user.CollectiveId,
CollectiveId: collective.id
})
.then(() => {
return Transaction.findAll()
.then(transactions => {
expect(transactions.length).to.equal(2);
expect(transactions[0] instanceof models.Transaction).to.be.true;
expect(transactions[0].description).to.equal(transactionsData[7].description);
expect(transactions[0].amount).to.equal(-transactionsData[7].netAmountInCollectiveCurrency);
expect(transactions[1].amount).to.equal(-transactions[0].netAmountInCollectiveCurrency);
})
})
})
it('createFromPayload() generates a new activity', (done) => {
const createActivityStub = sinon.stub(Transaction, 'createActivity', (t) => {
expect(Math.abs(t.amount)).to.equal(Math.abs(transactionsData[7].netAmountInCollectiveCurrency));
createActivityStub.restore();
done();
});
Transaction.createFromPayload({
transaction: transactionsData[7],
CreatedByUserId: user.id,
FromCollectiveId: user.CollectiveId,
CollectiveId: collective.id
})
.then(transaction => {
expect(transaction.CollectiveId).to.equal(collective.id);
})
.catch(done);
});
});