Skip to content

Commit

Permalink
fix(tests): restructure mockApi tests to integrate with historical api (
Browse files Browse the repository at this point in the history
#702)

* fix: refactor api.derive.chain in /blocks

* add historicApi

* remove bench test

* tests: adjust tests to accept historicalApi

* refactor blocks to use historicApi

* fix: fetchevents bug

* fix: update historic kusama blocks e2e tests

* put back --silent

* fix: lint

* fix: tests for pallets staking progress

* cleanup

* fix: restructure block tests and mockApi

* fix: change mockApi to defaultMockApi for BlocksTraceService

* fix: add AccountsAssetsService.spec.ts refactoring to mockApi

* lint and cleanup

* refactor AccountsBalanceInfoService.spec.ts

* refactor NodeNetworkService.spec.ts

* refactor NodeTransactionPoolService.spec.ts

* refactor NodeVersionService.spec.ts

* restructure AccountsStakingInfoService.spec.ts

* refactor AccountsVestingInfoService.spec.ts

* refactor PalletsAssetsService.spec.ts

* refactor PalletsStakingProgressService.spec.ts

* refactor PalletsStorageService.spec.ts

* cleanup

* refactor ParasService.spec.ts

* refactor RuntimeCodeService.spec.ts

* refactor RuntimeMetadataService.spec.ts

* refactor RuntimeSpecService.spec.ts

* refactor TransactionFeeEstimateService.spec.ts

* refactor TransactionMaterialService.spec.ts

* refactor TransactionSubmitService.spec.ts

* refactor mockApi to be defaultMockApi

* lint

* fix merge conflict with nextFeeMultiplier

* historical -> historic
  • Loading branch information
TarikGul committed Oct 12, 2021
1 parent b9404ff commit 2bf71ad
Show file tree
Hide file tree
Showing 20 changed files with 1,009 additions and 688 deletions.
154 changes: 153 additions & 1 deletion src/services/accounts/AccountsAssetsService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,159 @@
import { ApiPromise } from '@polkadot/api';
import { AssetId } from '@polkadot/types/interfaces';
import { PalletAssetsAssetBalance } from '@polkadot/types/lookup';

import { sanitizeNumbers } from '../../sanitize/sanitizeNumbers';
import { blockHash789629, mockApi } from '../test-helpers/mock';
import { statemintV1 } from '../../test-helpers/metadata/statemintMetadata';
import { rococoRegistry } from '../../test-helpers/registries';
import { createApiWithAugmentations } from '../../test-helpers/typeFactory';
import { TypeFactory } from '../../test-helpers/typeFactory';
import { blockHash789629, defaultMockApi } from '../test-helpers/mock';
import { AccountsAssetsService } from './AccountsAssetsService';

const statemintApiV1 = createApiWithAugmentations(statemintV1);
const statemintTypeFactory = new TypeFactory(statemintApiV1);

const falseBool = rococoRegistry.createType('bool', false);
const trueBool = rococoRegistry.createType('bool', true);
const assetTBalanceOne = rococoRegistry.createType('u64', 10000000);
const assetTBalanceTwo = rococoRegistry.createType('u64', 20000000);

const accountIdOne = rococoRegistry.createType(
'AccountId',
'1TYrFCWxwHA5bhiXf6uLvPfG6eEvrzzL7uiPK3Yc6yHLUqc'
);
const accountIdTwo = rococoRegistry.createType(
'AccountId',
'13NXiLYYzVEjXxU3eaZNcrjEX9vPyVDNNpURCzK8Bj9BiCWH'
);
const balanceOfTwo = rococoRegistry.createType('BalanceOf', 2000000);

const assetsInfo = () =>
Promise.resolve().then(() => {
const responseObj = {
owner: accountIdOne,
issue: accountIdTwo,
admin: accountIdTwo,
freezer: accountIdTwo,
supply: assetTBalanceOne,
deposit: balanceOfTwo,
minBalance: rococoRegistry.createType('u64', 10000),
isSufficient: trueBool,
accounts: rococoRegistry.createType('u32', 10),
sufficients: rococoRegistry.createType('u32', 15),
approvals: rococoRegistry.createType('u32', 20),
isFrozen: falseBool,
};

return rococoRegistry.createType('AssetDetails', responseObj);
});

const assetsMetadata = () =>
Promise.resolve().then(() => {
const responseObj = {
deposit: balanceOfTwo,
name: rococoRegistry.createType('Bytes', 'statemint'),
symbol: rococoRegistry.createType('Bytes', 'DOT'),
decimals: rococoRegistry.createType('u8', 10),
isFrozen: falseBool,
};

return rococoRegistry.createType('AssetMetadata', responseObj);
});

const assetBalanceObjOne = {
balance: assetTBalanceOne,
isFrozen: falseBool,
sufficient: trueBool,
};

const assetBalanceObjTwo = {
balance: assetTBalanceTwo,
isFrozen: trueBool,
sufficient: trueBool,
};

const assetBalanceObjThree = {
balance: assetTBalanceTwo,
isFrozen: falseBool,
sufficient: falseBool,
};

const assetBalanceFactory = {
'10': assetBalanceObjOne as PalletAssetsAssetBalance,
'20': assetBalanceObjTwo as PalletAssetsAssetBalance,
'30': assetBalanceObjThree as PalletAssetsAssetBalance,
};

const assetStorageKeyOne = statemintTypeFactory.storageKey(
10,
'AssetId',
statemintApiV1.query.assets.asset
);

const assetStorageKeyTwo = statemintTypeFactory.storageKey(
20,
'AssetId',
statemintApiV1.query.assets.asset
);

const assetStorageKeyThree = statemintTypeFactory.storageKey(
30,
'AssetId',
statemintApiV1.query.assets.asset
);

const assetsAccountKeysAt = () =>
Promise.resolve().then(() => {
return [assetStorageKeyOne, assetStorageKeyTwo, assetStorageKeyThree];
});

/**
* Attach `keysAt` to mockApi.query.assets.asset
*/
Object.assign(assetsInfo, {
keysAt: assetsAccountKeysAt,
});

/**
* @param assetId options are 10, 20, 30
*/
const assetsAccount = (assetId: number | AssetId, _address: string) => {
const id = typeof assetId === 'number' ? assetId : assetId.toNumber();

switch (id) {
case 10:
return assetBalanceFactory[10];
case 20:
return assetBalanceFactory[20];
case 30:
return assetBalanceFactory[30];
default:
return;
}
};

const assetApprovals = () =>
Promise.resolve().then(() => {
const assetObj = {
amount: assetTBalanceOne,
deposit: balanceOfTwo,
};
return rococoRegistry.createType('Option<AssetApproval>', assetObj);
});

const mockApi = {
...defaultMockApi,
query: {
assets: {
account: assetsAccount,
approvals: assetApprovals,
asset: assetsInfo,
metadata: assetsMetadata,
},
},
} as unknown as ApiPromise;

const accountsAssetsService = new AccountsAssetsService(mockApi);

describe('AccountsAssetsService', () => {
Expand Down
Loading

0 comments on commit 2bf71ad

Please sign in to comment.