Skip to content

Commit

Permalink
Add tests for some utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
perfectmak committed Apr 25, 2018
1 parent de46d40 commit 9711ffa
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions test/util/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import Web3 from 'web3';
import FakeProvider from 'web3-fake-provider';

import { getMetamaskError } from '../../src/util/utils';
import { getMetamaskError, signMessage, calculateCollateral } from '../../src/util/utils';

describe('getMetamaskError', () => {

Expand All @@ -21,5 +23,44 @@ describe('getMetamaskError', () => {

expect(actualErrorMessage).to.equals(expectedErrorMessage);
});
});

})
describe('signMessage', () => {
function getStubedWeb3(rawSignature) {
const fakeProvider = new FakeProvider();
const web3 = new Web3(fakeProvider);
fakeProvider.injectResult(rawSignature);
return web3;
}

it('should correctly extract [v, r, s] from signature', () => {
const rawSignature = '0x9955af11969a2d2a7f860cb00e6a00cfa7c581f5df2dbe8ea16700b33f4b4b9b69f945012f7ea7d3febf11eb1b78e1adc2d1c14c2cf48b25000938cc1860c83e01';
const web3 = getStubedWeb3(rawSignature);
const v = 28;
const r = '0x9955af11969a2d2a7f860cb00e6a00cfa7c581f5df2dbe8ea16700b33f4b4b9b';
const s = '0x69f945012f7ea7d3febf11eb1b78e1adc2d1c14c2cf48b25000938cc1860c83e';

const [ actualV, actualR, actualS] = signMessage(web3, '0xf204a4ef082f5c04bb89f7d5e6568b796096735a', 'This is my message :)');

expect(v, 'v not extracted correctly').to.equal(actualV);
expect(r, 'r not extracted correctly').to.equal(actualR);
expect(s, 's not extracted correctly').to.equal(actualS);
});
});

describe('calculateCollateral', () => {
[
/* [priceFloor, priceCap, qtyMultiplier, qty, price, expectedCollateral] */
[20, 50, 2, 3, 10, 0],
[20, 50, 2, 3, 25, 30],
[20, 50, 2, -3, 60, 0],
[20, 50, 2, -3, 25, 150]
].forEach((testCase) => {
it(`expects calculateCollateral(${testCase.slice(0, 5)}) to equal ${testCase[5]}`, () => {
const actualCollateral = calculateCollateral(...testCase);
const expectedCollateral = testCase[5];

expect(expectedCollateral).to.equal(actualCollateral);
});
});
});

0 comments on commit 9711ffa

Please sign in to comment.