navidrome/ui/src/i18n/useDateLocale.test.js
Deluan Quintão be8ec15168
feat(ui): format dates using the selected language (#6160)
Dates were rendered with the browser locale, ignoring the language chosen in
Personal settings, so a user browsing in German still saw US-style dates. All
date rendering now resolves its locale through a useDateLocale hook that returns
the selected language, augmented with the region from navigator.languages when
the language carries none (Intl reads a bare "en" as en-US). Applied to
DateField, the rated/loved tooltips, the mobile user list and album release
dates; the three inline timestamp tooltips now share a formatDateTime helper.

Closes #229
2026-09-19 14:07:42 -04:00

45 lines
1.5 KiB
JavaScript

import { renderHook } from '@testing-library/react-hooks'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { useDateLocale } from './useDateLocale'
vi.mock('react-admin', () => ({
useLocale: vi.fn(),
}))
describe('useDateLocale', () => {
beforeEach(() => {
vi.clearAllMocks()
})
const renderWith = async (locale, browserLocales) => {
const { useLocale } = await import('react-admin')
vi.mocked(useLocale).mockReturnValue(locale)
vi.spyOn(navigator, 'languages', 'get').mockReturnValue(browserLocales)
return renderHook(() => useDateLocale()).result
}
it('adds the region from the browser when the language has none', async () => {
const result = await renderWith('en', ['en-GB', 'fr-FR'])
expect(result.current).toEqual('en-GB')
})
it('falls back to the language when no browser entry matches', async () => {
const result = await renderWith('de', ['en-US', 'fr-FR'])
expect(result.current).toEqual('de')
})
it('keeps a language that already carries a region or script', async () => {
expect((await renderWith('pt-br', ['pt-PT'])).current).toEqual('pt-br')
expect((await renderWith('zh-Hans', ['zh-TW'])).current).toEqual('zh-Hans')
})
it('matches the browser language case-insensitively', async () => {
const result = await renderWith('pt', ['PT-PT'])
expect(result.current).toEqual('PT-PT')
})
it('returns undefined when there is no language', async () => {
const result = await renderWith(undefined, ['en-GB'])
expect(result.current).toBeUndefined()
})
})