-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocales.test.js
More file actions
184 lines (152 loc) · 5.59 KB
/
Copy pathlocales.test.js
File metadata and controls
184 lines (152 loc) · 5.59 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* T-001: Translation JSON Files Structure and Validation Tests
*
* Tests validate:
* - Directory structure exists
* - JSON files are valid and parse correctly
* - Keys match between en.json and es.json
* - Structure is max 2 levels deep
* - All required UI strings are present
*/
const fs = require('fs');
const path = require('path');
const localesDir = path.resolve(__dirname);
const enPath = path.resolve(localesDir, 'en.json');
const esPath = path.resolve(localesDir, 'es.json');
const readmePath = path.resolve(localesDir, '_README.json');
/**
* Helper to check if object structure is max 2 levels deep
*/
const isMaxTwoLevelsDeep = (obj, currentLevel = 1) => {
if (typeof obj !== 'object' || obj === null) {
return true;
}
if (currentLevel >= 2) {
// At level 2, all values must be primitives (strings, numbers, etc.)
return Object.values(obj).every((value) => typeof value !== 'object' || value === null);
}
// Check nested objects
return Object.values(obj).every((value) => isMaxTwoLevelsDeep(value, currentLevel + 1));
};
/**
* Recursively extract all leaf keys from nested object
*/
const extractLeafKeys = (obj, prefix = '') =>
Object.entries(obj).reduce((keys, [key, value]) => {
const fullKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
return [...keys, ...extractLeafKeys(value, fullKey)];
}
return [...keys, fullKey];
}, []);
describe('T-001: Translation JSON Files Structure', () => {
describe('Directory and File Existence', () => {
test('locales directory exists', () => {
expect(fs.existsSync(localesDir)).toBe(true);
});
test('en.json exists', () => {
expect(fs.existsSync(enPath)).toBe(true);
});
test('es.json exists', () => {
expect(fs.existsSync(esPath)).toBe(true);
});
test('_README.json exists', () => {
expect(fs.existsSync(readmePath)).toBe(true);
});
});
describe('JSON File Validity', () => {
let enData;
let esData;
let readmeData;
beforeAll(() => {
// These should not throw if JSON is valid
enData = JSON.parse(fs.readFileSync(enPath, 'utf-8'));
esData = JSON.parse(fs.readFileSync(esPath, 'utf-8'));
readmeData = JSON.parse(fs.readFileSync(readmePath, 'utf-8'));
});
test('en.json is valid JSON', () => {
expect(enData).toBeDefined();
expect(typeof enData).toBe('object');
});
test('es.json is valid JSON', () => {
expect(esData).toBeDefined();
expect(typeof esData).toBe('object');
});
test('_README.json is valid JSON and has description', () => {
expect(readmeData).toBeDefined();
expect(readmeData.description).toBeDefined();
expect(typeof readmeData.description).toBe('string');
});
test('en.json has documentation comment', () => {
const commentField = '_comment';
expect(enData[commentField]).toBeDefined();
expect(typeof enData[commentField]).toBe('string');
expect(enData[commentField].length).toBeGreaterThan(0);
});
test('es.json has documentation comment', () => {
const commentField = '_comment';
expect(esData[commentField]).toBeDefined();
expect(typeof esData[commentField]).toBe('string');
expect(esData[commentField].length).toBeGreaterThan(0);
});
});
describe('JSON Structure Validation', () => {
let enData;
let esData;
beforeAll(() => {
enData = JSON.parse(fs.readFileSync(enPath, 'utf-8'));
esData = JSON.parse(fs.readFileSync(esPath, 'utf-8'));
});
test('en.json structure is max 2 levels deep', () => {
expect(isMaxTwoLevelsDeep(enData)).toBe(true);
});
test('es.json structure is max 2 levels deep', () => {
expect(isMaxTwoLevelsDeep(esData)).toBe(true);
});
test('en.json and es.json have matching keys', () => {
const enKeys = extractLeafKeys(enData).sort();
const esKeys = extractLeafKeys(esData).sort();
expect(enKeys).toEqual(esKeys);
});
});
describe('Required UI Strings Presence', () => {
let enData;
beforeAll(() => {
enData = JSON.parse(fs.readFileSync(enPath, 'utf-8'));
});
test('contains Capytan tips', () => {
expect(enData.capytan).toBeDefined();
expect(enData.capytan.tip1).toBeDefined();
expect(enData.capytan.tip2).toBeDefined();
expect(enData.capytan.tip3).toBeDefined();
});
test('contains button labels', () => {
expect(enData.buttons).toBeDefined();
expect(enData.buttons.spinGlobe).toBeDefined();
expect(enData.buttons.nextClue).toBeDefined();
expect(enData.buttons.submitGuess).toBeDefined();
});
test('contains clue board labels', () => {
expect(enData.clueBoard).toBeDefined();
expect(enData.clueBoard.header).toBeDefined();
expect(enData.clueBoard.emptyState).toBeDefined();
});
test('contains feedback messages', () => {
expect(enData.feedback).toBeDefined();
expect(enData.feedback.correct).toBeDefined();
expect(enData.feedback.incorrect).toBeDefined();
expect(enData.feedback.emptyGuess).toBeDefined();
expect(enData.feedback.noActiveCountry).toBeDefined();
});
test('contains discovery log labels', () => {
expect(enData.discoveryLog).toBeDefined();
expect(enData.discoveryLog.header).toBeDefined();
expect(enData.discoveryLog.emptyState).toBeDefined();
});
test('contains form labels', () => {
expect(enData.form).toBeDefined();
expect(enData.form.guessLabel).toBeDefined();
expect(enData.form.guessPlaceholder).toBeDefined();
});
});
});