From 9711ffa24ef0a7ef48d1a58ce7d293b6b9055b1f Mon Sep 17 00:00:00 2001 From: Perfect Makanju Date: Mon, 23 Apr 2018 01:57:15 +0100 Subject: [PATCH] Add tests for some utility functions --- test/util/utils.test.js | 45 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/test/util/utils.test.js b/test/util/utils.test.js index f917914a..51f8ad64 100644 --- a/test/util/utils.test.js +++ b/test/util/utils.test.js @@ -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', () => { @@ -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); + }); + }); +});