Skip to content

Commit

Permalink
test: add sendTransaction test
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Feb 20, 2024
1 parent 470fb01 commit 8b9dfcf
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions packages/thirdweb/src/transaction/actions/send-transaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { sendTransaction } from "./send-transaction.js";
import type { Account, Wallet } from "../../wallets/index.js";
import { transfer } from "../../extensions/erc20.js";
import { USDC_CONTRACT } from "../../../test/src/test-contracts.js";
import { TEST_WALLET_A, TEST_WALLET_B } from "../../../test/src/addresses.js";

const MOCK_TX_HASH =
"0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef";

const mockAccount = {
address: TEST_WALLET_A,
sendTransaction: vi.fn().mockResolvedValue({ transactionHash: MOCK_TX_HASH }),
signMessage: vi.fn().mockResolvedValue("0xabcdef1234567890"),
signTypedData: vi.fn().mockResolvedValue("0xabcdef1234567890"),
} satisfies Account;

const mockWallet = {
getAccount: vi.fn().mockReturnValue(mockAccount),
getChain: vi.fn().mockReturnValue({ id: 1 }),
switchChain: vi.fn().mockResolvedValue(undefined),
metadata: {
id: "mock",
name: "Mock Wallet",
iconUrl: "https://example.com/icon.png",
},
connect: vi.fn().mockResolvedValue(mockAccount),
autoConnect: vi.fn().mockResolvedValue(mockAccount),
disconnect: vi.fn().mockResolvedValue(undefined),
} satisfies Wallet;

const TRANSACTION = transfer({
to: TEST_WALLET_B,
amount: 100,
contract: USDC_CONTRACT,
});

const TX_RESULT = {
accessList: undefined,
chainId: 1,
data: "0xa9059cbb00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000005f5e100",
gas: 45134n,
maxFeePerGas: 103423132068n,
maxPriorityFeePerGas: 53408668142n,
nonce: 0,
to: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
value: undefined,
};
describe("sendTransaction", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("account", () => {
it("should send a transaction", async () => {
const res = await sendTransaction({
account: mockAccount,
transaction: TRANSACTION,
});
expect(res.transactionHash).toEqual(MOCK_TX_HASH);
expect(mockAccount.sendTransaction).toHaveBeenCalledTimes(1);
expect(mockAccount.sendTransaction.mock.lastCall[0]).toMatchObject(

Check failure on line 61 in packages/thirdweb/src/transaction/actions/send-transaction.test.ts

View workflow job for this annotation

GitHub Actions / Unit Tests

src/transaction/actions/send-transaction.test.ts > sendTransaction > account > should send a transaction

AssertionError: expected { …(9) } to match object { accessList: undefined, …(8) } - Expected + Received Object { "accessList": undefined, "chainId": 1, "data": "0xa9059cbb00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000005f5e100", "gas": 45134n, - "maxFeePerGas": 103423132068n, - "maxPriorityFeePerGas": 53408668142n, + "maxFeePerGas": 51114463926n, + "maxPriorityFeePerGas": 1100000000n, "nonce": 0, "to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "value": undefined, } ❯ src/transaction/actions/send-transaction.test.ts:61:60

Check failure on line 61 in packages/thirdweb/src/transaction/actions/send-transaction.test.ts

View workflow job for this annotation

GitHub Actions / Unit Tests

src/transaction/actions/send-transaction.test.ts > sendTransaction > account > should send a transaction

AssertionError: expected { …(9) } to match object { accessList: undefined, …(8) } - Expected + Received Object { "accessList": undefined, "chainId": 1, "data": "0xa9059cbb00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000005f5e100", "gas": 45134n, - "maxFeePerGas": 103423132068n, - "maxPriorityFeePerGas": 53408668142n, + "maxFeePerGas": 51114463926n, + "maxPriorityFeePerGas": 1100000000n, "nonce": 0, "to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "value": undefined, } ❯ src/transaction/actions/send-transaction.test.ts:61:60

Check failure on line 61 in packages/thirdweb/src/transaction/actions/send-transaction.test.ts

View workflow job for this annotation

GitHub Actions / Unit Tests

src/transaction/actions/send-transaction.test.ts > sendTransaction > account > should send a transaction

AssertionError: expected { …(9) } to match object { accessList: undefined, …(8) } - Expected + Received Object { "accessList": undefined, "chainId": 1, "data": "0xa9059cbb00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000005f5e100", "gas": 45134n, - "maxFeePerGas": 103423132068n, - "maxPriorityFeePerGas": 53408668142n, + "maxFeePerGas": 51114463926n, + "maxPriorityFeePerGas": 1100000000n, "nonce": 0, "to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "value": undefined, } ❯ src/transaction/actions/send-transaction.test.ts:61:60

Check failure on line 61 in packages/thirdweb/src/transaction/actions/send-transaction.test.ts

View workflow job for this annotation

GitHub Actions / Unit Tests

src/transaction/actions/send-transaction.test.ts > sendTransaction > account > should send a transaction

AssertionError: expected { …(9) } to match object { accessList: undefined, …(8) } - Expected + Received Object { "accessList": undefined, "chainId": 1, "data": "0xa9059cbb00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000005f5e100", "gas": 45134n, - "maxFeePerGas": 103423132068n, - "maxPriorityFeePerGas": 53408668142n, + "maxFeePerGas": 51114463926n, + "maxPriorityFeePerGas": 1100000000n, "nonce": 0, "to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "value": undefined, } ❯ src/transaction/actions/send-transaction.test.ts:61:60
TX_RESULT,
);
});
});

describe("wallet", () => {
it("should send a transaction", async () => {
const res = await sendTransaction({
wallet: mockWallet,
transaction: TRANSACTION,
});
expect(res.transactionHash).toEqual(MOCK_TX_HASH);
expect(mockAccount.sendTransaction).toHaveBeenCalledTimes(1);
expect(mockAccount.sendTransaction.mock.lastCall[0]).toMatchObject(
TX_RESULT,
);
});

it("should switch chains if the chain mismatches", async () => {
const wallet = {
...mockWallet,
getChain: vi.fn().mockReturnValue({ id: 5 }),
};
const res = await sendTransaction({
wallet,
transaction: TRANSACTION,
});

expect(wallet.switchChain).toHaveBeenCalledTimes(1);
// we should've switched to the chain of the transaction
expect(wallet.switchChain.mock.lastCall[0]).toMatchObject({ id: 1 });
expect(res.transactionHash).toEqual(MOCK_TX_HASH);
expect(mockAccount.sendTransaction).toHaveBeenCalledTimes(1);
expect(mockAccount.sendTransaction.mock.lastCall[0]).toMatchObject(
TX_RESULT,
);
});

it("should throw if not connected", async () => {
const wallet = {
...mockWallet,
getAccount: vi.fn().mockReturnValue(undefined),
};
await expect(
sendTransaction({
wallet,
transaction: TRANSACTION,
}),
).rejects.toThrow("not connected");
});
});
});

0 comments on commit 8b9dfcf

Please sign in to comment.