Skip to content

Commit b56e969

Browse files
authored
Test(data): add test coverage for getEvents utility (#989)
test(data): add test coverage for getEvents utility re #986
1 parent c973bab commit b56e969

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

packages/data/tests/getEvents.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { Contract, EventLog } from "ethers/contract"
2+
import getEvents from "../src/getEvents"
3+
4+
jest.mock("ethers/contract", () => ({
5+
__esModule: true,
6+
Contract: jest.fn(),
7+
EventLog: jest.fn()
8+
}))
9+
10+
describe("getEvents", () => {
11+
let mockContract: jest.Mocked<Contract>
12+
13+
beforeEach(() => {
14+
mockContract = {
15+
filters: {
16+
TestEvent: jest.fn()
17+
},
18+
queryFilter: jest.fn()
19+
} as any
20+
})
21+
22+
describe("# getEvents", () => {
23+
it("should fetch events with basic parameters", async () => {
24+
const mockEvents = [
25+
{
26+
args: ["arg1", "arg2"],
27+
blockNumber: 123
28+
},
29+
{
30+
args: ["arg3", "arg4"],
31+
blockNumber: 124
32+
}
33+
] as EventLog[]
34+
35+
mockContract.queryFilter.mockResolvedValueOnce(mockEvents)
36+
37+
const result = await getEvents(mockContract, "TestEvent")
38+
39+
expect(mockContract.filters.TestEvent).toHaveBeenCalled()
40+
expect(mockContract.queryFilter).toHaveBeenCalled()
41+
expect(result).toEqual([
42+
["arg1", "arg2", 123],
43+
["arg3", "arg4", 124]
44+
])
45+
})
46+
47+
it("should handle filter arguments", async () => {
48+
const filterArgs = ["arg1", "arg2"]
49+
const mockEvents = [
50+
{
51+
args: ["arg1", "arg2"],
52+
blockNumber: 123
53+
}
54+
] as EventLog[]
55+
56+
mockContract.queryFilter.mockResolvedValueOnce(mockEvents)
57+
58+
await getEvents(mockContract, "TestEvent", filterArgs)
59+
60+
expect(mockContract.filters.TestEvent).toHaveBeenCalledWith(...filterArgs)
61+
})
62+
63+
it("should use startBlock parameter", async () => {
64+
const startBlock = 1000
65+
const mockEvents = [
66+
{
67+
args: ["arg1"],
68+
blockNumber: 1001
69+
}
70+
] as EventLog[]
71+
72+
mockContract.queryFilter.mockResolvedValueOnce(mockEvents)
73+
74+
await getEvents(mockContract, "TestEvent", [], startBlock)
75+
76+
expect(mockContract.queryFilter).toHaveBeenCalledWith(undefined, startBlock)
77+
})
78+
79+
it("should handle empty events array", async () => {
80+
mockContract.queryFilter.mockResolvedValueOnce([])
81+
82+
const result = await getEvents(mockContract, "TestEvent")
83+
84+
expect(result).toEqual([])
85+
})
86+
87+
it("should handle undefined filterArgs gracefully", async () => {
88+
const mockEvents = [
89+
{
90+
args: ["arg1"],
91+
blockNumber: 101
92+
}
93+
] as EventLog[]
94+
95+
mockContract.queryFilter.mockResolvedValueOnce(mockEvents)
96+
97+
await getEvents(mockContract, "TestEvent", undefined)
98+
99+
expect(mockContract.filters.TestEvent).toHaveBeenCalledWith()
100+
})
101+
102+
it("should handle contract errors", async () => {
103+
mockContract.queryFilter.mockRejectedValue(new Error("Contract error"))
104+
105+
await expect(getEvents(mockContract, "TestEvent")).rejects.toThrow("Contract error")
106+
})
107+
})
108+
})

0 commit comments

Comments
 (0)