|
| 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 { useObjectTranslation } from '../provider'; |
| 6 | +import { useObjectLabel } from '../useObjectLabel'; |
| 7 | + |
| 8 | +const wrapper = ({ children }: { children: React.ReactNode }) => |
| 9 | + React.createElement( |
| 10 | + I18nProvider, |
| 11 | + { config: { defaultLanguage: 'en', detectBrowserLanguage: false } }, |
| 12 | + children, |
| 13 | + ); |
| 14 | + |
| 15 | +/** The metadata literal spec, as authored on sys_user.create_user. */ |
| 16 | +const spec = { |
| 17 | + title: 'User Created', |
| 18 | + description: 'Copy the temporary password now — it is shown only once and never stored.', |
| 19 | + acknowledge: 'I have saved this password', |
| 20 | + fields: [ |
| 21 | + { path: 'user.email', label: 'Email', format: 'text' }, |
| 22 | + { path: 'user.phoneNumber', label: 'Phone Number', format: 'text' }, |
| 23 | + { path: 'temporaryPassword', label: 'Temporary Password', format: 'secret' }, |
| 24 | + ], |
| 25 | +}; |
| 26 | + |
| 27 | +describe('useObjectLabel().actionResultDialog', () => { |
| 28 | + it('falls back to the literal spec when no translation is registered', () => { |
| 29 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 30 | + const out = result.current.actionResultDialog('sys_user', 'create_user', spec); |
| 31 | + expect(out?.title).toBe('User Created'); |
| 32 | + expect(out?.fields?.[0].label).toBe('Email'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('overlays translated copy and per-path field labels (dotted paths stay literal keys)', () => { |
| 36 | + const { result } = renderHook( |
| 37 | + () => ({ labels: useObjectLabel(), i18n: useObjectTranslation().i18n }), |
| 38 | + { wrapper }, |
| 39 | + ); |
| 40 | + result.current.i18n.addResourceBundle( |
| 41 | + 'en', |
| 42 | + 'translation', |
| 43 | + { |
| 44 | + setup: { |
| 45 | + objects: { |
| 46 | + sys_user: { |
| 47 | + _actions: { |
| 48 | + create_user: { |
| 49 | + resultDialog: { |
| 50 | + title: '用户已创建', |
| 51 | + description: '请立即复制临时密码——它只显示一次,不会被保存。', |
| 52 | + acknowledge: '我已保存该密码', |
| 53 | + fields: { |
| 54 | + 'user.email': '邮箱', |
| 55 | + temporaryPassword: '临时密码', |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + true, |
| 65 | + true, |
| 66 | + ); |
| 67 | + |
| 68 | + const out = result.current.labels.actionResultDialog('sys_user', 'create_user', spec); |
| 69 | + expect(out?.title).toBe('用户已创建'); |
| 70 | + expect(out?.description).toBe('请立即复制临时密码——它只显示一次,不会被保存。'); |
| 71 | + expect(out?.acknowledge).toBe('我已保存该密码'); |
| 72 | + // The dotted path resolves as ONE literal key of the fields record. |
| 73 | + expect(out?.fields?.[0]).toEqual({ path: 'user.email', label: '邮箱', format: 'text' }); |
| 74 | + // Untranslated fields keep the metadata literal; format survives. |
| 75 | + expect(out?.fields?.[1]).toEqual({ path: 'user.phoneNumber', label: 'Phone Number', format: 'text' }); |
| 76 | + expect(out?.fields?.[2]).toEqual({ path: 'temporaryPassword', label: '临时密码', format: 'secret' }); |
| 77 | + // Source spec is not mutated. |
| 78 | + expect(spec.title).toBe('User Created'); |
| 79 | + expect(spec.fields[0].label).toBe('Email'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns the spec untouched when the action name is missing', () => { |
| 83 | + const { result } = renderHook(() => useObjectLabel(), { wrapper }); |
| 84 | + expect(result.current.actionResultDialog('sys_user', undefined, spec)).toBe(spec); |
| 85 | + expect(result.current.actionResultDialog('sys_user', 'create_user', undefined)).toBeUndefined(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments