mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-08 02:17:25 +02:00
Since #6160 dates follow the selected UI language, which is wrong for users who run the UI in one language but want their own region's date format. The Personal page now has a Date Format select with "Match language" (default, current behavior) and "Match browser". The choice is kept in localStorage and read by useDateLocale, which returns no locale in browser mode so Intl uses the browser's own. Six screens imported react-admin's DateField directly and so ignored the language setting (playlist, radio and missing files lists, album table, song and album info). They now use the common DateField wrapper, which also reads the record from the record context.
52 lines
1.8 KiB
JavaScript
52 lines
1.8 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()
|
|
localStorage.removeItem('dateFormat')
|
|
})
|
|
|
|
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()
|
|
})
|
|
|
|
it('returns undefined to use the browser locale when set to browser', async () => {
|
|
localStorage.setItem('dateFormat', 'browser')
|
|
const result = await renderWith('en', ['fr-FR'])
|
|
expect(result.current).toBeUndefined()
|
|
})
|
|
})
|