From 209266dbed9d7d038c90c2da8d9b99acab77c80c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 9 May 2018 20:36:28 +0200 Subject: Split 0x.js into contract-wrappers, order-watcher but keep 0x.js as a unifying library with the same interface --- .../test/token_registry_wrapper_test.ts | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 packages/contract-wrappers/test/token_registry_wrapper_test.ts (limited to 'packages/contract-wrappers/test/token_registry_wrapper_test.ts') diff --git a/packages/contract-wrappers/test/token_registry_wrapper_test.ts b/packages/contract-wrappers/test/token_registry_wrapper_test.ts new file mode 100644 index 000000000..1b8ce4d74 --- /dev/null +++ b/packages/contract-wrappers/test/token_registry_wrapper_test.ts @@ -0,0 +1,134 @@ +import { BlockchainLifecycle, devConstants } from '@0xproject/dev-utils'; +import { schemas, SchemaValidator } from '@0xproject/json-schemas'; +import * as chai from 'chai'; +import * as _ from 'lodash'; +import 'mocha'; + +import { ContractWrappers, Token } from '../src'; + +import { chaiSetup } from './utils/chai_setup'; +import { constants } from './utils/constants'; +import { provider, web3Wrapper } from './utils/web3_wrapper'; + +chaiSetup.configure(); +const expect = chai.expect; +const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); + +const TOKEN_REGISTRY_SIZE_AFTER_MIGRATION = 7; + +describe('TokenRegistryWrapper', () => { + let contractWrappers: ContractWrappers; + let tokens: Token[]; + const tokenAddressBySymbol: { [symbol: string]: string } = {}; + const tokenAddressByName: { [symbol: string]: string } = {}; + const tokenBySymbol: { [symbol: string]: Token } = {}; + const tokenByName: { [symbol: string]: Token } = {}; + const registeredSymbol = 'ZRX'; + const registeredName = '0x Protocol Token'; + const unregisteredSymbol = 'MAL'; + const unregisteredName = 'Malicious Token'; + const config = { + networkId: constants.TESTRPC_NETWORK_ID, + }; + before(async () => { + contractWrappers = new ContractWrappers(provider, config); + tokens = await contractWrappers.tokenRegistry.getTokensAsync(); + _.map(tokens, token => { + tokenAddressBySymbol[token.symbol] = token.address; + tokenAddressByName[token.name] = token.address; + tokenBySymbol[token.symbol] = token; + tokenByName[token.name] = token; + }); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + describe('#getTokensAsync', () => { + it('should return all the tokens added to the tokenRegistry during the migration', async () => { + expect(tokens).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION); + + const schemaValidator = new SchemaValidator(); + _.each(tokens, token => { + const validationResult = schemaValidator.validate(token, schemas.tokenSchema); + expect(validationResult.errors).to.have.lengthOf(0); + }); + }); + }); + describe('#getTokenAddressesAsync', () => { + it('should return all the token addresses added to the tokenRegistry during the migration', async () => { + const tokenAddresses = await contractWrappers.tokenRegistry.getTokenAddressesAsync(); + expect(tokenAddresses).to.have.lengthOf(TOKEN_REGISTRY_SIZE_AFTER_MIGRATION); + + const schemaValidator = new SchemaValidator(); + _.each(tokenAddresses, tokenAddress => { + const validationResult = schemaValidator.validate(tokenAddress, schemas.addressSchema); + expect(validationResult.errors).to.have.lengthOf(0); + expect(tokenAddress).to.not.be.equal(constants.NULL_ADDRESS); + }); + }); + }); + describe('#getTokenAddressBySymbol', () => { + it('should return correct address for a token in the registry', async () => { + const tokenAddress = await contractWrappers.tokenRegistry.getTokenAddressBySymbolIfExistsAsync( + registeredSymbol, + ); + expect(tokenAddress).to.be.equal(tokenAddressBySymbol[registeredSymbol]); + }); + it('should return undefined for a token out of registry', async () => { + const tokenAddress = await contractWrappers.tokenRegistry.getTokenAddressBySymbolIfExistsAsync( + unregisteredSymbol, + ); + expect(tokenAddress).to.be.undefined(); + }); + }); + describe('#getTokenAddressByName', () => { + it('should return correct address for a token in the registry', async () => { + const tokenAddress = await contractWrappers.tokenRegistry.getTokenAddressByNameIfExistsAsync(registeredName); + expect(tokenAddress).to.be.equal(tokenAddressByName[registeredName]); + }); + it('should return undefined for a token out of registry', async () => { + const tokenAddress = await contractWrappers.tokenRegistry.getTokenAddressByNameIfExistsAsync( + unregisteredName, + ); + expect(tokenAddress).to.be.undefined(); + }); + }); + describe('#getTokenBySymbol', () => { + it('should return correct token for a token in the registry', async () => { + const token = await contractWrappers.tokenRegistry.getTokenBySymbolIfExistsAsync(registeredSymbol); + expect(token).to.be.deep.equal(tokenBySymbol[registeredSymbol]); + }); + it('should return undefined for a token out of registry', async () => { + const token = await contractWrappers.tokenRegistry.getTokenBySymbolIfExistsAsync(unregisteredSymbol); + expect(token).to.be.undefined(); + }); + }); + describe('#getTokenByName', () => { + it('should return correct token for a token in the registry', async () => { + const token = await contractWrappers.tokenRegistry.getTokenByNameIfExistsAsync(registeredName); + expect(token).to.be.deep.equal(tokenByName[registeredName]); + }); + it('should return undefined for a token out of registry', async () => { + const token = await contractWrappers.tokenRegistry.getTokenByNameIfExistsAsync(unregisteredName); + expect(token).to.be.undefined(); + }); + }); + describe('#getTokenIfExistsAsync', () => { + it('should return the token added to the tokenRegistry during the migration', async () => { + const aToken = tokens[0]; + + const token = await contractWrappers.tokenRegistry.getTokenIfExistsAsync(aToken.address); + const schemaValidator = new SchemaValidator(); + const validationResult = schemaValidator.validate(token, schemas.tokenSchema); + expect(validationResult.errors).to.have.lengthOf(0); + }); + it('should return return undefined when passed a token address not in the tokenRegistry', async () => { + const unregisteredTokenAddress = '0x5409ed021d9299bf6814279a6a1411a7e866a631'; + const tokenIfExists = await contractWrappers.tokenRegistry.getTokenIfExistsAsync(unregisteredTokenAddress); + expect(tokenIfExists).to.be.undefined(); + }); + }); +}); -- cgit v1.2.3 From 294a65f84224e85b7dbeee7eace02578906e49dd Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 11 May 2018 13:11:50 +0200 Subject: Fix prettier --- packages/contract-wrappers/test/token_registry_wrapper_test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'packages/contract-wrappers/test/token_registry_wrapper_test.ts') diff --git a/packages/contract-wrappers/test/token_registry_wrapper_test.ts b/packages/contract-wrappers/test/token_registry_wrapper_test.ts index 1b8ce4d74..a21efed21 100644 --- a/packages/contract-wrappers/test/token_registry_wrapper_test.ts +++ b/packages/contract-wrappers/test/token_registry_wrapper_test.ts @@ -86,7 +86,9 @@ describe('TokenRegistryWrapper', () => { }); describe('#getTokenAddressByName', () => { it('should return correct address for a token in the registry', async () => { - const tokenAddress = await contractWrappers.tokenRegistry.getTokenAddressByNameIfExistsAsync(registeredName); + const tokenAddress = await contractWrappers.tokenRegistry.getTokenAddressByNameIfExistsAsync( + registeredName, + ); expect(tokenAddress).to.be.equal(tokenAddressByName[registeredName]); }); it('should return undefined for a token out of registry', async () => { -- cgit v1.2.3