forked from Supercolony-net/typechain-polkadot
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgeneral.test.ts
102 lines (77 loc) · 2.64 KB
/
general.test.ts
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
import Contract from '../generated/contracts/my_psp34_events';
import Constructors from '../generated/constructors/my_psp34_events';
import {ApiPromise, Keyring} from "@polkadot/api";
import type {KeyringPair} from "@polkadot/keyring/types";
import {GetAccounts} from "../config";
import {IdBuilder} from "../generated/types-returns/my_psp34_events";
import {IdBuilder as IdBuilderArgs} from "../generated/types-arguments/my_psp34_events";
import {ReturnNumber} from "@727-ventures/typechain-types";
describe("Events", () => {
let api: ApiPromise;
let contract: Contract;
let contract_bob: Contract;
let UserAlice: KeyringPair, UserBob: KeyringPair, UserCharlie: KeyringPair;
beforeEach(async () => {
api = await ApiPromise.create();
const accounts = GetAccounts();
UserAlice = accounts.UserAlice;
UserBob = accounts.UserBob;
UserCharlie = accounts.UserCharlie;
const factory = new Constructors(api, UserAlice);
const {address} = await factory.new();
contract = new Contract(address, UserAlice, api);
contract_bob = new Contract(address, UserBob, api);
});
afterEach(async () => {
await api.disconnect();
});
jest.setTimeout(10000);
test("Subscription to events works", async () => {
let eventsCount = 0;
const eventsToBeSent = [
{
from: null,
to: UserAlice.address,
id: IdBuilder.U128(new ReturnNumber(1)),
},
{
from: UserAlice.address,
to: UserBob.address,
id: IdBuilder.U128(new ReturnNumber(1)),
}
];
contract.events.subscribeOnTransferEvent((event) => {
expect(event).toMatchObject(eventsToBeSent[eventsCount]!);
eventsCount++;
});
await contract.tx.mint(UserAlice.address, IdBuilderArgs.U128(1));
await new Promise((resolve) => setTimeout(resolve, 2000));
await contract.tx.transfer(UserBob.address, IdBuilderArgs.U128(1), []);
await new Promise((resolve) => setTimeout(resolve, 2000));
expect(eventsCount).toBe(eventsToBeSent.length);
});
test("Test events on submittables", async () => {
const result = await contract.tx.mint(UserAlice.address, IdBuilderArgs.U32(1));
expect(result.events!.length).toBe(1);
expect(result.events![0]).toMatchObject({
name: 'Transfer',
args: {
from: null,
to: UserAlice.address.toString(),
id: IdBuilder.U32(1),
},
});
});
test('Test events on submittables with ReturnNumber', async () => {
const result2 = await contract.tx.mint(UserAlice.address, IdBuilderArgs.U128(1));
expect(result2.events!.length).toBe(1);
expect(result2.events![0]).toMatchObject({
name: 'Transfer',
args: {
from: null,
to: UserAlice.address.toString(),
id: IdBuilder.U128(new ReturnNumber(1)),
},
});
});
});