|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#30285 — Studio's translation table, mounted over a package read |
| 5 | + * through the metadata client and the language served through the SDK. |
| 6 | + * |
| 7 | + * Pinned: |
| 8 | + * - a row per declared string, grouped by item, with its source text, the |
| 9 | + * platform object the package points at (`sys_user`) included; |
| 10 | + * - the count of what is untranslated, and the filter that shows only that; |
| 11 | + * - what the language serves from another bundle counts as translated, and |
| 12 | + * an answer in another (fallback) language does not; |
| 13 | + * - typing a translation hands the item back with the value at the row's key; |
| 14 | + * - no "no custom renderer, editing as JSON" notice: JSON is the advanced view. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as React from 'react'; |
| 18 | +import { describe, expect, it, vi, beforeEach } from 'vitest'; |
| 19 | +import { fireEvent, render, screen, waitFor, within } from '@testing-library/react'; |
| 20 | + |
| 21 | +const DECLARED: Record<string, Array<Record<string, unknown>>> = { |
| 22 | + object: [ |
| 23 | + { |
| 24 | + name: 'crm_account', |
| 25 | + label: 'Account', |
| 26 | + fields: { |
| 27 | + tier: { type: 'select', label: 'Tier', options: [{ value: 'gold', label: 'Gold' }] }, |
| 28 | + owner: { type: 'lookup', label: 'Owner', reference: 'sys_user' }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + ], |
| 32 | + app: [{ name: 'crm', label: 'CRM', navigation: [] }], |
| 33 | +}; |
| 34 | +const PLATFORM: Record<string, Record<string, unknown>> = { sys_user: { name: 'sys_user', label: 'User', fields: {} } }; |
| 35 | + |
| 36 | +const mockClient = { |
| 37 | + list: vi.fn(async (type: string) => DECLARED[type] ?? []), |
| 38 | + layered: vi.fn(async (type: string, name: string, options?: { packageId?: string }) => { |
| 39 | + const doc = options?.packageId ? (DECLARED[type] ?? []).find((d) => d.name === name) : PLATFORM[name]; |
| 40 | + return { code: doc ?? null, effective: doc ?? null }; |
| 41 | + }), |
| 42 | +}; |
| 43 | +let servedAnswer: { locale: string; translations: Record<string, unknown> } = { locale: 'vi-VN', translations: {} }; |
| 44 | +const getTranslations = vi.fn(async () => servedAnswer); |
| 45 | + |
| 46 | +vi.mock('../metadata-admin/useMetadata', async (importOriginal) => { |
| 47 | + const mod = await importOriginal<typeof import('../metadata-admin/useMetadata')>(); |
| 48 | + return { ...mod, useMetadataClient: () => mockClient }; |
| 49 | +}); |
| 50 | +vi.mock('@object-ui/react', async (importOriginal) => { |
| 51 | + const mod = await importOriginal<typeof import('@object-ui/react')>(); |
| 52 | + const adapter = { getClient: () => ({ i18n: { getTranslations } }) }; |
| 53 | + return { ...mod, useAdapter: () => adapter }; |
| 54 | +}); |
| 55 | + |
| 56 | +import { StudioTranslationTable } from './StudioTranslationTable'; |
| 57 | + |
| 58 | +function Harness({ initial, onSeen }: { initial: Record<string, unknown>; onSeen?: (d: Record<string, unknown>) => void }) { |
| 59 | + const [draft, setDraft] = React.useState(initial); |
| 60 | + return ( |
| 61 | + <StudioTranslationTable |
| 62 | + packageId="com.example.crm" |
| 63 | + draft={draft} |
| 64 | + onChange={(next) => { |
| 65 | + onSeen?.(next); |
| 66 | + setDraft(next); |
| 67 | + }} |
| 68 | + readOnly={false} |
| 69 | + locale="en" |
| 70 | + /> |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +const rowFor = (key: string) => document.querySelector(`[data-testid="studio-translation-row"][data-key="${key}"]`) as HTMLElement; |
| 75 | + |
| 76 | +beforeEach(() => { |
| 77 | + servedAnswer = { locale: 'vi-VN', translations: {} }; |
| 78 | + mockClient.list.mockClear(); |
| 79 | + mockClient.layered.mockClear(); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('StudioTranslationTable', () => { |
| 83 | + it('lists the declared strings by item, the platform object pointed at included, with the untranslated count', async () => { |
| 84 | + render(<Harness initial={{ name: 'crm_vi', locale: 'vi-VN', objects: { crm_account: { label: 'Tài khoản' } } }} />); |
| 85 | + const count = await screen.findByTestId('studio-translation-count'); |
| 86 | + const groups = screen.getAllByTestId('studio-translation-group').map((g) => g.getAttribute('data-group')); |
| 87 | + expect(groups).toEqual(expect.arrayContaining(['objects.crm_account', 'objects.sys_user', 'apps.crm'])); |
| 88 | + expect(within(rowFor('objects.sys_user.label')).getByText('User')).toBeTruthy(); |
| 89 | + expect(within(rowFor('objects.crm_account.fields.tier.options.gold')).getByText('Gold')).toBeTruthy(); |
| 90 | + expect(rowFor('objects.crm_account.label').getAttribute('data-state')).toBe('here'); |
| 91 | + const total = Number(count.getAttribute('data-total')); |
| 92 | + expect(Number(count.getAttribute('data-untranslated'))).toBe(total - 1); |
| 93 | + expect(count.textContent).toBe(`${total - 1} of ${total} untranslated`); |
| 94 | + expect(document.body.textContent).not.toMatch(/custom renderer|JSON editing/i); |
| 95 | + }); |
| 96 | + |
| 97 | + it('shows only the untranslated rows when filtered', async () => { |
| 98 | + render(<Harness initial={{ name: 'crm_vi', locale: 'vi-VN', objects: { crm_account: { label: 'Tài khoản' } } }} />); |
| 99 | + await screen.findByTestId('studio-translation-count'); |
| 100 | + fireEvent.click(screen.getByTestId('studio-translation-only-missing')); |
| 101 | + const states = screen.getAllByTestId('studio-translation-row').map((r) => r.getAttribute('data-state')); |
| 102 | + expect(states.length).toBeGreaterThan(0); |
| 103 | + expect(new Set(states)).toEqual(new Set(['missing'])); |
| 104 | + expect(rowFor('objects.crm_account.label')).toBeNull(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('counts what the language serves from another bundle as translated, but not a fallback language', async () => { |
| 108 | + servedAnswer = { locale: 'vi-VN', translations: { objects: { sys_user: { label: 'Người dùng' } } } }; |
| 109 | + const { unmount } = render(<Harness initial={{ name: 'crm_vi', locale: 'vi-VN' }} />); |
| 110 | + await waitFor(() => expect(rowFor('objects.sys_user.label')?.getAttribute('data-state')).toBe('elsewhere')); |
| 111 | + unmount(); |
| 112 | + |
| 113 | + servedAnswer = { locale: 'en', translations: { objects: { sys_user: { label: 'User' } } } }; |
| 114 | + render(<Harness initial={{ name: 'crm_vi', locale: 'vi-VN' }} />); |
| 115 | + await screen.findByTestId('studio-translation-count'); |
| 116 | + await waitFor(() => expect(getTranslations).toHaveBeenCalledWith('vi-VN')); |
| 117 | + expect(rowFor('objects.sys_user.label').getAttribute('data-state')).toBe('missing'); |
| 118 | + }); |
| 119 | + |
| 120 | + it("hands the item back with the typed translation at the row's key", async () => { |
| 121 | + const seen = vi.fn(); |
| 122 | + render(<Harness initial={{ name: 'crm_vi', label: 'Tiếng Việt', locale: 'vi-VN' }} onSeen={seen} />); |
| 123 | + await screen.findByTestId('studio-translation-count'); |
| 124 | + fireEvent.change(within(rowFor('objects.sys_user.label')).getByTestId('studio-translation-input'), { |
| 125 | + target: { value: 'Người dùng' }, |
| 126 | + }); |
| 127 | + expect(seen).toHaveBeenLastCalledWith({ |
| 128 | + name: 'crm_vi', |
| 129 | + label: 'Tiếng Việt', |
| 130 | + locale: 'vi-VN', |
| 131 | + objects: { sys_user: { label: 'Người dùng' } }, |
| 132 | + }); |
| 133 | + expect(rowFor('objects.sys_user.label').getAttribute('data-state')).toBe('here'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('keeps the JSON as an advanced view of the translation groups', async () => { |
| 137 | + render(<Harness initial={{ name: 'crm_vi', locale: 'vi-VN', messages: { hello: 'Xin chào' } }} />); |
| 138 | + await screen.findByTestId('studio-translation-count'); |
| 139 | + const json = screen.getByTestId('studio-translation-json-input') as HTMLTextAreaElement; |
| 140 | + expect(JSON.parse(json.value)).toEqual({ messages: { hello: 'Xin chào' } }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments