All files / test/integration/helpers common.ts

100% Statements 23/23
100% Branches 4/4
100% Functions 11/11
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 912x             2x       39x 520x             39x 265x             39x                 2x     6x 60x         1159x 750x         409x         1159x                   2x         6x 39x     39x             2x 11x 11x 11x 11x        
import {
	CheckerFunctionResponse,
	CheckTransactionsResponseWithPassAndFail,
	Status,
} from '../../../src/check_transactions';
import { Transaction } from '../../../src/transaction_pool';
 
export const checkerFunctionResponseGenerator = async (
	passedTransactions: ReadonlyArray<Transaction>,
	failedTransactions: ReadonlyArray<Transaction>,
): Promise<CheckerFunctionResponse> => {
	const passedTransactionsResponse = passedTransactions.map(transaction => {
		return {
			id: transaction.id,
			status: Status.OK,
			errors: [],
		};
	});
 
	const failedTransactionsResponse = failedTransactions.map(transaction => {
		return {
			id: transaction.id,
			status: Status.FAIL,
			errors: [new Error()],
		};
	});
 
	return {
		status: failedTransactions.length === 0 ? Status.OK : Status.FAIL,
		transactionsResponses: [
			...passedTransactionsResponse,
			...failedTransactionsResponse,
		],
	};
};
 
export const fakeCheckFunctionGenerator = (
	firstCharacterOfFailedTransactionsId: ReadonlyArray<String>,
) => {
	return (transactions: ReadonlyArray<Transaction>) => {
		return transactions.reduce(
			(
				checkedTransactions: CheckTransactionsResponseWithPassAndFail,
				transaction: Transaction,
			) => {
				if (!firstCharacterOfFailedTransactionsId.includes(transaction.id[0])) {
					checkedTransactions.passedTransactions = [
						...checkedTransactions.passedTransactions,
						transaction,
					];
				} else {
					checkedTransactions.failedTransactions = [
						...checkedTransactions.failedTransactions,
						transaction,
					];
				}
				return checkedTransactions;
			},
			{
				passedTransactions: [],
				failedTransactions: [],
			},
		);
	};
};
 
export const fakeCheckerFunctionGenerator = (
	checkFunction: (
		transactions: ReadonlyArray<Transaction>,
	) => CheckTransactionsResponseWithPassAndFail,
) => {
	return (transactions: ReadonlyArray<Transaction>) => {
		const { passedTransactions, failedTransactions } = checkFunction(
			transactions,
		);
		return checkerFunctionResponseGenerator(
			passedTransactions,
			failedTransactions,
		);
	};
};
 
export const wrapExpectationInNextTick = (expectations: Function) => {
	return new Promise(resolve => {
		process.nextTick(() => {
			expectations();
			resolve();
		});
	});
};