MyCrypto/spec/libs/contract.spec.ts
HenryNguyen5 5d4b36d453 Migrate to Typescript (#224)
* Refactor babel/types

* Refactor entry point

* Refactor actions

* Refactor api

* Full project refactor -- Broad type fixing sweep

* - completely fix merge conflicts
- handle various type errors

* Add tslint to package.json

* Dependency cleanup

* Fix module resolution

* Work on type definitions for untyped libs

* progress commit

* Add more definition typing

* various type additions

* Add unit types

* Fix sagaiterator  + unit types

* various types added

* additional type additions

* Fix typing on Sagas

* remove flowfixmes; swap translate for translateRaw

* Get rid of contracts - awaiting Henry's contract PR

* Remove contracts from routing

* Fix most of actions/reducers

* refactor actions directory structure

* fix reducer action type imports

* Fix most of type errors pre-actions refactor

* fix action creator imports in containers

* Refactor more

* Refactor index of actions

* fix action imports; use module level index export

* package-lock.json updated

* Use action types in props

* Type up action creators

* Fix most of connect errors

* Typefixing progress

* More types

* Fix run-time errors

* Caching improvements for webpack

* Remove path resolve from webpack

* Update non-breaking packages to latest version

* Fix token typing

* Remove unused color code

* Fix wallet decrypt dispatch

* Set redux-form related props/functions to ANY, since we're stripping it out later on

* Revert BigNumber.js package changes

* Extend window to custom object for Perf

* Format Navigation

* Typecase keystore errors as any (since we shouldnt touch this)

* Push wallet context fix

* - find/replace value->payload in swap
- properly type swap state properties
- extract inline reducer into reducer function

* - type local storage retrieved items as generic

* - bind all RPCClient methods with fat arrow

* - reformat

* Change to enums for constants

* Change state into any

* Fix swap errors

* ensure that seconds are passed into state as integers

* Fix rest of errors

* use parseInt explicitly instead of type coercion

* Fix derivation-checker, remove flow command, add tslint command, add tslint-react, tell travis to use tslint instead of flow.

* Whoops, remove those tests.

* Remove unsupported (yet) config option.

* Fix precommit to target ts and tsx files.

* Fix some errors, ignore some silly rules.

* Revert jest to v19, use ts-jest and make all tests typescript. Fixes all but one.

* Get rid of saga tests

* Fix tslint errors
2017-09-24 19:06:28 -07:00

123 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Contract from 'libs/contract';
import Big from 'bignumber.js';
describe('Contract', () => {
// From the ABI docs
// https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#json
const testAbi = [
{
type: 'event',
inputs: [
{ name: 'a', type: 'uint256', indexed: true },
{ name: 'b', type: 'bytes32', indexed: false }
],
name: 'Event'
},
{
type: 'function',
inputs: [{ name: 'a', type: 'uint256' }],
name: 'foo',
outputs: []
}
];
const testContract = new Contract(testAbi);
// ----------------------------------------------------------------------
describe('constructor', () => {
it('should create an instance given a valid ABI', () => {
expect(testContract).toBeTruthy();
});
});
// ----------------------------------------------------------------------
describe('getMethodAbi', () => {
it('should return the a method, given the right name', () => {
const method = testContract.getMethodAbi('foo');
expect(method.name).toBe('foo');
expect(method.type).toBe('function');
expect(method.inputs.constructor).toBe(Array);
});
it('should throw if given an unknown method name', () => {
expect(() => {
testContract.getMethodAbi('gnjwakgnawk');
}).toThrow();
});
it('should throw if given a method isnt a function', () => {
expect(() => {
testContract.getMethodAbi('Event');
}).toThrow();
});
});
// ----------------------------------------------------------------------
describe('call / encodeArgs', () => {
it('should return hex data for the method', () => {
const result = testContract.call('foo', ['1337']);
expect(result).toBe(
'0x2fbebd380000000000000000000000000000000000000000000000000000000000000539'
);
});
it('should throw, if given too few method args', () => {
expect(() => {
testContract.call('foo', []);
}).toThrow();
});
it('should throw, if given too many method args', () => {
expect(() => {
testContract.call('foo', ['1', '2']);
}).toThrow();
});
it('should throw, if given invalid args', () => {
expect(() => {
testContract.call('foo', [{ some: 'object?' }]);
}).toThrow();
});
});
// ----------------------------------------------------------------------
describe('$call / decodeArgs', () => {
it('should decode some data', () => {
const decoded = testContract.$call(
'0x2fbebd380000000000000000000000000000000000000000000000000000000000000539'
);
expect(decoded.method.name).toBe('foo');
expect(decoded.args[0].toString(10)).toBe('1337');
});
it('should return identical data from a call return', () => {
const callMethod = 'foo';
const callArgs = ['42891048912084012480129'];
const callData = testContract.call(callMethod, callArgs);
const decoded = testContract.$call(callData);
expect(decoded.method.name).toBe(callMethod);
expect(decoded.args[0].toString(10)).toBe(callArgs[0]);
});
it('should throw, if given invalid data', () => {
expect(() => {
// ETH address
testContract.$call('0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8');
}).toThrow();
});
it('should throw, if given an unknown methods data', () => {
expect(() => {
// GNT token send data
testContract.$call(
'0xa9059cbb0000000000000000000000007cb57b5a97eabe94205c07890be4c1ad31e486a8000000000000000000000000000000000000000000000000130d2a539ba80000'
);
}).toThrow();
});
});
});