forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-client.spec.ts
More file actions
235 lines (211 loc) · 7.66 KB
/
Copy pathapi-client.spec.ts
File metadata and controls
235 lines (211 loc) · 7.66 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
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import axios, { AxiosError, type InternalAxiosRequestConfig } from "axios";
import {
DEFAULT_API_REQUEST_ERROR,
DEFAULT_NETWORK_CONNECTIVITY_ERROR,
} from "../constants/api";
import {
apiClient,
ApiError,
buildPathVariants,
fetchSplitById,
getApiErrorMessage,
getApiFieldErrors,
normalizeApiError,
} from "./api-client";
describe("buildPathVariants (route resolution)", () => {
it("on unversioned /api, prepends /v1 before bare controller path, then unversioned", () => {
const paths = buildPathVariants("/splits/abc", false, "/api");
expect(paths).toEqual(["/v1/splits/abc", "/splits/abc"]);
});
it("on versioned /api/v1, does not inject an extra /v1 prefix", () => {
const paths = buildPathVariants("/splits/abc", false, "/api/v1");
expect(paths).toEqual(["/splits/abc"]);
});
it("on unversioned base, includeControllerApiPrefix interleaves /api before bare paths", () => {
const paths = buildPathVariants("/receipts/1", true, "/api");
expect(paths).toEqual([
"/v1/api/receipts/1",
"/v1/receipts/1",
"/api/receipts/1",
"/receipts/1",
]);
});
it("on versioned base, includeControllerApiPrefix only toggles /api, not an extra /v1", () => {
const paths = buildPathVariants("/receipts/1", true, "/api/v1");
expect(paths).toEqual(["/api/receipts/1", "/receipts/1"]);
});
it("normalizes endpoint without a leading slash", () => {
const paths = buildPathVariants("splits/x", false, "/api");
expect(paths).toEqual(["/v1/splits/x", "/splits/x"]);
});
});
function axiosError(
config: { status: number; data?: unknown; code?: string; message?: string; response?: boolean },
): AxiosError<unknown> {
const err = new AxiosError(
config.message ?? "err",
config.code,
{ url: "/t" } as unknown as InternalAxiosRequestConfig,
);
if (config.response !== false) {
err.response = {
status: config.status,
data: config.data ?? {},
statusText: "",
config: { url: "/t" } as unknown as InternalAxiosRequestConfig,
headers: {},
};
}
return err;
}
describe("normalizeApiError and helpers", () => {
it("keeps ApiError as-is (identity)", () => {
const e = new ApiError({
message: "custom",
statusCode: 400,
fieldErrors: { title: "x" },
isNetworkError: false,
});
expect(normalizeApiError(e)).toBe(e);
});
it("wraps a non-axios error with the default request message", () => {
const n = normalizeApiError(new TypeError("oops"));
expect(n).toBeInstanceOf(ApiError);
expect(n.message).toBe(DEFAULT_API_REQUEST_ERROR);
expect(n.isNetworkError).toBe(false);
expect(n.fieldErrors).toEqual({});
});
it("uses the first string message and maps field keys from text", () => {
const err = axiosError({
status: 400,
data: {
message: "currency must be a valid code",
error: "ignored when message exists",
},
});
const n = normalizeApiError(err);
expect(n.message).toContain("currency");
expect(n.fieldErrors.currency).toBe("currency must be a valid code");
});
it("extracts nested and array messages for field heuristics", () => {
const err = axiosError({
status: 400,
data: {
message: [
{ msg: "participant 1 is missing" },
{ msg: "total amount is wrong" },
],
},
});
const n = normalizeApiError(err);
expect(n.fieldErrors.participants).toMatch(/participant/);
expect(n.fieldErrors.totalAmount).toMatch(/total/);
});
it("marks missing response as a network error; uses default when there is no extractable message", () => {
const e = new AxiosError("");
e.config = { url: "/a" } as never;
e.response = undefined;
const n = normalizeApiError(e);
expect(n.isNetworkError).toBe(true);
expect(n.message).toBe(DEFAULT_NETWORK_CONNECTIVITY_ERROR);
});
it("prefers axiosError.message for no-response when non-empty, but still sets isNetworkError", () => {
const e = new AxiosError("fail", "ERR", { url: "/a" } as never);
e.response = undefined;
const n = normalizeApiError(e);
expect(n.isNetworkError).toBe(true);
expect(n.message).toBe("fail");
});
it("uses the network default for ECONNABORTED when no user-facing message is extracted", () => {
const e = new AxiosError("");
e.config = { url: "/a" } as never;
e.code = "ECONNABORTED";
e.response = undefined;
const n = normalizeApiError(e);
expect(n.isNetworkError).toBe(true);
expect(n.message).toBe(DEFAULT_NETWORK_CONNECTIVITY_ERROR);
});
it("exposes the same data via getApiErrorMessage and getApiFieldErrors", () => {
const err = axiosError({
status: 400,
data: { message: "wallet is invalid" },
});
expect(getApiErrorMessage(err)).toBe(
(normalizeApiError(err) as ApiError).message,
);
expect(getApiFieldErrors(err).walletAddress).toBe("wallet is invalid");
});
});
describe("requestWithFallback (mocked transport)", () => {
// Vitest's spyOn key constraint does not list axios' `request`; runtime is correct.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let requestSpy: any;
const minimalSplit = {
id: "1",
totalAmount: 0,
amountPaid: 0,
status: "active" as const,
createdAt: "",
updatedAt: "",
participants: [] as { id: string; userId: string; amountOwed: number; amountPaid: number; status: "pending" }[],
};
beforeEach(() => {
requestSpy = vi.spyOn(apiClient, "request");
});
afterEach(() => {
requestSpy?.mockRestore();
});
it("retries the next path variant on 404 and returns when a later call succeeds", async () => {
const variants = buildPathVariants("/splits/fallback-id", false);
const first = variants[0]!;
const last = variants[variants.length - 1]!;
requestSpy.mockImplementation(
async (conf: InternalAxiosRequestConfig) => {
if (String(conf.url) === first) {
throw axiosError({ status: 404, data: { message: "nope" } });
}
return { data: minimalSplit };
},
);
const data = await fetchSplitById("fallback-id");
expect(data).toEqual(minimalSplit);
expect(requestSpy).toHaveBeenCalled();
const urls = requestSpy.mock.calls.map(
(c: [{ url?: string }]) => c[0].url,
) as (string | undefined)[];
expect(urls[0]).toBe(first);
expect(urls.at(-1)).toBe(last);
expect(requestSpy).toHaveBeenCalledTimes(2);
});
it("stops and throws on a non-404 (does not exhaust variants for retry)", async () => {
const variants = buildPathVariants("/splits/err-id", false);
requestSpy.mockImplementation(
async (conf: InternalAxiosRequestConfig) => {
if (String(conf.url) === variants[0]!) {
throw axiosError({ status: 500, data: { message: "server" } });
}
return { data: minimalSplit };
},
);
await expect(fetchSplitById("err-id")).rejects.toMatchObject({
message: "server",
statusCode: 500,
});
expect(requestSpy).toHaveBeenCalledTimes(1);
});
it("throws a normalized error after 404 on every variant", async () => {
const variants = buildPathVariants("/splits/404-only", false);
requestSpy.mockImplementation(async () => {
throw axiosError({ status: 404, data: { message: "missing" } });
});
await expect(fetchSplitById("404-only")).rejects.toBeInstanceOf(ApiError);
expect(requestSpy).toHaveBeenCalledTimes(variants.length);
});
});
describe("axios.isAxiosError is honored", () => {
it("is true for our axios error factory", () => {
const e = axiosError({ status: 400, data: {} });
expect(axios.isAxiosError(e)).toBe(true);
});
});