| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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', () => {
- // eslint-disable-next-line no-labels, ts/no-unused-expressions
- UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST: []
- })
-
- 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')
- })
- })
|