cms.c2sgmbh/tests/int/i18n.int.spec.ts
Martin Porwoll 47d4825f77 revert: downgrade to Next.js 15.5.9 for Payload compatibility
Payload CMS 3.68.4 doesn't officially support Next.js 16 yet.
Reverting to 15.5.9 restores full compatibility.

Changes:
- Revert next: 16.0.10 → 15.5.9
- Revert eslint-config-next: 16.0.10 → 15.5.9
- Revert proxy.ts → middleware.ts (Next.js 15 convention)
- Restore eslint config in next.config.mjs
- Remove turbopack config (not needed for Next.js 15)

Test fixes (TypeScript errors):
- Fix MockPayloadRequest interface (remove PayloadRequest extension)
- Add Where type imports to access control tests
- Fix headers type casting in rate-limiter tests
- Fix localization type guard in i18n tests
- Add type property to post creation in search tests
- Fix nodemailer mock typing in email tests

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 10:07:39 +00:00

144 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).not.toBe(false)
// Type guard for localization config
const localization = payloadConfig.localization
if (localization && typeof localization === 'object') {
expect(localization.locales).toBeDefined()
expect(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
})
describe('Search with Locale', () => {
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')
})
})