Skip to content

Commit b918deb

Browse files
committed
feat: Add Vitest for unit testing translations in firebaseui-core
1 parent b5e3231 commit b918deb

File tree

4 files changed

+721
-13
lines changed

4 files changed

+721
-13
lines changed

packages/firebaseui-core/package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"dev": "tsup --watch",
2323
"lint": "tsc --noEmit",
2424
"format": "prettier --write \"src/**/*.ts\"",
25-
"clean": "rimraf dist"
25+
"clean": "rimraf dist",
26+
"test": "vitest run",
27+
"test:watch": "vitest"
2628
},
2729
"keywords": [
2830
"firebase",
@@ -40,9 +42,11 @@
4042
"zod": "^3.24.1"
4143
},
4244
"devDependencies": {
43-
"typescript": "^5.7.3",
44-
"tsup": "^8.0.1",
4545
"prettier": "^3.1.1",
46-
"rimraf": "^6.0.1"
46+
"rimraf": "^6.0.1",
47+
"tsup": "^8.0.1",
48+
"typescript": "^5.7.3",
49+
"vite": "^6.2.0",
50+
"vitest": "^3.0.7"
4751
}
4852
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { getTranslation, type TranslationsConfig } from './translations';
3+
4+
describe('getTranslation', () => {
5+
it('should return default English translation when no custom translations provided', () => {
6+
const translation = getTranslation('errors', 'userNotFound');
7+
expect(translation).toBe('No account found with this email address');
8+
});
9+
10+
it('should use custom translation when provided', () => {
11+
const customTranslations: TranslationsConfig = {
12+
en: {
13+
errors: {
14+
userNotFound: 'Custom error message',
15+
},
16+
},
17+
};
18+
19+
const translation = getTranslation('errors', 'userNotFound', customTranslations);
20+
expect(translation).toBe('Custom error message');
21+
});
22+
23+
it('should use custom translation in specified language', () => {
24+
const customTranslations: TranslationsConfig = {
25+
es: {
26+
errors: {
27+
userNotFound: 'Usuario no encontrado',
28+
},
29+
},
30+
};
31+
32+
const translation = getTranslation('errors', 'userNotFound', customTranslations, 'es');
33+
expect(translation).toBe('Usuario no encontrado');
34+
});
35+
36+
it('should fallback to English when specified language is not available', () => {
37+
const customTranslations: TranslationsConfig = {
38+
en: {
39+
errors: {
40+
userNotFound: 'Custom English message',
41+
},
42+
},
43+
};
44+
45+
const translation = getTranslation('errors', 'userNotFound', customTranslations, 'fr');
46+
expect(translation).toBe('Custom English message');
47+
});
48+
49+
it('should fallback to default English when no custom translations match', () => {
50+
const customTranslations: TranslationsConfig = {
51+
es: {
52+
errors: {
53+
wrongPassword: 'Contraseña incorrecta',
54+
},
55+
},
56+
};
57+
58+
const translation = getTranslation('errors', 'userNotFound', customTranslations, 'es');
59+
expect(translation).toBe('No account found with this email address');
60+
});
61+
62+
it('should work with different translation categories', () => {
63+
const translation = getTranslation('messages', 'passwordResetEmailSent');
64+
expect(translation).toBe('Password reset email sent successfully');
65+
});
66+
67+
it('should handle partial custom translations', () => {
68+
const customTranslations: TranslationsConfig = {
69+
en: {
70+
errors: {
71+
// Only override one error message
72+
userNotFound: 'Custom not found message',
73+
},
74+
},
75+
};
76+
77+
const userNotFound = getTranslation('errors', 'userNotFound', customTranslations);
78+
const wrongPassword = getTranslation('errors', 'wrongPassword', customTranslations);
79+
80+
expect(userNotFound).toBe('Custom not found message');
81+
expect(wrongPassword).toBe('Incorrect password');
82+
});
83+
84+
it('should handle empty custom translations object', () => {
85+
const customTranslations: TranslationsConfig = {};
86+
const translation = getTranslation('errors', 'userNotFound', customTranslations);
87+
expect(translation).toBe('No account found with this email address');
88+
});
89+
90+
it('should handle undefined custom translations', () => {
91+
const translation = getTranslation('errors', 'userNotFound', undefined);
92+
expect(translation).toBe('No account found with this email address');
93+
});
94+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineConfig } from 'vitest/config';
2+
3+
export default defineConfig({
4+
test: {
5+
// Use the same environment as the package
6+
environment: 'node',
7+
// Include TypeScript files
8+
include: ['src/**/*.{test,spec}.{js,ts}'],
9+
// Exclude build output and node_modules
10+
exclude: ['node_modules/**/*', 'dist/**/*'],
11+
},
12+
});

0 commit comments

Comments
 (0)