diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/exchange_wrapper_test.ts | 10 | ||||
-rw-r--r-- | test/schema_test.ts | 59 | ||||
-rw-r--r-- | test/token_wrapper_test.ts | 112 |
3 files changed, 175 insertions, 6 deletions
diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 3a88db5c9..0321eb569 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -721,7 +721,7 @@ describe('ExchangeWrapper', () => { await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); - })(); + })().catch(done); }); it('Should receive the LogCancel event when an order is cancelled', (done: DoneCallback) => { (async () => { @@ -735,7 +735,7 @@ describe('ExchangeWrapper', () => { done(); }); await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelTakerAmountInBaseUnits); - })(); + })().catch(done); }); it('Outstanding subscriptions are cancelled when zeroEx.setProviderAsync called', (done: DoneCallback) => { (async () => { @@ -761,7 +761,7 @@ describe('ExchangeWrapper', () => { await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); - })(); + })().catch(done); }); it('Should stop watch for events when stopWatchingAsync called on the eventEmitter', (done: DoneCallback) => { (async () => { @@ -776,7 +776,7 @@ describe('ExchangeWrapper', () => { signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); done(); - })(); + })().catch(done); }); it('Should wrap all event args BigNumber instances in a newer version of BigNumber', (done: DoneCallback) => { (async () => { @@ -794,7 +794,7 @@ describe('ExchangeWrapper', () => { await zeroEx.exchange.fillOrderAsync( signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, takerAddress, ); - })(); + })().catch(done); }); }); describe('#getOrderHashHexUsingContractCallAsync', () => { diff --git a/test/schema_test.ts b/test/schema_test.ts index b251a68f9..c170bebb1 100644 --- a/test/schema_test.ts +++ b/test/schema_test.ts @@ -6,12 +6,14 @@ import promisify = require('es6-promisify'); import {constants} from './utils/constants'; import {SchemaValidator} from '../src/utils/schema_validator'; import {tokenSchema} from '../src/schemas/token_schema'; +import {orderHashSchema} from '../src/schemas/order_hash_schema'; import {orderSchema, signedOrderSchema} from '../src/schemas/order_schemas'; import {addressSchema, numberSchema} from '../src/schemas/basic_type_schemas'; import {orderFillOrKillRequestsSchema} from '../src/schemas/order_fill_or_kill_requests_schema'; import {ecSignatureParameterSchema, ecSignatureSchema} from '../src/schemas/ec_signature_schema'; import {orderCancellationRequestsSchema} from '../src/schemas/order_cancel_schema'; import {orderFillRequestsSchema} from '../src/schemas/order_fill_requests_schema'; +import {blockParamSchema, subscriptionOptsSchema} from '../src/schemas/subscription_opts_schema'; chai.config.includeStack = true; const expect = chai.expect; @@ -96,6 +98,62 @@ describe('Schema', () => { validateAgainstSchema(testCases, ecSignatureSchema, shouldFail); }); }); + describe('#orderHashSchema', () => { + it('should validate valid order hash', () => { + const testCases = [ + '0x61a3ed31B43c8780e905a260a35faefEc527be7516aa11c0256729b5b351bc33', + '0x40349190569279751135161d22529dc25add4f6069af05be04cacbda2ace2254', + ]; + validateAgainstSchema(testCases, orderHashSchema); + }); + it('should fail for invalid order hash', () => { + const testCases = [ + {}, + '0x', + '0x8b0292B11a196601eD2ce54B665CaFEca0347D42', + '61a3ed31B43c8780e905a260a35faefEc527be7516aa11c0256729b5b351bc33', + ]; + const shouldFail = true; + validateAgainstSchema(testCases, orderHashSchema, shouldFail); + }); + }); + describe('#blockParamSchema', () => { + it('should validate valid block param', () => { + const testCases = [ + 42, + 'latest', + 'pending', + 'earliest', + ]; + validateAgainstSchema(testCases, blockParamSchema); + }); + it('should fail for invalid block param', () => { + const testCases = [ + {}, + '42', + 'pemding', + ]; + const shouldFail = true; + validateAgainstSchema(testCases, blockParamSchema, shouldFail); + }); + }); + describe('#subscriptionOptsSchema', () => { + it('should validate valid subscription opts', () => { + const testCases = [ + {fromBlock: 42, toBlock: 'latest'}, + {fromBlock: 42}, + {}, + ]; + validateAgainstSchema(testCases, subscriptionOptsSchema); + }); + it('should fail for invalid subscription opts', () => { + const testCases = [ + {fromBlock: '42'}, + ]; + const shouldFail = true; + validateAgainstSchema(testCases, subscriptionOptsSchema, shouldFail); + }); + }); describe('#tokenSchema', () => { const token = { name: 'Zero Ex', @@ -143,6 +201,7 @@ describe('Schema', () => { takerTokenAddress: constants.NULL_ADDRESS, salt: '256', feeRecipient: constants.NULL_ADDRESS, + exchangeContractAddress: constants.NULL_ADDRESS, expirationUnixTimestampSec: '42', }; describe('#orderSchema', () => { diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index a1c035672..06e373bfa 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -5,8 +5,18 @@ import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); import {web3Factory} from './utils/web3_factory'; -import {ZeroEx, ZeroExError, Token} from '../src'; +import { + ZeroEx, + ZeroExError, + Token, + SubscriptionOpts, + TokenEvents, + ContractEvent, + TransferContractEventArgs, + ApprovalContractEventArgs, +} from '../src'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; +import {DoneCallback} from '../src/types'; chaiSetup.configure(); const expect = chai.expect; @@ -231,4 +241,104 @@ describe('TokenWrapper', () => { return expect(allowanceAfterSet).to.be.bignumber.equal(expectedAllowanceAfterAllowanceSet); }); }); + describe('#subscribeAsync', () => { + const indexFilterValues = {}; + const shouldCheckTransfer = false; + let tokenAddress: string; + const subscriptionOpts: SubscriptionOpts = { + fromBlock: 0, + toBlock: 'latest', + }; + const transferAmount = new BigNumber(42); + const allowanceAmount = new BigNumber(42); + before(() => { + const token = tokens[0]; + tokenAddress = token.address; + }); + afterEach(async () => { + await zeroEx.token.stopWatchingAllEventsAsync(); + }); + // Hack: Mocha does not allow a test to be both async and have a `done` callback + // Since we need to await the receipt of the event in the `subscribeAsync` callback, + // we do need both. A hack is to make the top-level a sync fn w/ a done callback and then + // wrap the rest of the test in an async block + // Source: https://github.com/mochajs/mocha/issues/2407 + it('Should receive the Transfer event when an order is filled', (done: DoneCallback) => { + (async () => { + const zeroExEvent = await zeroEx.token.subscribeAsync( + tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues); + zeroExEvent.watch((err: Error, event: ContractEvent) => { + expect(err).to.be.null(); + expect(event).to.not.be.undefined(); + const args = event.args as TransferContractEventArgs; + expect(args._from).to.be.equal(coinbase); + expect(args._to).to.be.equal(addressWithoutFunds); + expect(args._value).to.be.bignumber.equal(transferAmount); + done(); + }); + await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount); + })().catch(done); + }); + it('Should receive the Approval event when an order is cancelled', (done: DoneCallback) => { + (async () => { + const zeroExEvent = await zeroEx.token.subscribeAsync( + tokenAddress, TokenEvents.Approval, subscriptionOpts, indexFilterValues); + zeroExEvent.watch((err: Error, event: ContractEvent) => { + expect(err).to.be.null(); + expect(event).to.not.be.undefined(); + const args = event.args as ApprovalContractEventArgs; + expect(args._owner).to.be.equal(coinbase); + expect(args._spender).to.be.equal(addressWithoutFunds); + expect(args._value).to.be.bignumber.equal(allowanceAmount); + done(); + }); + await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount); + })().catch(done); + }); + it('Outstanding subscriptions are cancelled when zeroEx.setProviderAsync called', (done: DoneCallback) => { + (async () => { + const eventSubscriptionToBeCancelled = await zeroEx.token.subscribeAsync( + tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues); + eventSubscriptionToBeCancelled.watch((err: Error, event: ContractEvent) => { + done(new Error('Expected this subscription to have been cancelled')); + }); + + const newProvider = web3Factory.getRpcProvider(); + await zeroEx.setProviderAsync(newProvider); + + const eventSubscriptionToStay = await zeroEx.token.subscribeAsync( + tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues); + eventSubscriptionToStay.watch((err: Error, event: ContractEvent) => { + expect(err).to.be.null(); + expect(event).to.not.be.undefined(); + done(); + }); + await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount); + })().catch(done); + }); + it('Should stop watch for events when stopWatchingAsync called on the eventEmitter', (done: DoneCallback) => { + (async () => { + const eventSubscriptionToBeStopped = await zeroEx.token.subscribeAsync( + tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues); + eventSubscriptionToBeStopped.watch((err: Error, event: ContractEvent) => { + done(new Error('Expected this subscription to have been stopped')); + }); + await eventSubscriptionToBeStopped.stopWatchingAsync(); + await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount); + done(); + })().catch(done); + }); + it('Should wrap all event args BigNumber instances in a newer version of BigNumber', (done: DoneCallback) => { + (async () => { + const zeroExEvent = await zeroEx.token.subscribeAsync( + tokenAddress, TokenEvents.Transfer, subscriptionOpts, indexFilterValues); + zeroExEvent.watch((err: Error, event: ContractEvent) => { + const args = event.args as TransferContractEventArgs; + expect(args._value.isBigNumber).to.be.true(); + done(); + }); + await zeroEx.token.transferAsync(tokenAddress, coinbase, addressWithoutFunds, transferAmount); + })().catch(done); + }); + }); }); |