wwf
2025-05-20 938c3e5a587ce950a94964ea509b9e7f8834dfae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { renderHook } from '@testing-library/react'
import { useLanguage } from './hooks'
import { useContext } from 'use-context-selector'
import { after } from 'node:test'
 
jest.mock('swr', () => ({
  __esModule: true,
  default: jest.fn(), // mock useSWR
  useSWRConfig: jest.fn(),
}))
 
// mock use-context-selector
jest.mock('use-context-selector', () => ({
  useContext: jest.fn(),
}))
 
// mock service/common functions
jest.mock('@/service/common', () => ({
  fetchDefaultModal: jest.fn(),
  fetchModelList: jest.fn(),
  fetchModelProviderCredentials: jest.fn(),
  fetchModelProviders: jest.fn(),
  getPayUrl: jest.fn(),
}))
 
// mock context hooks
jest.mock('@/context/i18n', () => ({
  __esModule: true,
  default: jest.fn(),
}))
 
jest.mock('@/context/provider-context', () => ({
  useProviderContext: jest.fn(),
}))
 
jest.mock('@/context/modal-context', () => ({
  useModalContextSelector: jest.fn(),
}))
 
jest.mock('@/context/event-emitter', () => ({
  useEventEmitterContextContext: jest.fn(),
}))
 
// mock plugins
jest.mock('@/app/components/plugins/marketplace/hooks', () => ({
  useMarketplacePlugins: jest.fn(),
}))
 
jest.mock('@/app/components/plugins/marketplace/utils', () => ({
  getMarketplacePluginsByCollectionId: jest.fn(),
}))
 
jest.mock('./provider-added-card', () => jest.fn())
 
after(() => {
  jest.resetModules()
  jest.clearAllMocks()
})
 
describe('useLanguage', () => {
  it('should replace hyphen with underscore in locale', () => {
    (useContext as jest.Mock).mockReturnValue({
      locale: 'en-US',
    })
    const { result } = renderHook(() => useLanguage())
    expect(result.current).toBe('en_US')
  })
 
  it('should return locale as is if no hyphen exists', () => {
    (useContext as jest.Mock).mockReturnValue({
      locale: 'enUS',
    })
 
    const { result } = renderHook(() => useLanguage())
    expect(result.current).toBe('enUS')
  })
 
  it('should handle multiple hyphens', () => {
    // Mock the I18n context return value
    (useContext as jest.Mock).mockReturnValue({
      locale: 'zh-Hans-CN',
    })
 
    const { result } = renderHook(() => useLanguage())
    expect(result.current).toBe('zh_Hans-CN')
  })
})