SenShaMart/wallet/wallet.test.js

91 lines
No EOL
3.1 KiB
JavaScript

const Wallet = require('./index');
const Blockchain = require('../blockchain');
const ChainUtil = require('../chain-util');
const { INITIAL_BALANCE } = require('../config');
describe('Wallet', () => {
let wallet, bc;
beforeEach(() => {
wallet = new Wallet(ChainUtil.genKeyPair());
bc = new Blockchain();
});
describe('creating a transaction', () => {
let transaction, sendAmount, recipient;
beforeEach(() => {
sendAmount = 50;
recipient = 'r4nd0m-4ddr355';
transaction = wallet.createTransaction(recipient, sendAmount, bc);
tp.updateOrAddTransaction(transaction);
});
describe('and doing the same transaction', () => {
beforeEach(() => {
transaction = wallet.createTransaction(recipient, sendAmount, bc);
tp.updateOrAddTransaction(transaction);
});
//?
it('doubles the `sendAmount` subtracted from the wallet balance', () => {
expect(transaction.outputs.find(output => output.address === wallet.publicKey).amount)
.toEqual(wallet.balance - sendAmount * 2);
});
it('clones the `sendAmount` output for the recipient', () => {
expect(transaction.outputs.filter(output => output.address === recipient)
.map(output => output.amount)).toEqual([sendAmount, sendAmount]);
});
});
});
describe('calculating a balance', () => {
let addBalance, repeatAdd, senderWallet;
beforeEach(() => {
senderWallet = new Wallet();
addBalance = 100;
repeatAdd = 3;
for (let i=0; i<repeatAdd; i++) {
const transaction = senderWallet.createTransaction(wallet.publicKey, addBalance, bc);
tp.updateOrAddTransaction(transaction);
}
bc.addBlock(tp.transactions);
});
it('calculates the balance for blockchain transactions matching the recipient', () => {
expect(wallet.calculateBalance(bc)).toEqual(INITIAL_BALANCE + (addBalance * repeatAdd));
});
it('calculates the balance for blockchain transactions matching the sender', () => {
expect(senderWallet.calculateBalance(bc)).toEqual(INITIAL_BALANCE - (addBalance * repeatAdd));
});
describe('and the recipient conducts a transaction', () => {
let subtractBalance, recipientBalance;
beforeEach(() => {
tp.clear();
subtractBalance = 60;
recipientBalance = wallet.calculateBalance(bc);
const transaction = wallet.createTransaction(senderWallet.publicKey, subtractBalance, bc);
tp.updateOrAddTransaction(transaction);
bc.addBlock(tp.transactions);
});
describe('and the sender sends another transaction to the recipient', () => {
beforeEach(() => {
tp.clear();
const transaction = senderWallet.createTransaction(wallet.publicKey, addBalance, bc);
tp.updateOrAddTransaction(transaction);
bc.addBlock(tp.transactions);
});
it('calculate the recipient balance only using transactions since its most recent one', () => {
expect(wallet.calculateBalance(bc)).toEqual(recipientBalance - subtractBalance + addBalance);
});
});
});
});
});