All files / packages/app/utils i18n.test.ts

0% Statements 0/25
100% Branches 0/0
0% Functions 0/9
0% Lines 0/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                     
import enDefault from "@ra/web/public/locales/en/default.json" with { type: "json" };
import ruDefault from "@ra/web/public/locales/ru/default.json" with { type: "json" };
import { createInstance } from "i18next";
import { expect, test } from "vitest";
 
import { getValidatorMessageFactory } from "~app/utils/i18n";
import {
	emailSchema,
	passwordSchema,
	peerNameSchema,
	priceSchema,
} from "~app/utils/validation";
 
test.describe("Zod validation", () => {
	test.describe("translates", async () => {
		const i18n = createInstance();
		await i18n.init({
			lng: "en",
			resources: { en: { default: enDefault }, ru: { default: ruDefault } },
			defaultNS: "default",
		});
		const validatorMessageEn = getValidatorMessageFactory(i18n, "en");
		const validatorMessageRu = getValidatorMessageFactory(i18n, "ru");
 
		test("regular translation", () => {
			expect(
				priceSchema.safeParse(0, { error: validatorMessageEn }).error?.issues[0]
					?.message,
			).toBe("Price should be non-zero");
		});
 
		test("with parameter", () => {
			expect(
				passwordSchema.safeParse("x", { error: validatorMessageEn }).error
					?.issues[0]?.message,
			).toBe("Minimal length for password is 6");
		});
 
		test("with target", () => {
			expect(
				peerNameSchema.safeParse("", { error: validatorMessageEn }).error
					?.issues[0]?.message,
			).toBe("Minimal length for peer name is 1");
		});
 
		test("non-base language", () => {
			expect(
				priceSchema.safeParse(0.001, { error: validatorMessageRu }).error
					?.issues[0]?.message,
			).toBe("Цена: не более 2 знаков после запятой");
		});
	});
 
	test.describe("fallbacks", () => {
		test("falls back to english in case english is found", async () => {
			const i18n = createInstance();
			await i18n.init({
				lng: "en",
				resources: { en: { default: enDefault } },
				defaultNS: "default",
			});
			const validatorMessageRu = getValidatorMessageFactory(i18n, "ru");
			expect(
				emailSchema.safeParse("not-an-email", { error: validatorMessageRu })
					.error?.issues[0]?.message,
			).toBe("Invalid email address");
		});
 
		test("falls back to default in case no resource is found", async () => {
			const i18n = createInstance();
			await i18n.init({
				lng: "en",
				defaultNS: "default",
			});
			const validatorMessageRu = getValidatorMessageFactory(i18n, "ru");
			expect(
				emailSchema.safeParse("not-an-email", { error: validatorMessageRu })
					.error?.issues[0]?.message,
			).toBe("Invalid input");
		});
	});
});