cms.c2sgmbh/tests/int/i18n.int.spec.ts
Martin Porwoll a88e4f60d0 test: add E2E and integration tests with documentation
Tests:
- Update frontend.e2e.spec.ts with locale testing
- Add search.e2e.spec.ts for search functionality
- Add i18n.int.spec.ts for localization tests
- Add search.int.spec.ts for search integration
- Update playwright.config.ts

Documentation:
- Add CLAUDE.md with project instructions
- Add docs/ directory with detailed documentation
- Add scripts/ for utility scripts

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 08:19:52 +00:00

141 lines
4.4 KiB
TypeScript

import { getPayload, Payload } from 'payload'
import config from '@/payload.config'
import { describe, it, beforeAll, expect } from 'vitest'
import { locales, defaultLocale, isValidLocale, getLocaleFromPathname, addLocaleToPathname } from '@/lib/i18n'
let payload: Payload
describe('i18n Library', () => {
describe('locale validation', () => {
it('validates correct locales', () => {
expect(isValidLocale('de')).toBe(true)
expect(isValidLocale('en')).toBe(true)
})
it('rejects invalid locales', () => {
expect(isValidLocale('fr')).toBe(false)
expect(isValidLocale('es')).toBe(false)
expect(isValidLocale('')).toBe(false)
expect(isValidLocale('deutsch')).toBe(false)
})
})
describe('locale from pathname', () => {
it('extracts locale from pathname', () => {
expect(getLocaleFromPathname('/de/page')).toBe('de')
expect(getLocaleFromPathname('/en/page')).toBe('en')
})
it('returns default locale for paths without locale', () => {
expect(getLocaleFromPathname('/page')).toBe(defaultLocale)
expect(getLocaleFromPathname('/')).toBe(defaultLocale)
})
it('returns default locale for invalid locale in path', () => {
expect(getLocaleFromPathname('/fr/page')).toBe(defaultLocale)
})
})
describe('addLocaleToPathname', () => {
it('adds locale prefix to pathname', () => {
expect(addLocaleToPathname('/page', 'de')).toBe('/de/page')
expect(addLocaleToPathname('/page', 'en')).toBe('/en/page')
})
it('handles root path', () => {
expect(addLocaleToPathname('/', 'de')).toBe('/de')
expect(addLocaleToPathname('/', 'en')).toBe('/en')
})
it('replaces existing locale', () => {
expect(addLocaleToPathname('/de/page', 'en')).toBe('/en/page')
expect(addLocaleToPathname('/en/page', 'de')).toBe('/de/page')
})
})
describe('locales configuration', () => {
it('has correct default locale', () => {
expect(defaultLocale).toBe('de')
})
it('supports German and English', () => {
expect(locales).toContain('de')
expect(locales).toContain('en')
expect(locales.length).toBe(2)
})
})
})
describe('Payload Localization Integration', () => {
beforeAll(async () => {
const payloadConfig = await config
payload = await getPayload({ config: payloadConfig })
})
it('payload config has localization enabled', async () => {
const payloadConfig = await config
expect(payloadConfig.localization).toBeDefined()
expect(payloadConfig.localization?.locales).toBeDefined()
expect(payloadConfig.localization?.defaultLocale).toBe('de')
})
it('payload config has i18n enabled', async () => {
const payloadConfig = await config
expect(payloadConfig.i18n).toBeDefined()
expect(payloadConfig.i18n?.supportedLanguages).toBeDefined()
expect(payloadConfig.i18n?.fallbackLanguage).toBe('de')
})
// Note: Actual database operations require migration to be run first
// These tests verify the configuration is correct
})
// Note: These tests require the localization migration to be run first
// They will fail with "relation posts_locales does not exist" until migration is complete
describe.skip('Search with Locale (requires migration)', () => {
beforeAll(async () => {
const payloadConfig = await config
payload = await getPayload({ config: payloadConfig })
})
it('search supports locale parameter', async () => {
// Import search functions
const { searchPosts } = await import('@/lib/search')
// Search with German locale
const resultDe = await searchPosts(payload, {
query: 'test',
locale: 'de',
limit: 10,
offset: 0,
})
expect(resultDe.filters.locale).toBe('de')
})
it('suggestions support locale parameter', async () => {
const { getSearchSuggestions } = await import('@/lib/search')
const suggestions = await getSearchSuggestions(payload, {
query: 'test',
locale: 'en',
limit: 5,
})
// Should return array (may be empty if no matching posts)
expect(Array.isArray(suggestions)).toBe(true)
})
it('getPostsByCategory supports locale parameter', async () => {
const { getPostsByCategory } = await import('@/lib/search')
const result = await getPostsByCategory(payload, {
locale: 'de',
page: 1,
limit: 10,
})
expect(result).toHaveProperty('docs')
expect(result).toHaveProperty('totalDocs')
})
})