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

test: add throw scenarios to simulate and dryRun #1246

Merged
merged 8 commits into from
Sep 14, 2023
2 changes: 2 additions & 0 deletions .changeset/rare-buttons-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
50 changes: 49 additions & 1 deletion apps/demo-typegen/src/demo.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #region Testing-with-jest-ts
import { generateTestWallet } from '@fuel-ts/wallet/test-utils';
import { ContractFactory, Provider, toHex, BaseAssetId } from 'fuels';
import { ContractFactory, Provider, toHex, BaseAssetId, Wallet } from 'fuels';

import storageSlots from '../contract/out/debug/demo-contract-storage_slots.json';

Expand Down Expand Up @@ -45,3 +45,51 @@ describe('ExampleContract', () => {
});
});
// #endregion Testing-with-jest-ts

it('should throw when simulating via contract factory with wallet with no resources', async () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
const fundedWallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, fundedWallet);
const contract = await factory.deployContract();
const contractInstance = DemoContractAbi__factory.connect(contract.id, unfundedWallet);

let result;
let error;

try {
const r = await contractInstance.functions.return_input(1337).simulate();
result = r;
} catch (e) {
error = e as { message: string };
expect(error?.message).toContain('not enough coins to fit the target');
}

expect(result).toBeFalsy();
expect(error).toBeTruthy();
});

it('should throw when dry running via contract factory with wallet with no resources', async () => {
const provider = new Provider('http://127.0.0.1:4000/graphql');
const fundedWallet = await generateTestWallet(provider, [[1_000, BaseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, fundedWallet);
const contract = await factory.deployContract();
const contractInstance = DemoContractAbi__factory.connect(contract.id, unfundedWallet);

let result;
let error;

try {
const r = await contractInstance.functions.return_input(1337).dryRun();
result = r;
} catch (e) {
error = e as { message: string };
expect(error?.message).toContain('not enough coins to fit the target');
}

expect(result).toBeFalsy();
expect(error).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseAssetId, Wallet, BN } from 'fuels';
import { BaseAssetId, Wallet, BN, Contract } from 'fuels';

import { SnippetProjectEnum } from '../../../projects';
import { SnippetProjectEnum, getSnippetProjectArtifacts } from '../../../projects';
import { createAndDeployContractFromProject } from '../../utils';

describe(__filename, () => {
Expand Down Expand Up @@ -35,4 +35,46 @@ describe(__filename, () => {
// #endregion simulate-transactions-2
expect(value).toEqual(15);
});

it('should throw when simulating with an unfunded wallet', async () => {
const contract = await createAndDeployContractFromProject(SnippetProjectEnum.ECHO_VALUES);
const unfundedWallet = Wallet.generate({ provider: contract.provider });
const { abiContents: abi } = getSnippetProjectArtifacts(SnippetProjectEnum.ECHO_VALUES);
const deployedContract = new Contract(contract.id, abi, unfundedWallet);

let result;
let error;

try {
const r = await deployedContract.functions.echo_u8(15).simulate();
result = r;
} catch (e) {
error = e as { message: string };
expect(error?.message).toContain('not enough coins to fit the target');
}

expect(result).toBeFalsy();
expect(error).toBeTruthy();
});

it('should throw when dry running with an unfunded wallet', async () => {
const contract = await createAndDeployContractFromProject(SnippetProjectEnum.ECHO_VALUES);
const unfundedWallet = Wallet.generate({ provider: contract.provider });
const { abiContents: abi } = getSnippetProjectArtifacts(SnippetProjectEnum.ECHO_VALUES);
const deployedContract = new Contract(contract.id, abi, unfundedWallet);

let result;
let error;

try {
const r = await deployedContract.functions.echo_u8(15).dryRun();
result = r;
} catch (e) {
error = e as { message: string };
expect(error?.message).toContain('not enough coins to fit the target');
}

expect(result).toBeFalsy();
expect(error).toBeTruthy();
});
});