mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-16 03:00:17 -06:00
Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com> Co-authored-by: Piyush Gupta <56182734+gupta-piyush19@users.noreply.github.com> Co-authored-by: Victor Hugo dos Santos <115753265+victorvhs017@users.noreply.github.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com> Co-authored-by: Matti Nannt <matti@formbricks.com> Co-authored-by: Matti Nannt <mail@matthiasnannt.com> Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { getServerSession } from "next-auth";
|
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
|
import { DEFAULT_LOCALE } from "@/lib/constants";
|
|
import { getUserLocale } from "@/lib/user/service";
|
|
import { findMatchingLocale } from "@/lib/utils/locale";
|
|
import { authOptions } from "@/modules/auth/lib/authOptions";
|
|
import { getLocale } from "./language";
|
|
|
|
// Mock dependencies
|
|
vi.mock("next-auth", () => ({
|
|
getServerSession: vi.fn(),
|
|
}));
|
|
vi.mock("@/lib/user/service", () => ({
|
|
getUserLocale: vi.fn(),
|
|
}));
|
|
vi.mock("@/lib/utils/locale", () => ({
|
|
findMatchingLocale: vi.fn(),
|
|
}));
|
|
vi.mock("@/modules/auth/lib/authOptions", () => ({
|
|
authOptions: vi.fn(),
|
|
}));
|
|
|
|
describe("lingodotdev getLocale", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
test("should return user locale when session exists and user has id", async () => {
|
|
const mockSession = {
|
|
user: {
|
|
id: "user-123",
|
|
},
|
|
};
|
|
const userLocale = "de-DE";
|
|
|
|
vi.mocked(getServerSession).mockResolvedValue(mockSession);
|
|
vi.mocked(getUserLocale).mockResolvedValue(userLocale);
|
|
|
|
const result = await getLocale();
|
|
|
|
expect(getServerSession).toHaveBeenCalledWith(authOptions);
|
|
expect(getUserLocale).toHaveBeenCalledWith("user-123");
|
|
expect(findMatchingLocale).not.toHaveBeenCalled();
|
|
expect(result).toBe(userLocale);
|
|
});
|
|
|
|
test("should use findMatchingLocale when no session exists", async () => {
|
|
const matchingLocale = "fr-FR";
|
|
|
|
vi.mocked(getServerSession).mockResolvedValue(null);
|
|
vi.mocked(findMatchingLocale).mockResolvedValue(matchingLocale);
|
|
|
|
const result = await getLocale();
|
|
|
|
expect(getServerSession).toHaveBeenCalledWith(authOptions);
|
|
expect(getUserLocale).not.toHaveBeenCalled();
|
|
expect(findMatchingLocale).toHaveBeenCalled();
|
|
expect(result).toBe(matchingLocale);
|
|
});
|
|
|
|
test("should return DEFAULT_LOCALE when getUserLocale returns undefined", async () => {
|
|
const mockSession = {
|
|
user: {
|
|
id: "user-123",
|
|
},
|
|
};
|
|
|
|
vi.mocked(getServerSession).mockResolvedValue(mockSession);
|
|
vi.mocked(getUserLocale).mockResolvedValue(undefined);
|
|
|
|
const result = await getLocale();
|
|
|
|
expect(getServerSession).toHaveBeenCalledWith(authOptions);
|
|
expect(getUserLocale).toHaveBeenCalledWith("user-123");
|
|
expect(result).toBe(DEFAULT_LOCALE);
|
|
});
|
|
});
|