-
Notifications
You must be signed in to change notification settings - Fork 128
Add useCrossVmSpendFt
hook
#2499
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
Merged
+447
−4
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1b12b8a
Add `useFullTokenBalance` hook
jribbink 797fc01
Merge remote-tracking branch 'origin/master' into jribbink/cross-vm-b…
jribbink 46146b2
Add Cross VM NFT Spend Transcation
jribbink 874607d
Merge remote-tracking branch 'origin/master' into jribbink/cross-vm-n…
jribbink b5658cf
delete unused
jribbink 7ed0e72
Add cross VM FT Spend hook
jribbink 1461b8e
Merge branch 'feature/cross-vm-hooks' into jribbink/cross-vm-ft-spend
jribbink 5bf261d
switch to new fmt with test
jribbink 3ec1e96
cleanup
jribbink 8ebc971
Merge branch 'feature/cross-vm-hooks' into jribbink/cross-vm-ft-spend
jribbink 588c7cb
Fix tests
jribbink 5647c4c
fix naming
jribbink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import {renderHook, act, waitFor} from "@testing-library/react" | ||
import * as fcl from "@onflow/fcl" | ||
import {FlowProvider} from "../provider" | ||
import { | ||
getCrossVmSpendFtTransaction, | ||
useCrossVmSpendFt, | ||
} from "./useCrossVmSpendFt" | ||
import {useFlowChainId} from "./useFlowChainId" | ||
|
||
jest.mock("@onflow/fcl", () => require("../__mocks__/fcl").default) | ||
jest.mock("./useFlowChainId", () => ({ | ||
useFlowChainId: jest.fn(), | ||
})) | ||
|
||
describe("useBatchEvmTransaction", () => { | ||
const mockCalls = [ | ||
{ | ||
address: "0x123", | ||
abi: [{type: "function", name: "test"}], | ||
functionName: "test", | ||
args: [1, 2], | ||
gasLimit: BigInt(100000), | ||
value: BigInt(0), | ||
}, | ||
] | ||
|
||
const mockTxId = "0x123" | ||
const mockTxResult = { | ||
events: [ | ||
{ | ||
type: "TransactionExecuted", | ||
data: { | ||
hash: ["1", "2", "3"], | ||
errorCode: "0", | ||
errorMessage: "", | ||
}, | ||
}, | ||
], | ||
} | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
jest.mocked(useFlowChainId).mockReturnValue({ | ||
data: "mainnet", | ||
isLoading: false, | ||
} as any) | ||
}) | ||
|
||
describe("getCrossVmspendFtTransaction", () => { | ||
it("should return correct cadence for mainnet", () => { | ||
const result = getCrossVmSpendFtTransaction("mainnet") | ||
expect(result).toContain("import EVM from 0xe467b9dd11fa00df") | ||
}) | ||
|
||
it("should return correct cadence for testnet", () => { | ||
const result = getCrossVmSpendFtTransaction("testnet") | ||
expect(result).toContain("import EVM from 0x8c5303eaa26202d6") | ||
}) | ||
|
||
it("should throw error for unsupported chain", () => { | ||
expect(() => getCrossVmSpendFtTransaction("unsupported")).toThrow( | ||
"Unsupported chain: unsupported" | ||
) | ||
}) | ||
}) | ||
|
||
describe("useCrossVmBatchTransaction", () => { | ||
test("should handle successful transaction", async () => { | ||
jest.mocked(fcl.mutate).mockResolvedValue(mockTxId) | ||
jest.mocked(fcl.tx).mockReturnValue({ | ||
onceExecuted: jest.fn().mockResolvedValue(mockTxResult), | ||
} as any) | ||
|
||
let result: any | ||
let rerender: any | ||
await act(async () => { | ||
;({result, rerender} = renderHook(useCrossVmSpendFt, { | ||
wrapper: FlowProvider, | ||
})) | ||
}) | ||
|
||
await act(async () => { | ||
await result.current.spendFt({ | ||
calls: mockCalls, | ||
vaultIdentifier: "A.1234.Token.Vault", | ||
amount: "100.0", | ||
}) | ||
rerender() | ||
}) | ||
|
||
await waitFor(() => result.current.isPending === false) | ||
|
||
expect(result.current.isError).toBe(false) | ||
expect(result.current.data).toBe(mockTxId) | ||
}) | ||
|
||
it("should handle missing chain ID", async () => { | ||
;(useFlowChainId as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isLoading: false, | ||
}) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook(() => useCrossVmSpendFt(), { | ||
wrapper: FlowProvider, | ||
}) | ||
hookResult = result | ||
}) | ||
|
||
await act(async () => { | ||
await hookResult.current.spendFt({ | ||
calls: mockCalls, | ||
vaultIdentifier: "A.1234.Token.Vault", | ||
amount: "100.0", | ||
}) | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isError).toBe(true)) | ||
expect(hookResult.current.error?.message).toBe("No current chain found") | ||
}) | ||
|
||
it("should handle loading chain ID", async () => { | ||
;(useFlowChainId as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isLoading: true, | ||
}) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook(() => useCrossVmSpendFt(), { | ||
wrapper: FlowProvider, | ||
}) | ||
hookResult = result | ||
}) | ||
|
||
await act(async () => { | ||
await hookResult.current.spendFt({ | ||
calls: mockCalls, | ||
vaultIdentifier: "A.1234.Token.Vault", | ||
amount: "100.0", | ||
}) | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isError).toBe(true)) | ||
expect(hookResult.current.error?.message).toBe("No current chain found") | ||
}) | ||
|
||
it("should handle mutation error", async () => { | ||
;(fcl.mutate as jest.Mock).mockRejectedValue(new Error("Mutation failed")) | ||
|
||
let hookResult: any | ||
|
||
await act(async () => { | ||
const {result} = renderHook(() => useCrossVmSpendFt(), { | ||
wrapper: FlowProvider, | ||
}) | ||
hookResult = result | ||
}) | ||
|
||
await act(async () => { | ||
await hookResult.current.spendFt({ | ||
calls: mockCalls, | ||
vaultIdentifier: "A.1234.Token.Vault", | ||
amount: "100.0", | ||
}) | ||
}) | ||
|
||
await waitFor(() => expect(hookResult.current.isError).toBe(true)) | ||
expect(hookResult.current.error?.message).toBe("Mutation failed") | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.