Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-test-framework): add reusable mocks from tests #3621

Merged
merged 15 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 86 additions & 110 deletions __tests__/unit/core-api/__support__/index.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,90 @@
import passphrases from "@packages/core-test-framework/src/internal/passphrases.json";

import { Application, Container, Contracts, Providers, Services } from "@packages/core-kernel";
import { Identifiers } from "@packages/core-kernel/src/ioc";
import { Wallets } from "@packages/core-state";
import { Identities, Utils } from "@packages/crypto";
import { One, Two } from "@packages/core-transactions/src/handlers";
import { TransactionHandlerProvider } from "@packages/core-transactions/src/handlers/handler-provider";
import { TransactionHandlerRegistry } from "@packages/core-transactions/src/handlers/handler-registry";
import {
BridgechainRegistrationTransactionHandler,
BusinessRegistrationTransactionHandler,
} from "@packages/core-magistrate-transactions/src/handlers";
import {
addressesIndexer,
ipfsIndexer, locksIndexer,
publicKeysIndexer,
usernamesIndexer,
} from "@packages/core-state/src/wallets/indexers/indexers";
import {
bridgechainIndexer,
businessIndexer,
MagistrateIndex,
} from "@packages/core-magistrate-transactions/src/wallet-indexes";
import { Wallets } from "@packages/core-state";
import {
BlockchainMocks,
BlockRepositoryMocks, NetworkMonitorMocks,
PeerStorageMocks, RoundRepositoryMocks,
StateStoreMocks, TransactionPoolProcessorMocks, TransactionPoolQueryMocks, TransactionRepositoryMocks,
} from "@tests/unit/core-api/mocks";
addressesIndexer,
ipfsIndexer,
locksIndexer,
publicKeysIndexer,
usernamesIndexer,
} from "@packages/core-state/src/wallets/indexers/indexers";
import { Mocks } from "@packages/core-test-framework";
import passphrases from "@packages/core-test-framework/src/internal/passphrases.json";
import { One, Two } from "@packages/core-transactions/src/handlers";
import { TransactionHandlerProvider } from "@packages/core-transactions/src/handlers/handler-provider";
import { TransactionHandlerRegistry } from "@packages/core-transactions/src/handlers/handler-registry";
import { Identities, Utils } from "@packages/crypto";

export type PaginatedResponse = {
totalCount: number,
results: object[],
meta: object
}
totalCount: number;
results: object[];
meta: object;
};

export type ItemResponse = {
data: object
}
data: object;
};

export const parseObjectWithBigInt = (item) => {
return JSON.parse(JSON.stringify(item, (key, value) =>
typeof value === 'bigint'
? value.toString()
: value
));
return JSON.parse(JSON.stringify(item, (key, value) => (typeof value === "bigint" ? value.toString() : value)));
};

export const buildSenderWallet = (app: Application, passphrase: string | null = null): Contracts.State.Wallet => {
let walletRepository = app.get<Wallets.WalletRepository>(Identifiers.WalletRepository);
const walletRepository = app.get<Wallets.WalletRepository>(Identifiers.WalletRepository);

let wallet: Contracts.State.Wallet = walletRepository.createWallet(Identities.Address.fromPassphrase(passphrase ? passphrase : passphrases[0]));
const wallet: Contracts.State.Wallet = walletRepository.createWallet(
Identities.Address.fromPassphrase(passphrase ? passphrase : passphrases[0]),
);

wallet.publicKey = Identities.PublicKey.fromPassphrase(passphrase ? passphrase : passphrases[0]);
wallet.balance = Utils.BigNumber.make(7527654310);

return wallet
return wallet;
};

export const initApp = (): Application => {
let app = new Application(new Container.Container());
const app = new Application(new Container.Container());

app
.bind(Container.Identifiers.PluginConfiguration)
.to(Providers.PluginConfiguration)
.inSingletonScope();
app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();

app
.bind(Container.Identifiers.StateStore)
.toConstantValue(StateStoreMocks.stateStore);
app.bind(Container.Identifiers.StateStore).toConstantValue(Mocks.StateStore.instance);

app
.bind(Container.Identifiers.BlockchainService)
.toConstantValue(BlockchainMocks.blockchain);
app.bind(Container.Identifiers.BlockchainService).toConstantValue(Mocks.Blockchain.instance);

app
.bind(Container.Identifiers.BlockRepository)
.toConstantValue(BlockRepositoryMocks.blockRepository);
app.bind(Container.Identifiers.BlockRepository).toConstantValue(Mocks.BlockRepository.instance);

app
.bind(Container.Identifiers.TransactionRepository)
.toConstantValue(TransactionRepositoryMocks.transactionRepository);
app.bind(Container.Identifiers.TransactionRepository).toConstantValue(
Mocks.TransactionRepository.instance,
);

app
.bind(Container.Identifiers.PeerNetworkMonitor)
.toConstantValue(NetworkMonitorMocks.networkMonitor);
app.bind(Container.Identifiers.PeerNetworkMonitor).toConstantValue(Mocks.NetworkMonitor.instance);

app
.bind(Container.Identifiers.PeerStorage)
.toConstantValue(PeerStorageMocks.peerStorage);
app.bind(Container.Identifiers.PeerStorage).toConstantValue(Mocks.PeerStorage.instance);

app
.bind(Container.Identifiers.RoundRepository)
.toConstantValue(RoundRepositoryMocks.roundRepository);
app.bind(Container.Identifiers.RoundRepository).toConstantValue(Mocks.RoundRepository.instance);

app
.bind(Container.Identifiers.TransactionPoolQuery)
.toConstantValue(TransactionPoolQueryMocks.transactionPoolQuery);
app.bind(Container.Identifiers.TransactionPoolQuery).toConstantValue(
Mocks.TransactionPoolQuery.instance,
);

app
.bind(Container.Identifiers.TransactionPoolProcessor)
.toConstantValue(TransactionPoolProcessorMocks.transactionPoolProcessor);
app.bind(Container.Identifiers.TransactionPoolProcessor).toConstantValue(
Mocks.TransactionPoolProcessor.instance,
);

app
.bind(Container.Identifiers.TransactionPoolProcessorFactory)
.toFactory(() => () => {
return TransactionPoolProcessorMocks.transactionPoolProcessor
});
app.bind(Container.Identifiers.TransactionPoolProcessorFactory).toFactory(() => () => {
return Mocks.TransactionPoolProcessor.instance;
});

app
.bind(Container.Identifiers.EventDispatcherService)
.toConstantValue({});
app.bind(Container.Identifiers.EventDispatcherService).toConstantValue({});

app.bind(Identifiers.TransactionHandler).to(One.TransferTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.TransferTransactionHandler);
Expand All @@ -135,54 +109,56 @@ export const initApp = (): Application => {
app.bind(Identifiers.TransactionHandler).to(BusinessRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(BridgechainRegistrationTransactionHandler);

app
.bind<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes)
app.bind<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes)
.to(Services.Attributes.AttributeSet)
.inSingletonScope();

app
.bind<Contracts.State.WalletIndexerIndex>(Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Addresses, indexer: addressesIndexer });

app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.PublicKeys, indexer: publicKeysIndexer });

app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Usernames, indexer: usernamesIndexer });

app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Ipfs, indexer: ipfsIndexer });

app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: Contracts.State.WalletIndexes.Locks, indexer: locksIndexer });

app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: MagistrateIndex.Businesses, indexer: businessIndexer });

app
.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex)
.toConstantValue({ name: MagistrateIndex.Bridgechains, indexer: bridgechainIndexer });

app
.bind(Identifiers.WalletFactory)
.toFactory<Contracts.State.Wallet>((context: Container.interfaces.Context) => (address: string) =>
app.bind<Contracts.State.WalletIndexerIndex>(Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Addresses,
indexer: addressesIndexer,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.PublicKeys,
indexer: publicKeysIndexer,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Usernames,
indexer: usernamesIndexer,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Ipfs,
indexer: ipfsIndexer,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Locks,
indexer: locksIndexer,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: MagistrateIndex.Businesses,
indexer: businessIndexer,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: MagistrateIndex.Bridgechains,
indexer: bridgechainIndexer,
});

app.bind(Identifiers.WalletFactory).toFactory<Contracts.State.Wallet>(
(context: Container.interfaces.Context) => (address: string) =>
new Wallets.Wallet(
address,
new Services.Attributes.AttributeMap(
context.container.get<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes),
),
),
);
);

app
.bind(Identifiers.WalletRepository)
.to(Wallets.WalletRepository)
.inSingletonScope();
app.bind(Identifiers.WalletRepository).to(Wallets.WalletRepository).inSingletonScope();

return app;
};
36 changes: 21 additions & 15 deletions __tests__/unit/core-api/controllers/blockchain.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import "jest-extended";
import { Application } from "@packages/core-kernel";
import { initApp, ItemResponse } from "../__support__";

import { BlockchainController } from "@packages/core-api/src/controllers/blockchain";
import { StateStoreMocks } from "../mocks";
import { Application } from "@packages/core-kernel";
import { Mocks } from "@packages/core-test-framework";
import { Interfaces } from "@packages/crypto";

import { initApp, ItemResponse } from "../__support__";

let app: Application;
let controller: BlockchainController;

Expand All @@ -14,31 +16,35 @@ beforeEach(() => {
controller = app.resolve<BlockchainController>(BlockchainController);
});

afterEach(() => {
Mocks.StateStore.setBlock(undefined);
});

describe("BlockchainController", () => {
describe("index", () => {
it("should return last block from store", async () => {
let mockBlockData: Partial<Interfaces.IBlockData> = {
const mockBlockData: Partial<Interfaces.IBlockData> = {
id: "1",
height: 1,
};

let mockBlock: Partial<Interfaces.IBlock> = {
data: mockBlockData as Interfaces.IBlockData
const mockBlock = {
data: mockBlockData,
};

StateStoreMocks.setMockBlock(mockBlock);
Mocks.StateStore.setBlock(mockBlock as Partial<Interfaces.IBlock>);

type BlockItemResponse = ItemResponse & {
data: {
block: {
id: string,
height: number
},
supply: string
},
}

let response = <BlockItemResponse>(await controller.index());
id: string;
height: number;
};
supply: string;
};
};

const response = (await controller.index()) as BlockItemResponse;

expect(response.data.supply).toBeDefined();
expect(response.data.block).toEqual(mockBlockData);
Expand Down
Loading