/
blocks.test.ts
94 lines (68 loc) · 3 KB
/
blocks.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
import "jest-extended";
import { app } from "@arkecosystem/core-container";
import { Peer } from "@arkecosystem/core-p2p/src/peer";
import nock from "nock";
import { sendRequest } from "./__support__/request";
import { setUp, tearDown } from "./__support__/setup";
jest.mock("is-reachable", () => jest.fn(async () => true));
let peerMock;
let mockHost;
beforeAll(async () => {
await setUp();
peerMock = new Peer("1.0.0.99", 4003); // @NOTE: we use the Public API port
app.resolvePlugin("p2p")
.getStorage()
.setPeer(peerMock);
nock("http://localhost", { allowUnmocked: true });
mockHost = nock(peerMock.url);
});
afterAll(async () => await tearDown());
afterEach(async () => nock.cleanAll());
describe("Blocks", () => {
describe("POST blocks.latest", () => {
it("should get the latest block", async () => {
mockHost
.get("/api/blocks")
.query({ orderBy: "height:desc", limit: 1 })
.reply(200, { data: [{ id: "123" }] }, peerMock.headers);
const response = await sendRequest("blocks.latest");
expect(response.body.result.id).toBe("123");
});
it("should not find the latest block", async () => {
mockHost.get("/api/blocks").reply(404, {}, peerMock.headers);
const response = await sendRequest("blocks.latest");
expect(response.body.error.message).toBe("Latest block could not be found.");
});
});
describe("POST blocks.info", () => {
it("should get the block information", async () => {
mockHost.get("/api/blocks/123").reply(200, { data: { id: "123" } }, peerMock.headers);
const response = await sendRequest("blocks.info", {
id: "123",
});
expect(response.body.result.id).toBe("123");
});
it("should fail to get the block information", async () => {
const response = await sendRequest("blocks.info", { id: "123" });
expect(response.body.error.code).toBe(404);
expect(response.body.error.message).toBe("Block 123 could not be found.");
});
});
describe("POST blocks.transactions", () => {
it("should get the block transactions", async () => {
mockHost
.get("/api/blocks/123/transactions")
.query({ orderBy: "timestamp:desc" })
.reply(200, { meta: { totalCount: 1 }, data: [{ id: "123" }, { id: "123" }] }, peerMock.headers);
const response = await sendRequest("blocks.transactions", {
id: "123",
});
expect(response.body.result.data).toHaveLength(2);
});
it("should fail to get the block transactions", async () => {
const response = await sendRequest("blocks.transactions", { id: "123" });
expect(response.body.error.code).toBe(404);
expect(response.body.error.message).toBe("Block 123 could not be found.");
});
});
});