Files
jobtrackingapp/job-tracker-ui/src/api-error-localisation.test.ts
T

25 lines
1.4 KiB
TypeScript

import { AxiosError, CanceledError } from "axios";
const { getApiErrorMessage } = jest.requireActual<typeof import("./api")>("./api");
test.each(["Failed to update company.", "Kunne ikke oppdatere selskap."])("transport failures use the caller's localized fallback: %s", (fallback) => {
for (const message of ["Request failed with status code 500", "Network Error", "timeout of 10000ms exceeded"]) {
expect(getApiErrorMessage(new AxiosError(message), fallback)).toBe(fallback);
}
});
test("structured validation and sanitized server explanations remain available", () => {
for (const data of [{ detail: "Review the company name." }, { errors: { name: ["Review the company name."] } }]) {
expect(getApiErrorMessage(Object.assign(new AxiosError("Request failed with status code 400"), { response: { data } }), "Localized fallback")).toBe("Review the company name.");
}
expect(getApiErrorMessage({ response: { data: "<html>proxy error</html>" } }, "Localized fallback")).toBe("Localized fallback");
});
test("explicit session cancellation explanations remain intact", () => {
expect(getApiErrorMessage(new CanceledError("This page belongs to an earlier session."), "Localized fallback")).toBe("This page belongs to an earlier session.");
});
test("ordinary application errors retain their explanation", () => {
expect(getApiErrorMessage(new Error("The draft changed."), "Localized fallback")).toBe("The draft changed.");
});