mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-16 23:35:15 -05:00
fix: localize text validators message (#6719)
This commit is contained in:
@@ -1,10 +1,33 @@
|
||||
import { expect, test } from "vitest";
|
||||
import { expect, test, vi } from "vitest";
|
||||
import enUS from "~/lang/messages/en-US.json";
|
||||
|
||||
import { required, email, whitespace, url, minLength, maxLength } from "./inputs";
|
||||
|
||||
vi.mock("~/composables/use-global-i18n", () => {
|
||||
const interpolate = (msg: string, params?: Record<string, unknown>) => {
|
||||
if (!params) return msg;
|
||||
return msg
|
||||
.replace("{min}", String(params.min ?? ""))
|
||||
.replace("{max}", String(params.max ?? ""));
|
||||
};
|
||||
|
||||
const t = (key: string, params?: Record<string, unknown>) => {
|
||||
const parts = key.split(".");
|
||||
let acc: any = enUS as any;
|
||||
for (const p of parts) acc = acc?.[p];
|
||||
const msg: string | undefined = acc;
|
||||
return interpolate(msg ?? key, params);
|
||||
};
|
||||
|
||||
return { useGlobalI18n: () => ({ t }) };
|
||||
});
|
||||
|
||||
export { scorePassword } from "./password";
|
||||
|
||||
// Tests
|
||||
|
||||
test("validator required", () => {
|
||||
const falsey = "This Field is Required";
|
||||
const falsey = enUS.validators.required;
|
||||
expect(required("123")).toBe(true);
|
||||
expect(required("")).toBe(falsey);
|
||||
expect(required(undefined)).toBe(falsey);
|
||||
@@ -14,7 +37,7 @@ test("validator required", () => {
|
||||
const nulls = [undefined, null];
|
||||
|
||||
test("validator email", () => {
|
||||
const falsey = "Email Must Be Valid";
|
||||
const falsey = enUS.validators["invalid-email"];
|
||||
expect(email("123")).toBe(falsey);
|
||||
expect(email("email@example.com")).toBe(true);
|
||||
|
||||
@@ -24,7 +47,7 @@ test("validator email", () => {
|
||||
});
|
||||
|
||||
test("whitespace", () => {
|
||||
const falsey = "No Whitespace Allowed";
|
||||
const falsey = enUS.validators["no-whitespace"];
|
||||
expect(whitespace("123")).toBe(true);
|
||||
expect(whitespace(" ")).toBe(falsey);
|
||||
expect(whitespace("123 123")).toBe(falsey);
|
||||
@@ -35,7 +58,7 @@ test("whitespace", () => {
|
||||
});
|
||||
|
||||
test("url", () => {
|
||||
const falsey = "Must Be A Valid URL";
|
||||
const falsey = enUS.validators["invalid-url"];
|
||||
expect(url("https://example.com")).toBe(true);
|
||||
expect(url("")).toBe(falsey);
|
||||
|
||||
@@ -46,7 +69,7 @@ test("url", () => {
|
||||
|
||||
test("minLength", () => {
|
||||
const min = 3;
|
||||
const falsey = `Must Be At Least ${min} Characters`;
|
||||
const falsey = enUS.validators["min-length"].replace("{min}", String(min));
|
||||
const fn = minLength(min);
|
||||
expect(fn("123")).toBe(true);
|
||||
expect(fn("12")).toBe(falsey);
|
||||
@@ -59,7 +82,7 @@ test("minLength", () => {
|
||||
|
||||
test("maxLength", () => {
|
||||
const max = 3;
|
||||
const falsey = `Must Be At Most ${max} Characters`;
|
||||
const falsey = enUS.validators["max-length"].replace("{max}", String(max));
|
||||
const fn = maxLength(max);
|
||||
expect(fn("123")).toBe(true);
|
||||
expect(fn("1234")).toBe(falsey);
|
||||
|
||||
Reference in New Issue
Block a user