-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathhandlers.ts
117 lines (104 loc) · 3.51 KB
/
handlers.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import path from 'node:path';
import { createApolloFetch } from 'apollo-fetch';
import { expect } from 'chai';
import { filesystem, patching, system } from 'gluegun';
import { ethers } from 'hardhat';
const srcDir = path.join(__dirname, '..');
const fetchSubgraphs = createApolloFetch({ uri: 'http://localhost:8030/graphql' });
const fetchSubgraph = createApolloFetch({
uri: 'http://localhost:8000/subgraphs/name/test/basic-event-handlers',
});
const waitForSubgraphToBeSynced = async () =>
new Promise((resolve, reject) => {
// Wait for 10s
const deadline = Date.now() + 10 * 1000;
const checkSubgraphSynced = async () => {
if (Date.now() > deadline) {
reject('Timeout while waiting for the subgraph to be synced');
}
// Query the subgraph meta data for the indexing status
const result = await fetchSubgraphs({
query: `
{
statuses: indexingStatusesForSubgraphName(
subgraphName: "test/basic-event-handlers"
) {
synced
}
}
`,
});
if (result.data.statuses[0].synced) {
setTimeout(resolve, 1000);
} else {
setTimeout(checkSubgraphSynced, 500);
}
};
setTimeout(checkSubgraphSynced, 0);
});
describe('Basic event handlers', () => {
// Deploy the subgraph once before all tests
before(async () => {
const GravatarRegistry = await ethers.getContractFactory('GravatarRegistry');
const registry = await GravatarRegistry.deploy();
const accounts = await ethers.getSigners();
filesystem.copy('template-subgraph.yaml', 'subgraph.yaml', { overwrite: true });
// Insert its address into subgraph manifest
await patching.replace(
path.join(srcDir, 'subgraph.yaml'),
'DEPLOYED_CONTRACT_ADDRESS',
String(registry.target),
);
await registry.setMythicalGravatar();
await registry.createGravatar('Carl', 'https://thegraph.com/img/team/team_04.png');
await registry
.connect(accounts[1])
.createGravatar('Lucas', 'https://thegraph.com/img/team/bw_Lucas.jpg');
await registry.connect(accounts[0]).updateGravatarName('Nena');
await registry.connect(accounts[1]).updateGravatarName('Jorge');
// Create and deploy the subgraph
await system.run(`yarn codegen`, { cwd: srcDir });
await system.run(`yarn create-test`, { cwd: srcDir });
await system.run(`yarn deploy-test`, { cwd: srcDir });
// Wait for the subgraph to be indexed
await waitForSubgraphToBeSynced();
});
it('all events are indexed', async () => {
// Query the subgraph for entities
const result = await fetchSubgraph({
query: `
{
newGravatars(orderBy: id) { id displayName imageUrl }
updatedGravatars(orderBy: id) { id displayName imageUrl }
}
`,
});
expect(result.errors).to.be.undefined;
expect(result.data).to.deep.equal({
newGravatars: [
{
displayName: 'Carl',
id: '0x1',
imageUrl: 'https://thegraph.com/img/team/team_04.png',
},
{
displayName: 'Lucas',
id: '0x2',
imageUrl: 'https://thegraph.com/img/team/bw_Lucas.jpg',
},
],
updatedGravatars: [
{
displayName: 'Nena',
id: '0x1',
imageUrl: 'https://thegraph.com/img/team/team_04.png',
},
{
displayName: 'Jorge',
id: '0x2',
imageUrl: 'https://thegraph.com/img/team/bw_Lucas.jpg',
},
],
});
});
});