forked from muttoni/fcl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-transaction.test.js
116 lines (99 loc) · 3.64 KB
/
send-transaction.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
import {AccessAPI} from "@onflow/protobuf"
import {sendTransaction} from "./send-transaction.js"
import {build} from "../build/build.js"
import {transaction} from "../build/build-transaction.js"
import {proposer} from "../build/build-proposer.js"
import {payer} from "../build/build-payer.js"
import {ref} from "../build/build-ref.js"
import {authorizations} from "../build/build-authorizations.js"
import {preSendCheck} from "../build/build-pre-send-check.js"
import {voucherToTxId} from "../resolve/voucher.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("Transaction", () => {
test("SendTransaction", async () => {
const unaryMock = jest.fn();
const returnedTransactionId = "a1b2c3"
unaryMock.mockReturnValue({
getId_asU8: () => hexStrToUInt8Array("a1b2c3")
});
const response = await sendTransaction(
await resolve(
await build([
transaction`cadence transaction`,
proposer({
addr: "f8d6e0586b0a20c7",
keyId: 1,
sequenceNum: 123,
signingFunction: () => ({
addr: "f8d6e0586b0a20c7",
keyId: 1,
signature: "abc123"
}),
resolve: null,
roles: { proposer: true, authorizer: true, payer: true, param: false },
}),
payer({
addr: "f8d6e0586b0a20c7",
keyId: 1,
sequenceNum: 123,
signingFunction: () => ({
addr: "f8d6e0586b0a20c7",
keyId: 1,
signature: "abc123"
}),
resolve: null,
roles: { proposer: true, authorizer: true, payer: true, param: false },
}),
authorizations([
{
addr: "f8d6e0586b0a20c7",
keyId: 1,
sequenceNum: 123,
signingFunction: () => ({
addr: "f8d6e0586b0a20c7",
keyId: 1,
signature: "abc123"
}),
resolve: null,
roles: { proposer: true, authorizer: true, payer: true, param: false },
}
]),
ref("abc123"),
preSendCheck(async voucher => {
voucherToTxId(voucher)
}),
])
),
{
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.SendTransaction)
const unaryMockRequest = unaryMock.mock.calls[0][2]
expect(unaryMockRequest).not.toBeUndefined()
expect(response.transactionId).toBe(returnedTransactionId)
})
})