81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
import { describe, it, expect } from 'vitest'
|
|
import { toFrontend, toBackend } from '../langFormat'
|
|
|
|
describe('langFormat', () => {
|
|
describe('toFrontend', () => {
|
|
it('should convert backend format to frontend format', () => {
|
|
expect(toFrontend('zh_CN')).toBe('zh-CN')
|
|
expect(toFrontend('en_US')).toBe('en-US')
|
|
expect(toFrontend('ja_JP')).toBe('ja-JP')
|
|
})
|
|
|
|
it('should handle already frontend format', () => {
|
|
expect(toFrontend('zh-CN')).toBe('zh-CN')
|
|
expect(toFrontend('en-US')).toBe('en-US')
|
|
})
|
|
|
|
it('should handle empty string', () => {
|
|
expect(toFrontend('')).toBe('')
|
|
})
|
|
|
|
it('should handle null', () => {
|
|
expect(toFrontend(null)).toBe(null)
|
|
})
|
|
|
|
it('should handle undefined', () => {
|
|
expect(toFrontend(undefined)).toBe(undefined)
|
|
})
|
|
|
|
it('should handle non-string input', () => {
|
|
expect(toFrontend(123)).toBe(123)
|
|
expect(toFrontend({})).toBe({})
|
|
})
|
|
})
|
|
|
|
describe('toBackend', () => {
|
|
it('should convert frontend format to backend format', () => {
|
|
expect(toBackend('zh-CN')).toBe('zh_CN')
|
|
expect(toBackend('en-US')).toBe('en_US')
|
|
expect(toBackend('ja-JP')).toBe('ja_JP')
|
|
})
|
|
|
|
it('should handle already backend format', () => {
|
|
expect(toBackend('zh_CN')).toBe('zh_CN')
|
|
expect(toBackend('en_US')).toBe('en_US')
|
|
})
|
|
|
|
it('should handle empty string', () => {
|
|
expect(toBackend('')).toBe('')
|
|
})
|
|
|
|
it('should handle null', () => {
|
|
expect(toBackend(null)).toBe(null)
|
|
})
|
|
|
|
it('should handle undefined', () => {
|
|
expect(toBackend(undefined)).toBe(undefined)
|
|
})
|
|
|
|
it('should handle non-string input', () => {
|
|
expect(toBackend(123)).toBe(123)
|
|
expect(toBackend({})).toBe({})
|
|
})
|
|
})
|
|
|
|
describe('round-trip conversion', () => {
|
|
it('should maintain consistency after round-trip conversion', () => {
|
|
const frontend = 'zh-CN'
|
|
const backend = toBackend(frontend)
|
|
const result = toFrontend(backend)
|
|
expect(result).toBe(frontend)
|
|
})
|
|
|
|
it('should maintain consistency after round-trip conversion (backend first)', () => {
|
|
const backend = 'zh_CN'
|
|
const frontend = toFrontend(backend)
|
|
const result = toBackend(frontend)
|
|
expect(result).toBe(backend)
|
|
})
|
|
})
|
|
})
|