|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { renderHook } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import { I18nProvider } from '../provider'; |
| 5 | +import { createI18n } from '../i18n'; |
| 6 | +import { useObjectLabel } from '../useObjectLabel'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Create a wrapper with custom translations to simulate CRM locale bundles. |
| 10 | + */ |
| 11 | +function createWrapper(lang: string, translations?: Record<string, unknown>) { |
| 12 | + const instance = createI18n({ defaultLanguage: lang, detectBrowserLanguage: false }); |
| 13 | + if (translations) { |
| 14 | + instance.addResourceBundle(lang, 'translation', translations, true, true); |
| 15 | + } |
| 16 | + return ({ children }: { children: React.ReactNode }) => |
| 17 | + React.createElement(I18nProvider, { instance }, children); |
| 18 | +} |
| 19 | + |
| 20 | +describe('useObjectLabel', () => { |
| 21 | + describe('objectLabel', () => { |
| 22 | + it('returns the plain label when no translation exists', () => { |
| 23 | + const wrapper = createWrapper('en'); |
| 24 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 25 | + |
| 26 | + const label = result.current.objectLabel({ name: 'order_item', label: 'Order Item' }); |
| 27 | + expect(label).toBe('Order Item'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns the translated label when a translation exists', () => { |
| 31 | + const wrapper = createWrapper('zh', { |
| 32 | + crm: { objects: { order_item: { label: '订单明细' } } }, |
| 33 | + }); |
| 34 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 35 | + |
| 36 | + const label = result.current.objectLabel({ name: 'order_item', label: 'Order Item' }); |
| 37 | + expect(label).toBe('订单明细'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('falls back to plain label when translation key returns empty string', () => { |
| 41 | + const wrapper = createWrapper('zh', { |
| 42 | + crm: { objects: { order_item: { label: '' } } }, |
| 43 | + }); |
| 44 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 45 | + |
| 46 | + const label = result.current.objectLabel({ name: 'order_item', label: 'Order Item' }); |
| 47 | + expect(label).toBe('Order Item'); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('objectDescription', () => { |
| 52 | + it('returns undefined when objectDef has no description', () => { |
| 53 | + const wrapper = createWrapper('en'); |
| 54 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 55 | + |
| 56 | + const desc = result.current.objectDescription({ name: 'order_item' }); |
| 57 | + expect(desc).toBeUndefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns the plain description when no translation exists', () => { |
| 61 | + const wrapper = createWrapper('en'); |
| 62 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 63 | + |
| 64 | + const desc = result.current.objectDescription({ name: 'order_item', description: 'Line items in an order' }); |
| 65 | + expect(desc).toBe('Line items in an order'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns the translated description when a translation exists', () => { |
| 69 | + const wrapper = createWrapper('zh', { |
| 70 | + crm: { objects: { order_item: { description: '订单中的商品条目' } } }, |
| 71 | + }); |
| 72 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 73 | + |
| 74 | + const desc = result.current.objectDescription({ name: 'order_item', description: 'Line items in an order' }); |
| 75 | + expect(desc).toBe('订单中的商品条目'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('fieldLabel', () => { |
| 80 | + it('returns the fallback label when no translation exists', () => { |
| 81 | + const wrapper = createWrapper('en'); |
| 82 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 83 | + |
| 84 | + const label = result.current.fieldLabel('order_item', 'name', 'Line Item'); |
| 85 | + expect(label).toBe('Line Item'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns the translated label when a translation exists', () => { |
| 89 | + const wrapper = createWrapper('zh', { |
| 90 | + crm: { fields: { order_item: { name: '订单项' } } }, |
| 91 | + }); |
| 92 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 93 | + |
| 94 | + const label = result.current.fieldLabel('order_item', 'name', 'Line Item'); |
| 95 | + expect(label).toBe('订单项'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('falls back to plain label when translation key returns empty string', () => { |
| 99 | + const wrapper = createWrapper('zh', { |
| 100 | + crm: { fields: { order_item: { name: '' } } }, |
| 101 | + }); |
| 102 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 103 | + |
| 104 | + const label = result.current.fieldLabel('order_item', 'name', 'Line Item'); |
| 105 | + expect(label).toBe('Line Item'); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('multiple objects', () => { |
| 110 | + it('resolves labels for different objects independently', () => { |
| 111 | + const wrapper = createWrapper('zh', { |
| 112 | + crm: { |
| 113 | + objects: { |
| 114 | + contact: { label: '联系人' }, |
| 115 | + opportunity: { label: '商机' }, |
| 116 | + }, |
| 117 | + fields: { |
| 118 | + contact: { email: '邮箱' }, |
| 119 | + opportunity: { stage: '阶段' }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }); |
| 123 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 124 | + |
| 125 | + expect(result.current.objectLabel({ name: 'contact', label: 'Contact' })).toBe('联系人'); |
| 126 | + expect(result.current.objectLabel({ name: 'opportunity', label: 'Opportunity' })).toBe('商机'); |
| 127 | + expect(result.current.fieldLabel('contact', 'email', 'Email')).toBe('邮箱'); |
| 128 | + expect(result.current.fieldLabel('opportunity', 'stage', 'Stage')).toBe('阶段'); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments