You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.spec.ts 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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', () => {
  44. // eslint-disable-next-line no-labels, ts/no-unused-expressions
  45. UPDATE_MODEL_PROVIDER_CUSTOM_MODEL_LIST: []
  46. })
  47. after(() => {
  48. jest.resetModules()
  49. jest.clearAllMocks()
  50. })
  51. describe('useLanguage', () => {
  52. it('should replace hyphen with underscore in locale', () => {
  53. (useContext as jest.Mock).mockReturnValue({
  54. locale: 'en-US',
  55. })
  56. const { result } = renderHook(() => useLanguage())
  57. expect(result.current).toBe('en_US')
  58. })
  59. it('should return locale as is if no hyphen exists', () => {
  60. (useContext as jest.Mock).mockReturnValue({
  61. locale: 'enUS',
  62. })
  63. const { result } = renderHook(() => useLanguage())
  64. expect(result.current).toBe('enUS')
  65. })
  66. it('should handle multiple hyphens', () => {
  67. // Mock the I18n context return value
  68. (useContext as jest.Mock).mockReturnValue({
  69. locale: 'zh-Hans-CN',
  70. })
  71. const { result } = renderHook(() => useLanguage())
  72. expect(result.current).toBe('zh_Hans-CN')
  73. })
  74. })