您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

hooks.spec.ts 2.1KB

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