diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/0x.js_test.ts | 9 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 16 | ||||
-rw-r--r-- | test/schema_test.ts | 46 | ||||
-rw-r--r-- | test/token_registry_wrapper_test.ts | 7 | ||||
-rw-r--r-- | test/token_wrapper_test.ts | 14 | ||||
-rw-r--r-- | test/utils/chai_setup.ts | 13 |
6 files changed, 84 insertions, 21 deletions
diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index 43c50ef34..5096d5df2 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -1,18 +1,15 @@ import * as _ from 'lodash'; import * as chai from 'chai'; +import {chaiSetup} from './utils/chai_setup'; import 'mocha'; import * as BigNumber from 'bignumber.js'; -import ChaiBigNumber = require('chai-bignumber'); -import * as dirtyChai from 'dirty-chai'; import * as Sinon from 'sinon'; import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; -import {Order, DoneCallback} from '../src/types'; +import {Order} from '../src/types'; -chai.config.includeStack = true; -chai.use(ChaiBigNumber()); -chai.use(dirtyChai); +chaiSetup.configure(); const expect = chai.expect; describe('ZeroEx library', () => { diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index a841f82ec..e4b0d47f7 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -1,10 +1,10 @@ import 'mocha'; -import * as _ from 'lodash'; import * as chai from 'chai'; import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; -import * as dirtyChai from 'dirty-chai'; +import {chaiSetup} from './utils/chai_setup'; import ChaiBigNumber = require('chai-bignumber'); +import * as chaiAsPromised from 'chai-as-promised'; import promisify = require('es6-promisify'); import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; @@ -21,9 +21,7 @@ import { import {FillScenarios} from './utils/fill_scenarios'; import {TokenUtils} from './utils/token_utils'; -chai.config.includeStack = true; -chai.use(dirtyChai); -chai.use(ChaiBigNumber()); +chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(); @@ -347,7 +345,7 @@ describe('ExchangeWrapper', () => { describe('#getUnavailableTakerAmountAsync', () => { it ('should throw if passed an invalid orderHash', async () => { const invalidOrderHashHex = '0x123'; - expect(zeroEx.exchange.getUnavailableTakerAmountAsync(invalidOrderHashHex)).to.be.rejected(); + return expect(zeroEx.exchange.getUnavailableTakerAmountAsync(invalidOrderHashHex)).to.be.rejected(); }); it ('should return zero if passed a valid but non-existent orderHash', async () => { const unavailableValueT = await zeroEx.exchange.getUnavailableTakerAmountAsync(NON_EXISTENT_ORDER_HASH); @@ -362,7 +360,7 @@ describe('ExchangeWrapper', () => { describe('#getFilledTakerAmountAsync', () => { it ('should throw if passed an invalid orderHash', async () => { const invalidOrderHashHex = '0x123'; - expect(zeroEx.exchange.getFilledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected(); + return expect(zeroEx.exchange.getFilledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected(); }); it ('should return zero if passed a valid but non-existent orderHash', async () => { const filledValueT = await zeroEx.exchange.getFilledTakerAmountAsync(NON_EXISTENT_ORDER_HASH); @@ -377,7 +375,7 @@ describe('ExchangeWrapper', () => { describe('#getCanceledTakerAmountAsync', () => { it ('should throw if passed an invalid orderHash', async () => { const invalidOrderHashHex = '0x123'; - expect(zeroEx.exchange.getCanceledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected(); + return expect(zeroEx.exchange.getCanceledTakerAmountAsync(invalidOrderHashHex)).to.be.rejected(); }); it ('should return zero if passed a valid but non-existent orderHash', async () => { const cancelledValueT = await zeroEx.exchange.getCanceledTakerAmountAsync(NON_EXISTENT_ORDER_HASH); @@ -413,7 +411,7 @@ describe('ExchangeWrapper', () => { ); }); afterEach(async () => { - (zeroEx.exchange as any).stopWatchingExchangeLogEventsAsync(); + await (zeroEx.exchange as any).stopWatchingExchangeLogEventsAsync(); }); // 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, diff --git a/test/schema_test.ts b/test/schema_test.ts new file mode 100644 index 000000000..133914e6d --- /dev/null +++ b/test/schema_test.ts @@ -0,0 +1,46 @@ +import 'mocha'; +import * as _ from 'lodash'; +import * as chai from 'chai'; +import * as BigNumber from 'bignumber.js'; +import promisify = require('es6-promisify'); +import {numberSchema} from '../src/schemas/order_schemas'; +import {SchemaValidator} from '../src/utils/schema_validator'; + +chai.config.includeStack = true; +const expect = chai.expect; + +describe('Schema', () => { + const validator = new SchemaValidator(); + describe('#numberSchema', () => { + describe('number regex', () => { + it('should validate valid numbers', () => { + const testCases = ['42', '0', '1.3', '0.2', '00.00']; + _.forEach(testCases, (testCase: string) => { + expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(0); + }); + }); + it('should fail for invalid numbers', () => { + const testCases = ['.3', '1.', 'abacaba', 'и', '1..0']; + _.forEach(testCases, (testCase: string) => { + expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(1); + }); + }); + }); + }); + describe('BigNumber serialization', () => { + it.only('should correctly serialize BigNumbers', () => { + const testCases = { + '42': '42', + '0': '0', + '1.3': '1.3', + '0.2': '0.2', + '00.00': '0', + '.3': '0.3', + }; + _.forEach(testCases, (serialized: string, input: string) => { + expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber(input))) + .to.be.equal(serialized); + }); + }); + }); +}); diff --git a/test/token_registry_wrapper_test.ts b/test/token_registry_wrapper_test.ts index 195b2f205..33c4a8054 100644 --- a/test/token_registry_wrapper_test.ts +++ b/test/token_registry_wrapper_test.ts @@ -1,18 +1,15 @@ import * as _ from 'lodash'; import 'mocha'; import * as chai from 'chai'; -import chaiAsPromised = require('chai-as-promised'); -import * as Web3 from 'web3'; +import {chaiSetup} from './utils/chai_setup'; import {web3Factory} from './utils/web3_factory'; import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -import {Token} from '../src/types'; import {SchemaValidator} from '../src/utils/schema_validator'; import {tokenSchema} from '../src/schemas/token_schema'; -chai.config.includeStack = true; +chaiSetup.configure(); const expect = chai.expect; -chai.use(chaiAsPromised); const blockchainLifecycle = new BlockchainLifecycle(); const TOKEN_REGISTRY_SIZE_AFTER_MIGRATION = 7; diff --git a/test/token_wrapper_test.ts b/test/token_wrapper_test.ts index 698ad5b6a..243d918ed 100644 --- a/test/token_wrapper_test.ts +++ b/test/token_wrapper_test.ts @@ -1,5 +1,6 @@ import 'mocha'; import * as chai from 'chai'; +import {chaiSetup} from './utils/chai_setup'; import * as Web3 from 'web3'; import * as BigNumber from 'bignumber.js'; import promisify = require('es6-promisify'); @@ -8,7 +9,7 @@ import {ZeroEx} from '../src/0x.js'; import {ZeroExError, Token} from '../src/types'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; -chai.config.includeStack = true; +chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(); @@ -89,6 +90,17 @@ describe('TokenWrapper', () => { token.address, fromAddress, toAddress, senderAddress, transferAmount, )).to.be.rejectedWith(ZeroExError.INSUFFICIENT_ALLOWANCE_FOR_TRANSFER); }); + it('[regression] should fail to transfer tokens if set allowance for toAddress instead of senderAddress', + async () => { + const fromAddress = coinbase; + const transferAmount = new BigNumber(42); + + await zeroEx.token.setAllowanceAsync(token.address, fromAddress, toAddress, transferAmount); + + return expect(zeroEx.token.transferFromAsync( + token.address, fromAddress, toAddress, senderAddress, transferAmount, + )).to.be.rejectedWith(ZeroExError.INSUFFICIENT_ALLOWANCE_FOR_TRANSFER); + }); it('should fail to transfer tokens if fromAddress has insufficient balance', async () => { const fromAddress = addressWithoutFunds; const transferAmount = new BigNumber(42); diff --git a/test/utils/chai_setup.ts b/test/utils/chai_setup.ts new file mode 100644 index 000000000..c18988106 --- /dev/null +++ b/test/utils/chai_setup.ts @@ -0,0 +1,13 @@ +import * as chai from 'chai'; +import * as dirtyChai from 'dirty-chai'; +import ChaiBigNumber = require('chai-bignumber'); +import chaiAsPromised = require('chai-as-promised'); + +export const chaiSetup = { + configure() { + chai.config.includeStack = true; + chai.use(ChaiBigNumber()); + chai.use(dirtyChai); + chai.use(chaiAsPromised); + }, +}; |