aboutsummaryrefslogtreecommitdiffstats
path: root/test/subscription_test.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-13 09:49:48 +0800
committerFabio Berger <me@fabioberger.com>2017-11-13 09:49:48 +0800
commit442f35a1fdd98846d3985548b3de6f5c620e68a1 (patch)
treee498559ce452a0d1eddfc4df6224450492efa892 /test/subscription_test.ts
parent6becf22a2f752ef7c34ce1b423efd51773cc5fde (diff)
parent1392a855bb17981f7680548a23062842fb6dc4e0 (diff)
downloaddexon-0x-contracts-442f35a1fdd98846d3985548b3de6f5c620e68a1.tar.gz
dexon-0x-contracts-442f35a1fdd98846d3985548b3de6f5c620e68a1.tar.zst
dexon-0x-contracts-442f35a1fdd98846d3985548b3de6f5c620e68a1.zip
Merge branch 'development' into orderWatcher
* development: 0.23.0 Update CHANGELOG Fix amounts in tests one last time. Now that we updated the testRPC snapshot, this should no longer be mismatched between CI and locally Update testRPC snapshot used by CircleCi Push unsubscribe to the base class rather than super Check for null rather than undefined Removed nits Test case was error then unsubscribe Clean up subscription state. Fix unhandled promise rejection error on subscriptions # Conflicts: # src/types.ts # test/exchange_wrapper_test.ts # test/token_wrapper_test.ts
Diffstat (limited to 'test/subscription_test.ts')
-rw-r--r--test/subscription_test.ts95
1 files changed, 95 insertions, 0 deletions
diff --git a/test/subscription_test.ts b/test/subscription_test.ts
new file mode 100644
index 000000000..985fdc1d6
--- /dev/null
+++ b/test/subscription_test.ts
@@ -0,0 +1,95 @@
+import 'mocha';
+import * as _ from 'lodash';
+import * as chai from 'chai';
+import * as Sinon from 'sinon';
+import {chaiSetup} from './utils/chai_setup';
+import * as Web3 from 'web3';
+import BigNumber from 'bignumber.js';
+import promisify = require('es6-promisify');
+import {web3Factory} from './utils/web3_factory';
+import {
+ ZeroEx,
+ ZeroExError,
+ Token,
+ ApprovalContractEventArgs,
+ TokenEvents,
+ LogEvent,
+} from '../src';
+import {BlockchainLifecycle} from './utils/blockchain_lifecycle';
+import {TokenUtils} from './utils/token_utils';
+import {DoneCallback, BlockParamLiteral} from '../src/types';
+
+chaiSetup.configure();
+const expect = chai.expect;
+const blockchainLifecycle = new BlockchainLifecycle();
+
+describe('SubscriptionTest', () => {
+ let web3: Web3;
+ let zeroEx: ZeroEx;
+ let userAddresses: string[];
+ let tokens: Token[];
+ let tokenUtils: TokenUtils;
+ let coinbase: string;
+ let addressWithoutFunds: string;
+ before(async () => {
+ web3 = web3Factory.create();
+ zeroEx = new ZeroEx(web3.currentProvider);
+ userAddresses = await zeroEx.getAvailableAddressesAsync();
+ tokens = await zeroEx.tokenRegistry.getTokensAsync();
+ tokenUtils = new TokenUtils(tokens);
+ coinbase = userAddresses[0];
+ addressWithoutFunds = userAddresses[1];
+ });
+ beforeEach(async () => {
+ await blockchainLifecycle.startAsync();
+ });
+ afterEach(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ describe('#subscribe', () => {
+ const indexFilterValues = {};
+ const shouldThrowOnInsufficientBalanceOrAllowance = true;
+ let tokenAddress: string;
+ const transferAmount = new BigNumber(42);
+ const allowanceAmount = new BigNumber(42);
+ let stubs: Sinon.SinonStub[] = [];
+ before(() => {
+ const token = tokens[0];
+ tokenAddress = token.address;
+ });
+ afterEach(() => {
+ zeroEx.token.unsubscribeAll();
+ _.each(stubs, s => s.restore());
+ stubs = [];
+ });
+ it('Should receive the Error when an error occurs', (done: DoneCallback) => {
+ (async () => {
+ const callback = (err: Error, logEvent: LogEvent<ApprovalContractEventArgs>) => {
+ expect(err).to.not.be.null();
+ expect(logEvent).to.be.undefined();
+ done();
+ };
+ stubs = [
+ Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync')
+ .throws("JSON RPC error")
+ ]
+ zeroEx.token.subscribe(
+ tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
+ await zeroEx.token.setAllowanceAsync(tokenAddress, coinbase, addressWithoutFunds, allowanceAmount);
+ })().catch(done);
+ });
+ it('Should allow unsubscribeAll to be called successfully after an error', (done: DoneCallback) => {
+ (async () => {
+ const callback = (err: Error, logEvent: LogEvent<ApprovalContractEventArgs>) => { };
+ zeroEx.token.subscribe(
+ tokenAddress, TokenEvents.Approval, indexFilterValues, callback);
+ stubs = [
+ Sinon.stub((zeroEx as any)._web3Wrapper, 'getBlockAsync')
+ .throws("JSON RPC error")
+ ]
+ zeroEx.token.unsubscribeAll();
+ done();
+ })().catch(done);
+ });
+ })
+ }) \ No newline at end of file