forked from Split-Naira/SplitNaira
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions.test.ts
More file actions
167 lines (135 loc) · 5.31 KB
/
Copy pathtransactions.test.ts
File metadata and controls
167 lines (135 loc) · 5.31 KB
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
import { describe, it, expect, beforeAll, vi } from "vitest";
import request from "supertest";
const mockTransactionRecords = [
{
id: "tx-1",
roundId: "round-1",
recipient: "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF",
amount: "1000",
token: "CAS3...",
timestamp: Math.floor(Date.now() / 1000) - 3600,
txHash: "validhash123",
status: "completed"
}
];
const mockGetMany = vi.fn().mockResolvedValue(mockTransactionRecords);
const mockFindOneBy = vi.fn().mockResolvedValue(null);
const mockFindBy = vi.fn().mockResolvedValue(mockTransactionRecords);
const mockFind = vi.fn().mockImplementation((options) => {
const optionsStr = JSON.stringify(options || {});
if (optionsStr.includes("nonexistent")) {
return [];
}
return mockTransactionRecords;
});
const mockQueryBuilder = {
andWhere: vi.fn().mockReturnThis(),
orderBy: vi.fn().mockReturnThis(),
skip: vi.fn().mockReturnThis(),
take: vi.fn().mockReturnThis(),
getMany: mockGetMany
};
vi.mock("../services/database.js", () => ({
getDataSource: () => ({
getRepository: () => ({
createQueryBuilder: () => mockQueryBuilder,
findOneBy: mockFindOneBy,
findBy: mockFindBy,
find: mockFind
})
}),
initDatabase: async () => {},
closeDatabase: async () => {}
}));
import { app } from "../index.js";
describe("Transaction History API", () => {
beforeAll(async () => {
// Note: This test assumes PayoutHistoryService is properly initialized
// In a real scenario, you'd want to seed test data
});
describe("GET /transactions/history", () => {
it("should return transaction history with default pagination", async () => {
const response = await request(app)
.get("/transactions/history");
expect(response.status).toBe(200);
expect(response.body).toHaveProperty("transactions");
expect(response.body).toHaveProperty("total");
expect(response.body).toHaveProperty("limit");
expect(response.body).toHaveProperty("offset");
expect(Array.isArray(response.body.transactions)).toBe(true);
});
it("should filter by wallet address", async () => {
const walletAddress = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
const response = await request(app)
.get(`/transactions/history?walletAddress=${walletAddress}`);
expect(response.status).toBe(200);
expect(response.body.transactions).toBeDefined();
});
it("should filter by status", async () => {
const response = await request(app)
.get("/transactions/history?status=completed");
expect(response.status).toBe(200);
expect(response.body.transactions).toBeDefined();
});
it("should apply pagination", async () => {
const response = await request(app)
.get("/transactions/history?limit=5&offset=0");
expect(response.status).toBe(200);
expect(response.body.limit).toBe(5);
expect(response.body.offset).toBe(0);
});
it("should filter by date range", async () => {
const startDate = Math.floor(Date.now() / 1000) - 86400; // 24 hours ago
const endDate = Math.floor(Date.now() / 1000);
const response = await request(app)
.get(`/transactions/history?startDate=${startDate}&endDate=${endDate}`);
expect(response.status).toBe(200);
expect(response.body.transactions).toBeDefined();
});
it("should reject invalid status value", async () => {
const response = await request(app)
.get("/transactions/history?status=invalid");
expect(response.status).toBe(400);
});
it("should reject invalid limit value", async () => {
const response = await request(app)
.get("/transactions/history?limit=1000");
expect(response.status).toBe(400);
});
it("should reject invalid wallet address format", async () => {
const response = await request(app)
.get("/transactions/history?walletAddress=INVALID");
expect(response.status).toBe(400);
});
});
describe("GET /transactions/:txHash", () => {
it("should return 404 for non-existent transaction", async () => {
const response = await request(app)
.get("/transactions/nonexistenthash123");
expect(response.status).toBe(404);
});
it("should reject empty transaction hash", async () => {
const response = await request(app)
.get("/transactions/");
// Should hit the not found handler or return 404
expect([404, 400]).toContain(response.status);
});
});
describe("GET /transactions/recipient/:walletAddress", () => {
it("should return transactions for valid wallet address", async () => {
const walletAddress = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
const response = await request(app)
.get(`/transactions/recipient/${walletAddress}`);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty("transactions");
expect(response.body).toHaveProperty("total");
expect(response.body.walletAddress).toBe(walletAddress);
expect(Array.isArray(response.body.transactions)).toBe(true);
});
it("should reject invalid wallet address format", async () => {
const response = await request(app)
.get("/transactions/recipient/INVALID");
expect(response.status).toBe(400);
});
});
});