machinomy
Version:
Micropayments powered by Ethereum
101 lines • 4.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const sinon = require("sinon");
const contracts = require("@machinomy/contracts");
const ChannelEthContract_1 = require("./ChannelEthContract");
const Signature_1 = require("./Signature");
const expect = require("expect");
const ChannelId_1 = require("./ChannelId");
const ID = '0e29e61f256b40b2a6280f8181a1b5ff';
const RANDOM_ID = 'RANDOM';
const SETTLEMENT_PERIOD = new bignumber_js_1.BigNumber(1234);
const RECEIVER = '0xRECEIVER';
const VALUE = new bignumber_js_1.BigNumber(10);
const SENDER = '0xSENDER';
const SIG = Signature_1.default.fromRpcSig('0xd8a923b39ae82bb39d3b64d58f06e1d776bcbcae34e5b4a6f4a952e8892e6a5b4c0f88833c06fe91729057035161e599fda536e8ce0ab4be2c214d6ea961e93a01');
describe('ChannelEthContract', () => {
const web3 = { currentProvider: {} };
let deployed;
let contractStub;
let contract;
let channelIdStub;
beforeEach(() => {
channelIdStub = sinon.stub(ChannelId_1.default, 'random').returns(RANDOM_ID);
deployed = {};
contractStub = sinon.stub(contracts.Unidirectional, 'contract');
contractStub.withArgs(web3.currentProvider).returns({
deployed: sinon.stub().resolves(Promise.resolve(deployed))
});
contract = new ChannelEthContract_1.default(web3, 0);
});
afterEach(() => {
contractStub.restore();
channelIdStub.restore();
});
describe('#open', () => {
specify('call contract', async () => {
deployed.open = sinon.stub();
await contract.open(SENDER, RECEIVER, VALUE, SETTLEMENT_PERIOD, ID);
expect(deployed.open.calledWith(ID, RECEIVER, SETTLEMENT_PERIOD, {
from: SENDER,
value: VALUE,
gas: 300000
})).toBeTruthy();
});
});
describe('#claim', () => {
it('eth: claims the channel', async () => {
deployed.claim = sinon.stub();
await contract.claim(RECEIVER, ID, VALUE, SIG);
expect(deployed.claim.calledWith(ID, VALUE, SIG.toString(), {
from: RECEIVER
})).toBeTruthy();
});
});
describe('#deposit', () => {
it('eth: deposits money into the channel', async () => {
deployed.deposit = sinon.stub();
await contract.deposit(SENDER, ID, VALUE);
expect(deployed.deposit.calledWith(ID, {
from: SENDER,
value: VALUE,
gas: 300000
})).toBeTruthy();
});
});
describe('#getState', () => {
it('eth: returns 0 if the channel is open', async () => {
contract.channelById = sinon.stub().returns(Promise.resolve([SENDER, RECEIVER, VALUE, SETTLEMENT_PERIOD, new bignumber_js_1.BigNumber(0)]));
let state = await contract.getState(ID);
expect(state).toBe(0);
});
it('eth: returns 1 if the channel is settling', async () => {
contract.channelById = sinon.stub().returns(Promise.resolve([SENDER, RECEIVER, VALUE, SETTLEMENT_PERIOD, new bignumber_js_1.BigNumber(10).plus(SETTLEMENT_PERIOD)]));
let state = await contract.getState(ID);
expect(state).toBe(1);
});
});
describe('#startSettle', () => {
it('eth: starts settling the channel', async () => {
deployed.startSettling = sinon.stub();
await contract.startSettle('acc', ID);
expect(deployed.startSettling.calledWith(ID, { from: 'acc' })).toBeTruthy();
});
});
describe('#finishSettle', () => {
it('eth: finishes settling the channel', async () => {
deployed.settle = sinon.stub();
await contract.finishSettle('acc', ID);
expect(deployed.settle.calledWith(ID, { from: 'acc', gas: 400000 })).toBeTruthy();
});
});
describe('#paymentDigest', () => {
it('eth: returns the digest', async () => {
deployed.paymentDigest = sinon.stub().withArgs(ID, VALUE).resolves('digest');
let digest = await contract.paymentDigest(ID, VALUE);
expect(digest).toBe('0x62a8023ff2b551d6334468a55f46c55452d606f13fc2e79b8ce429f6fa26025b');
});
});
});
//# sourceMappingURL=ChannelEthContract.test.js.map