Skip to content

Commit

Permalink
feat: adding test case for txbuilder compelte
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Aug 24, 2024
1 parent 3ddd8a2 commit 034b545
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/mesh-transaction/src/mesh-tx-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class MeshTxBuilder extends MeshTxBuilderCore {
const incompleteTxIns = [...inputs, ...collaterals].filter(
(txIn) => !this.isInputComplete(txIn),
);
const incompleteMints = mints.filter((mint) => this.isMintComplete(mint));
const incompleteMints = mints.filter((mint) => !this.isMintComplete(mint));
// Getting all missing utxo information
await this.queryAllTxInfo(incompleteTxIns, incompleteMints);
// Completing all inputs
Expand Down
116 changes: 105 additions & 11 deletions packages/mesh-transaction/test/mesh-tx-builder/complete.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,108 @@
// jest.mock("@meshsdk/transaction", () => ({
// resolveDataHash: jest.fn(),
// }));

describe("complete", () => {
describe("fetcher", () => {
it("should fill incomplete tx in", () => {
expect(1).toBe(1);
import {
AccountInfo,
emptyTxBuilderBody,
IFetcher,
MintItem,
ScriptSource,
SimpleScriptSourceInfo,
TransactionInfo,
TxIn,
} from "@meshsdk/common";
import { MeshTxBuilder } from "@meshsdk/transaction";

class MockTxBuilder extends MeshTxBuilder {
constructor() {
super();
this.queriedTxHashes = new Set();
this.queriedUTxOs = {};
this.fetcher = {
fetchUTxOs: jest.fn().mockResolvedValue([]),
fetchAccountInfo: jest.fn().mockResolvedValue({} as AccountInfo),
fetchAddressUTxOs: jest.fn().mockResolvedValue([]),
fetchProtocolParameters: jest.fn().mockResolvedValue({}),
fetchTxInfo: jest.fn().mockResolvedValue({} as TransactionInfo),
fetchNetworkInfo: jest.fn().mockResolvedValue({}),
fetchAssetInfo: jest.fn().mockResolvedValue({}),
fetchStakePoolInfo: jest.fn().mockResolvedValue({}),
fetchDelegationInfo: jest.fn().mockResolvedValue({}),
} as unknown as IFetcher;
}

getUTxOInfoExtended = this.getUTxOInfo;
queryAllTxInfoExtended = this.queryAllTxInfo;
}

describe("MeshTxBuilder", () => {
let txBuilder: MockTxBuilder;

beforeEach(() => {
txBuilder = new MockTxBuilder();
});

it("should update incomplete txIns and mints", async () => {
// Mock methods

jest
.spyOn(txBuilder.serializer as any, "serializeTxBody")
.mockImplementation(() => "");
jest
.spyOn(txBuilder as any, "addUtxosFromSelection")
.mockImplementation(() => {});
jest
.spyOn(txBuilder as any, "isInputComplete")
.mockImplementation((txIn) => false);
jest
.spyOn(txBuilder as any, "isMintComplete")
.mockImplementation((mint) => false);
jest.spyOn(txBuilder as any, "queryAllTxInfo").mockResolvedValue(undefined);
const completeTxInformationMock = jest
.spyOn(txBuilder as any, "completeTxInformation")
.mockImplementation(() => {});
const completeScriptInfoMock = jest
.spyOn(txBuilder as any, "completeScriptInfo")
.mockImplementation(() => {});

// Define incomplete inputs and mints
const incompleteTxIns: TxIn[] = [
{ type: "PubKey", txIn: { txHash: "txHash1", txIndex: 0 } },
{ type: "PubKey", txIn: { txHash: "txHash2", txIndex: 1 } },
];
const incompleteMints: MintItem[] = [
{
type: "Plutus",
policyId: "policyId1",
assetName: "assetName1",
amount: "100",
scriptSource: {
type: "Inline",
txHash: "txHash3",
txIndex: 2,
} as ScriptSource,
},
];

// Set the meshTxBuilderBody
txBuilder.meshTxBuilderBody = {
...emptyTxBuilderBody(),
inputs: incompleteTxIns,
collaterals: [],
mints: incompleteMints,
};

// Call the complete method
await txBuilder.complete();

// Assertions
expect(completeTxInformationMock).toHaveBeenCalledTimes(
incompleteTxIns.length,
);
incompleteTxIns.forEach((txIn) => {
expect(completeTxInformationMock).toHaveBeenCalledWith(txIn);
});
// it("should fill incomplete collateral tx in", () => {});
// it("should fill script tx in ref script script hash", () => {});
// it("should fill plutus minting ref script script hash", () => {});

expect(completeScriptInfoMock).toHaveBeenCalledTimes(1);
expect(completeScriptInfoMock).toHaveBeenCalledWith(
(incompleteMints[0] as any).scriptSource,
);
});
});

0 comments on commit 034b545

Please sign in to comment.