mirror of
https://github.com/Athou/commafeed.git
synced 2026-09-24 21:15:13 +00:00
Use typed backend errors for translations
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { i18n } from "@lingui/core"
|
||||
import type { AxiosError } from "axios"
|
||||
import { describe, expect, it } from "vitest"
|
||||
import { loginErrorToStrings } from "./client"
|
||||
import { afterEach, describe, expect, it, vi } from "vitest"
|
||||
import { errorToStrings } from "./client"
|
||||
|
||||
const axiosError = (status: number, data: unknown) =>
|
||||
({
|
||||
@@ -8,16 +9,28 @@ const axiosError = (status: number, data: unknown) =>
|
||||
response: { status, data },
|
||||
}) as AxiosError
|
||||
|
||||
describe("loginErrorToStrings", () => {
|
||||
it("uses the translated message for authentication errors", () => {
|
||||
const error = axiosError(401, { message: "wrong username or password" })
|
||||
describe("errorToStrings", () => {
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
expect(loginErrorToStrings(error, "Translated authentication error")).toEqual(["Translated authentication error"])
|
||||
it("translates known application error types", () => {
|
||||
vi.spyOn(i18n, "_").mockReturnValue("Translated authentication error")
|
||||
const error = axiosError(401, {
|
||||
type: "WRONG_USERNAME_OR_PASSWORD",
|
||||
message: "wrong username or password",
|
||||
})
|
||||
|
||||
expect(errorToStrings(error)).toEqual(["Translated authentication error"])
|
||||
})
|
||||
|
||||
it("preserves backend messages for unexpected errors", () => {
|
||||
const error = axiosError(500, { message: "unexpected error" })
|
||||
|
||||
expect(loginErrorToStrings(error, "Translated authentication error")).toEqual(["unexpected error"])
|
||||
expect(errorToStrings(error)).toEqual(["unexpected error"])
|
||||
})
|
||||
|
||||
it("preserves backend messages for unknown application error types", () => {
|
||||
const error = axiosError(400, { type: "UNKNOWN_ERROR", message: "unknown error" })
|
||||
|
||||
expect(errorToStrings(error)).toEqual(["unknown error"])
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user