Skip to content

Commit

Permalink
test: jestify get-balance test
Browse files Browse the repository at this point in the history
Migrated test from Tap to Jest.

File Path:
packages/cactus-plugin-ledger-connector-besu/src/test/
typescript/integration/plugin-ledger-connector-besu/
deploy-contract/get-balance.test.ts

This is a PARTIAL resolution to issue hyperledger-cacti#238

Signed-off-by: awadhana <awadhana0825@gmail.com>
  • Loading branch information
awadhana authored and petermetz committed Dec 20, 2021
1 parent 825b4f0 commit c186c65
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
1 change: 0 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ files:
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-balance.test.ts
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-deploy-contract-from-json.test.ts
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-record-locator.test.ts
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/get-balance.test.ts
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/invoke-contract.test.ts
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/private-deploy-contract-from-json-web3-eea.test.ts
- ./packages/cactus-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-ledger-connector-besu/deploy-contract/v21-get-past-logs.test.ts
Expand Down
1 change: 0 additions & 1 deletion jest.config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test, { Test } from "tape";
import { v4 as uuidv4 } from "uuid";
import "jest-extended";
import { Account } from "web3-core";
import { PluginRegistry } from "@hyperledger/cactus-core";
import {
PluginLedgerConnectorBesu,
Expand All @@ -13,59 +14,70 @@ import HelloWorldContractJson from "../../../../solidity/hello-world-contract/He
import Web3 from "web3";
import { PluginImportType } from "@hyperledger/cactus-core-api";

test("can get balance of an account", async (t: Test) => {
const testcase = "can get balance of an account";
describe(testcase, () => {
const logLevel: LogLevelDesc = "TRACE";
const besuTestLedger = new BesuTestLedger();
await besuTestLedger.start();

test.onFinish(async () => {
let rpcApiHttpHost: string,
rpcApiWsHost: string,
web3: Web3,
keychainPlugin: PluginKeychainMemory,
firstHighNetWorthAccount: string,
testEthAccount: Account,
keychainEntryKey: string,
keychainEntryValue: string;

afterAll(async () => {
await besuTestLedger.stop();
await besuTestLedger.destroy();
});
beforeAll(async () => {
await besuTestLedger.start();
web3 = new Web3(rpcApiHttpHost);
firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey();
testEthAccount = web3.eth.accounts.create(uuidv4());

const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost();
const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost();

keychainEntryKey = uuidv4();
keychainEntryValue = testEthAccount.privateKey;
keychainPlugin = new PluginKeychainMemory({
instanceId: uuidv4(),
keychainId: uuidv4(),
// pre-provision keychain with mock backend holding the private key of the
// test account that we'll reference while sending requests with the
// signing credential pointing to this keychain entry.
backend: new Map([[keychainEntryKey, keychainEntryValue]]),
logLevel,
});
rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost();
rpcApiWsHost = await besuTestLedger.getRpcApiWsHost();
});
/**
* Constant defining the standard 'dev' Besu genesis.json contents.
*
* @see https://github.com/hyperledger/besu/blob/1.5.1/config/src/main/resources/dev.json
*/
const firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey();
const web3 = new Web3(rpcApiHttpHost);
const testEthAccount = web3.eth.accounts.create(uuidv4());

const keychainEntryKey = uuidv4();
const keychainEntryValue = testEthAccount.privateKey;
const keychainPlugin = new PluginKeychainMemory({
instanceId: uuidv4(),
keychainId: uuidv4(),
// pre-provision keychain with mock backend holding the private key of the
// test account that we'll reference while sending requests with the
// signing credential pointing to this keychain entry.
backend: new Map([[keychainEntryKey, keychainEntryValue]]),
logLevel,
});
keychainPlugin.set(
HelloWorldContractJson.contractName,
JSON.stringify(HelloWorldContractJson),
);
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});
const connector: PluginLedgerConnectorBesu = await factory.create({
rpcApiHttpHost,
rpcApiWsHost,
instanceId: uuidv4(),
pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }),
});
await connector.onPluginInit();
test(testcase, async () => {
keychainPlugin.set(
HelloWorldContractJson.contractName,
JSON.stringify(HelloWorldContractJson),
);
const factory = new PluginFactoryLedgerConnector({
pluginImportType: PluginImportType.Local,
});
const connector: PluginLedgerConnectorBesu = await factory.create({
rpcApiHttpHost,
rpcApiWsHost,
instanceId: uuidv4(),
pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }),
});
await connector.onPluginInit();

const req: GetBalanceV1Request = { address: firstHighNetWorthAccount };
const currentBalance = await connector.getBalance(req);
t.comment(JSON.stringify(currentBalance));
//makes the information in to string
t.ok(currentBalance, " Balance response is OK :-)");
t.equal(typeof currentBalance, "object", "Balance response type is OK :-)");
t.end();
const req: GetBalanceV1Request = { address: firstHighNetWorthAccount };
const currentBalance = await connector.getBalance(req);
//makes the information in to string
expect(currentBalance).toBeTruthy();
expect(typeof currentBalance).toBe("object");
});
});

0 comments on commit c186c65

Please sign in to comment.