forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitHistoryRepository.test.ts
More file actions
244 lines (227 loc) · 6.28 KB
/
Copy pathsplitHistoryRepository.test.ts
File metadata and controls
244 lines (227 loc) · 6.28 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { afterEach, describe, expect, it, vi } from "vitest";
import { getSplitHistoryRepository } from "./splitHistoryRepository";
import { apiClient } from "../utils/api-client";
describe("splitHistoryRepository", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("fetches every server page and deduplicates before requested pagination", async () => {
const getSpy = vi
.spyOn(apiClient, "get")
.mockResolvedValueOnce({
data: apiResponse(
[
apiItem({
id: "history-new",
description: "API Split New",
totalAmount: 20,
completionTime: "2026-01-03T00:00:00.000Z",
}),
apiItem({
id: "history-old",
description: "API Split Old",
totalAmount: 12,
completionTime: "2026-01-01T00:00:00.000Z",
}),
],
{ total: 101, page: 1, limit: 100, hasMore: true },
),
})
.mockResolvedValueOnce({
data: apiResponse(
[
apiItem({
id: "history-middle",
description: "API Split Middle",
totalAmount: 16,
completionTime: "2026-01-02T00:00:00.000Z",
}),
apiItem({
id: "history-old",
description: "API Split Old",
totalAmount: 12,
completionTime: "2026-01-01T00:00:00.000Z",
}),
],
{ total: 101, page: 2, limit: 100, hasMore: false },
),
});
const result = await getSplitHistoryRepository().fetchHistory({
statuses: ["active"],
role: "creator",
search: " API Split ",
page: 2,
limit: 1,
});
expect(getSpy).toHaveBeenNthCalledWith(1, "/splits/history", {
params: {
page: 1,
limit: 100,
role: "creator",
search: "API Split",
status: "active",
},
});
expect(getSpy).toHaveBeenNthCalledWith(2, "/splits/history", {
params: {
page: 2,
limit: 100,
role: "creator",
search: "API Split",
status: "active",
},
});
expect(result.data).toHaveLength(1);
expect(result.data[0]).toMatchObject({
id: "history-middle",
title: "API Split Middle",
currency: "EUR",
status: "active",
role: "creator",
});
expect(result.meta).toEqual({
page: 2,
limit: 1,
totalItems: 3,
totalPages: 3,
hasMore: true,
});
expect(result.summary).toEqual({
totalAmount: 48,
average: 16,
counts: { active: 3, completed: 0, cancelled: 0 },
});
});
it("combines multi-status results before global sorting and pagination", async () => {
const getSpy = vi
.spyOn(apiClient, "get")
.mockResolvedValueOnce({
data: apiResponse([
apiItem({ id: "active", status: "active", totalAmount: 10 }),
]),
})
.mockResolvedValueOnce({
data: apiResponse([
apiItem({
id: "completed",
status: "completed",
totalAmount: 40,
}),
]),
});
const result = await getSplitHistoryRepository().fetchHistory({
statuses: ["active", "completed"],
sort: "amount-desc",
page: 1,
limit: 1,
});
expect(getSpy).toHaveBeenNthCalledWith(1, "/splits/history", {
params: { page: 1, limit: 100, status: "active" },
});
expect(getSpy).toHaveBeenNthCalledWith(2, "/splits/history", {
params: { page: 1, limit: 100, status: "completed" },
});
expect(result.data.map((split) => split.id)).toEqual(["completed"]);
expect(result.meta).toEqual({
page: 1,
limit: 1,
totalItems: 2,
totalPages: 2,
hasMore: true,
});
expect(result.summary.counts).toEqual({
active: 1,
completed: 1,
cancelled: 0,
});
});
it("maps cancelled UI filters to archived API records", async () => {
const getSpy = vi.spyOn(apiClient, "get").mockResolvedValueOnce({
data: apiResponse([
apiItem({
id: "history-2",
splitId: "split-2",
role: "participant",
finalAmount: -24,
status: "archived",
description: undefined,
preferredCurrency: undefined,
totalAmount: 24,
isArchived: true,
}),
]),
});
const result = await getSplitHistoryRepository().fetchHistory({
statuses: ["cancelled"],
});
expect(getSpy).toHaveBeenCalledWith("/splits/history", {
params: { page: 1, limit: 100, status: "archived" },
});
expect(result.data[0]).toMatchObject({
title: "Split split-2",
status: "cancelled",
totalAmount: 24,
});
});
it("propagates API failures so the page can render its error state", async () => {
vi.spyOn(apiClient, "get").mockRejectedValueOnce(new Error("boom"));
await expect(
getSplitHistoryRepository().fetchHistory({ statuses: ["active"] }),
).rejects.toThrow("boom");
});
it("returns an empty page without an API call when no statuses are selected", async () => {
const getSpy = vi.spyOn(apiClient, "get");
const result = await getSplitHistoryRepository().fetchHistory({
statuses: [],
page: 3,
limit: 10,
});
expect(getSpy).not.toHaveBeenCalled();
expect(result.data).toEqual([]);
expect(result.meta).toEqual({
page: 3,
limit: 10,
totalItems: 0,
totalPages: 1,
hasMore: false,
});
expect(result.summary).toEqual({
totalAmount: 0,
average: 0,
counts: { active: 0, completed: 0, cancelled: 0 },
});
});
});
function apiResponse(
data: ReturnType<typeof apiItem>[],
overrides: Partial<{
total: number;
page: number;
limit: number;
hasMore: boolean;
}> = {},
) {
return {
data,
total: data.length,
page: 1,
limit: 100,
hasMore: false,
...overrides,
};
}
function apiItem(overrides: Record<string, unknown> = {}) {
return {
id: "history-1",
splitId: "split-1",
role: "creator",
finalAmount: 12,
status: "active",
description: "API Split",
preferredCurrency: "EUR",
totalAmount: 12,
completionTime: "2026-01-01T00:00:00.000Z",
isArchived: false,
...overrides,
};
}