mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-23 13:48:58 -05:00
test: add comprehensive tests for findLanguageCodesForDuplicateLabels
- Added test coverage for undefined value handling - Tests verify fix for TypeError when language keys are missing - Added test script to types package.json - All 7 tests passing
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
"sideEffects": false,
|
||||
"scripts": {
|
||||
"lint": "eslint . --ext .ts,.js,.tsx,.jsx",
|
||||
"clean": "rimraf node_modules .turbo"
|
||||
"clean": "rimraf node_modules .turbo",
|
||||
"test": "vitest run",
|
||||
"test:coverage": "vitest run --coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "6.14.0",
|
||||
@@ -16,6 +18,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@formbricks/config-typescript": "workspace:*",
|
||||
"@formbricks/database": "workspace:*"
|
||||
"@formbricks/database": "workspace:*",
|
||||
"vitest": "3.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { TI18nString } from "../i18n";
|
||||
import type { TSurveyLanguage } from "./types";
|
||||
import { findLanguageCodesForDuplicateLabels } from "./validation";
|
||||
|
||||
describe("findLanguageCodesForDuplicateLabels", () => {
|
||||
const mockLanguages: TSurveyLanguage[] = [
|
||||
{
|
||||
default: true,
|
||||
enabled: true,
|
||||
language: { id: "en", code: "en", alias: null, createdAt: new Date(), updatedAt: new Date() },
|
||||
},
|
||||
{
|
||||
default: false,
|
||||
enabled: true,
|
||||
language: { id: "es", code: "es", alias: null, createdAt: new Date(), updatedAt: new Date() },
|
||||
},
|
||||
];
|
||||
|
||||
it("should handle undefined language values without throwing", () => {
|
||||
const labels: TI18nString[] = [
|
||||
{ default: "Option 1", es: "Opción 1" },
|
||||
{ default: "Option 2" }, // Missing 'es' key - this was causing the error
|
||||
{ default: "Option 3", es: "Opción 3" },
|
||||
];
|
||||
|
||||
// This should not throw a TypeError
|
||||
expect(() => findLanguageCodesForDuplicateLabels(labels, mockLanguages)).not.toThrow();
|
||||
});
|
||||
|
||||
it("should detect duplicates when labels are the same", () => {
|
||||
const labels: TI18nString[] = [
|
||||
{ default: "Option 1", es: "Opción 1" },
|
||||
{ default: "Option 1", es: "Opción 1" }, // Duplicate
|
||||
{ default: "Option 3", es: "Opción 3" },
|
||||
];
|
||||
|
||||
const result = findLanguageCodesForDuplicateLabels(labels, mockLanguages);
|
||||
expect(result).toContain("default");
|
||||
expect(result).toContain("es");
|
||||
});
|
||||
|
||||
it("should not detect duplicates when labels are different", () => {
|
||||
const labels: TI18nString[] = [
|
||||
{ default: "Option 1", es: "Opción 1" },
|
||||
{ default: "Option 2", es: "Opción 2" },
|
||||
{ default: "Option 3", es: "Opción 3" },
|
||||
];
|
||||
|
||||
const result = findLanguageCodesForDuplicateLabels(labels, mockLanguages);
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should ignore empty strings when detecting duplicates", () => {
|
||||
const labels: TI18nString[] = [
|
||||
{ default: "Option 1", es: "Opción 1" },
|
||||
{ default: "", es: "" }, // Empty strings
|
||||
{ default: " ", es: " " }, // Whitespace-only strings
|
||||
{ default: "Option 2", es: "Opción 2" },
|
||||
];
|
||||
|
||||
const result = findLanguageCodesForDuplicateLabels(labels, mockLanguages);
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should handle mixed scenarios with undefined and duplicates", () => {
|
||||
const labels: TI18nString[] = [
|
||||
{ default: "Option 1" }, // Missing 'es'
|
||||
{ default: "Option 2", es: "Opción" },
|
||||
{ default: "Option 2", es: "Opción" }, // Duplicate in both languages
|
||||
];
|
||||
|
||||
const result = findLanguageCodesForDuplicateLabels(labels, mockLanguages);
|
||||
expect(result).toContain("default");
|
||||
expect(result).toContain("es");
|
||||
});
|
||||
|
||||
it("should work with only default language enabled", () => {
|
||||
const defaultOnlyLanguages: TSurveyLanguage[] = [
|
||||
{
|
||||
default: true,
|
||||
enabled: true,
|
||||
language: { id: "en", code: "en", alias: null, createdAt: new Date(), updatedAt: new Date() },
|
||||
},
|
||||
];
|
||||
|
||||
const labels: TI18nString[] = [
|
||||
{ default: "Option 1" },
|
||||
{ default: "Option 1" }, // Duplicate
|
||||
];
|
||||
|
||||
const result = findLanguageCodesForDuplicateLabels(labels, defaultOnlyLanguages);
|
||||
expect(result).toContain("default");
|
||||
});
|
||||
|
||||
it("should handle completely undefined values gracefully", () => {
|
||||
const labels: TI18nString[] = [
|
||||
{ default: "Option 1" },
|
||||
{ default: "Option 2" },
|
||||
{} as TI18nString, // Empty object - all languages undefined
|
||||
];
|
||||
|
||||
// This should not throw
|
||||
expect(() => findLanguageCodesForDuplicateLabels(labels, mockLanguages)).not.toThrow();
|
||||
const result = findLanguageCodesForDuplicateLabels(labels, mockLanguages);
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user