diff --git a/tests/utils/delegations/createStakingTx.test.ts b/tests/utils/delegations/createStakingTx.test.ts index 773b616c..45a94929 100644 --- a/tests/utils/delegations/createStakingTx.test.ts +++ b/tests/utils/delegations/createStakingTx.test.ts @@ -4,10 +4,12 @@ import { createStakingTx } from "@/utils/delegations/signStakingTx"; import { DEFAULT_TEST_FEE_RATE, testingNetworks } from "../../helper"; -describe("utils/delegations/createStakingTx", () => { - initBTCCurve(); - testingNetworks.forEach(({ network, dataGenerator: dataGen }) => { - [true, false].forEach((isFixed) => { +initBTCCurve(); + +describe.each(testingNetworks)( + "utils/delegations/createStakingTx", + ({ network, dataGenerator: dataGen }) => { + describe.each([true, false])("isFixed %s", (isFixed) => { const randomFpKeys = dataGen.generateRandomKeyPair(); const randomStakerKeys = dataGen.generateRandomKeyPair(); const feeRate = DEFAULT_TEST_FEE_RATE; @@ -205,5 +207,5 @@ describe("utils/delegations/createStakingTx", () => { }); } }); - }); -}); + }, +); diff --git a/tests/utils/delegations/fee.test.ts b/tests/utils/delegations/fee.test.ts index 6afc0adf..b1a46b82 100644 --- a/tests/utils/delegations/fee.test.ts +++ b/tests/utils/delegations/fee.test.ts @@ -2,8 +2,9 @@ import { txFeeSafetyCheck } from "@/utils/delegations/fee"; import { testingNetworks } from "../../helper"; -describe("txFeeSafetyCheck", () => { - testingNetworks.map(({ networkName, dataGenerator }) => { +describe.each(testingNetworks)( + "txFeeSafetyCheck", + ({ networkName, dataGenerator }) => { const feeRate = dataGenerator.generateRandomFeeRates(); const globalParams = dataGenerator.generateGlobalPramsVersions( dataGenerator.getRandomIntegerBetween(1, 10), @@ -17,6 +18,7 @@ describe("txFeeSafetyCheck", () => { randomParam.activationHeight + 1, ); const tx = signedPsbt.extractTransaction(); + describe(`on ${networkName} - `, () => { test("should not throw an error if the estimated fee is within the acceptable range", () => { let estimatedFee = (tx.virtualSize() * feeRate) / 2 + 1; @@ -58,5 +60,5 @@ describe("txFeeSafetyCheck", () => { }).not.toThrow(); }); }); - }); -}); + }, +); diff --git a/tests/utils/delegations/signStakingTx.test.ts b/tests/utils/delegations/signStakingTx.test.ts index 8b39960d..23b2d884 100644 --- a/tests/utils/delegations/signStakingTx.test.ts +++ b/tests/utils/delegations/signStakingTx.test.ts @@ -1,10 +1,3 @@ -import { initBTCCurve } from "btc-staking-ts"; - -import { signStakingTx } from "@/utils/delegations/signStakingTx"; - -import { DEFAULT_TEST_FEE_RATE, testingNetworks } from "../../helper"; - -// Mock the signPsbtTransaction function jest.mock("@/app/common/utils/psbt", () => ({ signPsbtTransaction: jest.fn(), })); @@ -12,12 +5,16 @@ jest.mock("@/utils/delegations/fee", () => ({ txFeeSafetyCheck: jest.fn().mockReturnValue(undefined), })); -describe("utils/delegations/signStakingTx", () => { - initBTCCurve(); - testingNetworks.forEach(({ network, dataGenerator: dataGen }) => { +import { signPsbtTransaction } from "@/app/common/utils/psbt"; +import { signStakingTx } from "@/utils/delegations/signStakingTx"; + +import { DEFAULT_TEST_FEE_RATE, testingNetworks } from "../../helper"; + +describe.each(testingNetworks)( + "txFeeSafetyCheck", + ({ network, dataGenerator: dataGen }) => { const randomFpKeys = dataGen.generateRandomKeyPair(); const randomUserKeys = dataGen.generateRandomKeyPair(); - const feeRate = DEFAULT_TEST_FEE_RATE; const randomParam = dataGen.generateRandomGlobalParams(true); const randomStakingAmount = dataGen.getRandomIntegerBetween( randomParam.minStakingAmountSat, @@ -31,15 +28,12 @@ describe("utils/delegations/signStakingTx", () => { dataGen.getAddressAndScriptPubKey( dataGen.generateRandomKeyPair().publicKey, ).taproot; - const randomInputUTXOs = dataGen.generateRandomUTXOs( randomStakingAmount + Math.floor(Math.random() * 100000000), Math.floor(Math.random() * 10) + 1, scriptPubKey, ); const txHex = dataGen.generateRandomTxId(); - - // mock the btcWallet const btcWallet = { signPsbt: jest.fn(), getWalletProviderName: jest.fn(), @@ -47,15 +41,9 @@ describe("utils/delegations/signStakingTx", () => { }; it("should successfully sign a staking transaction", async () => { - // Import the mock function - const { signPsbtTransaction } = require("@/app/common/utils/psbt"); - signPsbtTransaction.mockImplementationOnce(() => { - return async () => { - return { - toHex: () => txHex, - }; - }; - }); + (signPsbtTransaction as any).mockImplementationOnce(() => async () => ({ + toHex: () => txHex, + })); const result = await signStakingTx( btcWallet as any, @@ -66,7 +54,7 @@ describe("utils/delegations/signStakingTx", () => { network, randomTaprootAddress, randomUserKeys.noCoordPublicKey, - feeRate, + DEFAULT_TEST_FEE_RATE, randomInputUTXOs, ); @@ -79,12 +67,8 @@ describe("utils/delegations/signStakingTx", () => { }); it("should throw an error when signing a staking transaction", async () => { - // Import the mock function - const { signPsbtTransaction } = require("@/app/common/utils/psbt"); - signPsbtTransaction.mockImplementationOnce(() => { - return async () => { - throw new Error("signing error"); - }; + (signPsbtTransaction as any).mockImplementationOnce(() => async () => { + throw new Error("signing error"); }); try { @@ -97,7 +81,7 @@ describe("utils/delegations/signStakingTx", () => { network, randomTaprootAddress, randomUserKeys.noCoordPublicKey, - feeRate, + DEFAULT_TEST_FEE_RATE, randomInputUTXOs, ); } catch (error: any) { @@ -105,5 +89,5 @@ describe("utils/delegations/signStakingTx", () => { expect(error.message).toBe("signing error"); } }); - }); -}); + }, +); diff --git a/tests/utils/delegations/signUnbondingTx.test.ts b/tests/utils/delegations/signUnbondingTx.test.ts index 7969510c..0403e2eb 100644 --- a/tests/utils/delegations/signUnbondingTx.test.ts +++ b/tests/utils/delegations/signUnbondingTx.test.ts @@ -35,9 +35,7 @@ describe("signUnbondingTx", () => { randomStakingTxHeight, stakerKeys, ); - const signedPsbtTx = signedPsbt.extractTransaction(); - const mockedDelegationApi = [ { stakingTxHashHex: randomTxId, diff --git a/tests/utils/globalParams.test.ts b/tests/utils/globalParams.test.ts index 720301b6..dceae9dd 100644 --- a/tests/utils/globalParams.test.ts +++ b/tests/utils/globalParams.test.ts @@ -102,8 +102,11 @@ describe("globalParams", () => { expect(result.isApprochingNextVersion).toBeFalsy(); }); - it("should return correct values for params version that are not first or last", () => { - for (let index = 1; index < globalParams.length - 2; index++) { + describe("should return correct values for params version that are not first or last", () => { + const globalParams = dataGenerator.generateGlobalPramsVersions(10); + const paramIndexes = globalParams.map((_, i) => i).slice(1, -1); + + it.each(paramIndexes)("param #%i", (index) => { const param = globalParams[index]; // before approaching const lastConfirmationDepth = globalParams[index - 1].confirmationDepth; @@ -131,6 +134,6 @@ describe("globalParams", () => { expect(result.currentVersion).toEqual(param); expect(result.nextVersion).toEqual(globalParams[index + 1]); expect(result.isApprochingNextVersion).toBeFalsy(); - } + }); }); });