-
Notifications
You must be signed in to change notification settings - Fork 837
Expand file tree
/
Copy pathconfig.spec.ts
More file actions
148 lines (128 loc) · 3.66 KB
/
config.spec.ts
File metadata and controls
148 lines (128 loc) · 3.66 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { describe, it, expect } from "vitest";
import {
parseI18nConfig,
defaultConfig,
LATEST_CONFIG_DEFINITION,
} from "./config";
// Helper function to create a v0 config
const createV0Config = () => ({
version: 0,
});
// Helper function to create a v1 config
const createV1Config = () => ({
version: 1,
locale: {
source: "en",
targets: ["es"],
},
buckets: {
"src/ui/[locale]/.json": "json",
"src/blog/[locale]/*.md": "markdown",
},
});
// Helper function to create a v1.1 config
const createV1_1Config = () => ({
version: 1.1,
locale: {
source: "en",
targets: ["es", "fr", "pt-PT", "pt_BR"],
},
buckets: {
json: {
include: ["src/ui/[locale]/.json"],
},
markdown: {
include: ["src/blog/[locale]/*.md"],
exclude: ["src/blog/[locale]/drafts.md"],
},
},
});
const createV1_2Config = () => ({
...createV1_1Config(),
version: 1.2,
});
const createV1_3Config = () => ({
...createV1_2Config(),
version: 1.3,
});
const createV1_4Config = () => ({
...createV1_3Config(),
version: 1.4,
$schema: "https://lingo.dev/schema/i18n.json",
});
const createInvalidLocaleConfig = () => ({
version: 1,
locale: {
source: "bbbb",
targets: ["es", "aaaa"],
},
buckets: {
"src/ui/[locale]/.json": "json",
"src/blog/[locale]/*.md": "markdown",
},
});
describe("I18n Config Parser", () => {
it("should upgrade v0 config to latest version", () => {
const v0Config = createV0Config();
const result = parseI18nConfig(v0Config);
expect(result["$schema"]).toBeDefined();
expect(result.version).toBe(LATEST_CONFIG_DEFINITION.defaultValue.version);
expect(result.locale).toEqual(defaultConfig.locale);
expect(result.buckets).toEqual({});
});
it("should upgrade v1 config to latest version", () => {
const v1Config = createV1Config();
const result = parseI18nConfig(v1Config);
expect(result["$schema"]).toBeDefined();
expect(result.version).toBe(LATEST_CONFIG_DEFINITION.defaultValue.version);
expect(result.locale).toEqual(v1Config.locale);
expect(result.buckets).toEqual({
json: {
include: ["src/ui/[locale]/.json"],
},
markdown: {
include: ["src/blog/[locale]/*.md"],
},
});
});
it("should handle empty config and use defaults", () => {
const emptyConfig = {};
const result = parseI18nConfig(emptyConfig);
expect(result).toEqual(defaultConfig);
});
it("should ignore extra fields in the config", () => {
const configWithExtra = {
...createV1_4Config(),
extraField: "should be ignored",
};
const result = parseI18nConfig(configWithExtra);
expect(result).not.toHaveProperty("extraField");
expect(result).toEqual(createV1_4Config());
});
it("should parse config with dev.usePseudotranslator", () => {
const configWithDev = {
...createV1_4Config(),
version: "1.14",
dev: {
usePseudotranslator: true,
},
};
const result = parseI18nConfig(configWithDev);
expect(result.dev?.usePseudotranslator).toBe(true);
expect(result.version).toBe("1.14");
});
it("should parse config without dev field", () => {
const configWithoutDev = {
...createV1_4Config(),
version: "1.14",
};
const result = parseI18nConfig(configWithoutDev);
expect(result.dev).toBeUndefined();
});
it("should throw an error for unsupported locales", () => {
const invalidLocaleConfig = createInvalidLocaleConfig();
expect(() => parseI18nConfig(invalidLocaleConfig)).toThrow(
`\nUnsupported locale: ${invalidLocaleConfig.locale.source}\nUnsupported locale: ${invalidLocaleConfig.locale.targets[1]}`,
);
});
});