forked from muttoni/fcl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-get-events.test.js
180 lines (150 loc) · 5.46 KB
/
send-get-events.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {AccessAPI} from "@onflow/protobuf"
import {sendGetEvents} from "./send-get-events.js"
import {build} from "../build/build.js"
import {getEventsAtBlockIds} from "../build/build-get-events-at-block-ids.js"
import {getEventsAtBlockHeightRange} from "../build/build-get-events-at-block-height-range.js"
import {atBlockHeight} from "../build/build-at-block-height.js"
import {resolve} from "../resolve/resolve.js"
const jsonToUInt8Array = (json) => {
var str = JSON.stringify(json, null, 0);
var ret = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
ret[i] = str.charCodeAt(i);
}
return ret
};
const hexStrToUInt8Array = (hex) => {
return new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
};
const strToUInt8Array = (str) => {
var ret = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
ret[i] = str.charCodeAt(i);
}
return ret
};
describe("Send Get Events", () => {
test("GetEventsForBlockIDs", async () => {
const unaryMock = jest.fn();
const dateNow = new Date(Date.now())
const returnedEvents = [
{
blockId: "a1b2c3",
blockHeight: 123,
blockTimestamp: dateNow.toISOString(),
type: "MyEvent",
transactionId: "a1b2c3",
transactionIndex: 123,
eventIndex: 456,
payload: {type: "String", value: "Hello, Flow"}
}
]
unaryMock.mockReturnValue({
getResultsList: () => [
{
getBlockId_asU8: () => hexStrToUInt8Array("a1b2c3"),
getBlockHeight: () => 123,
getBlockTimestamp: () => ({
toDate: () => ({
toISOString: () => dateNow.toISOString(),
})
}),
getEventsList: () => ([
{
getType: () => "MyEvent",
getTransactionId_asU8: () => hexStrToUInt8Array("a1b2c3"),
getTransactionIndex: () => 123,
getEventIndex: () => 456,
getPayload_asU8: () => jsonToUInt8Array({type: "String", value: "Hello, Flow"}),
}
])
}
]
});
const response = await sendGetEvents(
await resolve(
await build([
getEventsAtBlockIds("MyEvent", ["a1b2c3"]),
])
),
{
unary: unaryMock,
node: "localhost:3000"
}
)
expect(unaryMock.mock.calls.length).toEqual(1)
const unaryMockArgs = unaryMock.mock.calls[0]
expect(unaryMockArgs.length).toEqual(3)
const unaryType = unaryMock.mock.calls[0][1]
expect(unaryType).toEqual(AccessAPI.GetEventsForBlockIDs)
const unaryMockRequest = unaryMock.mock.calls[0][2]
const unaryMockType = unaryMockRequest.getType()
const unaryMockBlockIds = unaryMockRequest.getBlockIdsList()
expect(unaryMockType).not.toBeUndefined()
expect(unaryMockBlockIds).not.toBeUndefined()
expect(unaryMockBlockIds.length).toBe(1)
expect(response.events[0]).toStrictEqual(returnedEvents[0])
})
test("GetEventsForHeightRange", async () => {
const unaryMock = jest.fn();
const dateNow = new Date(Date.now())
const returnedEvents = [
{
blockId: "a1b2c3",
blockHeight: 123,
blockTimestamp: dateNow.toISOString(),
type: "MyEvent",
transactionId: "a1b2c3",
transactionIndex: 123,
eventIndex: 456,
payload: {type: "String", value: "Hello, Flow"}
}
]
unaryMock.mockReturnValue({
getResultsList: () => [
{
getBlockId_asU8: () => hexStrToUInt8Array("a1b2c3"),
getBlockHeight: () => 123,
getBlockTimestamp: () => ({
toDate: () => ({
toISOString: () => dateNow.toISOString(),
})
}),
getEventsList: () => ([
{
getType: () => "MyEvent",
getTransactionId_asU8: () => hexStrToUInt8Array("a1b2c3"),
getTransactionIndex: () => 123,
getEventIndex: () => 456,
getPayload_asU8: () => jsonToUInt8Array({type: "String", value: "Hello, Flow"}),
}
])
}
]
});
const response = await sendGetEvents(
await resolve(
await build([
getEventsAtBlockHeightRange("MyEvent", 123, 456),
])
),
{
unary: unaryMock,
node: "localhost:3000"
}
)
expect(unaryMock.mock.calls.length).toEqual(1)
const unaryMockArgs = unaryMock.mock.calls[0]
expect(unaryMockArgs.length).toEqual(3)
const unaryType = unaryMock.mock.calls[0][1]
expect(unaryType).toEqual(AccessAPI.GetEventsForHeightRange)
const unaryMockRequest = unaryMock.mock.calls[0][2]
const unaryMockType = unaryMockRequest.getType()
const unaryMockStartHeight = unaryMockRequest.getStartHeight()
const unaryMockEndHeight = unaryMockRequest.getEndHeight()
expect(unaryMockType).not.toBeUndefined()
expect(unaryMockStartHeight).not.toBeUndefined()
expect(unaryMockEndHeight).not.toBeUndefined()
expect(response.events[0]).toStrictEqual(returnedEvents[0])
})
})