|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #25916, third pass — the References sheet's table headers read |
| 5 | + * `engine.edit.refsCol*` from this module's own `engine.*` table (the Studio |
| 6 | + * carve-out, `packages/i18n/README.md`), so a zh session reads its column |
| 7 | + * names in zh where they were English literals. Control: an English session |
| 8 | + * still reads From type / From name / Path. |
| 9 | + */ |
| 10 | + |
| 11 | +import '@testing-library/jest-dom/vitest'; |
| 12 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 13 | +import { fireEvent, render, screen, cleanup, waitFor, within } from '@testing-library/react'; |
| 14 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 15 | +import { I18nProvider } from '@object-ui/i18n'; |
| 16 | +import { t } from './i18n'; |
| 17 | + |
| 18 | +const objectDef = { |
| 19 | + name: 'showcase_account', |
| 20 | + label: 'Account', |
| 21 | + fields: [{ name: 'title', label: 'Title', type: 'text' }], |
| 22 | +}; |
| 23 | + |
| 24 | +/** A plain packaged entry — this suite is about the References sheet only. */ |
| 25 | +const LAYERED = { |
| 26 | + code: { ...objectDef, _packageId: 'com.example.showcase', _provenance: 'package' }, |
| 27 | + overlay: null, |
| 28 | + effective: { ...objectDef }, |
| 29 | + provenance: 'package', |
| 30 | + packageId: 'com.example.showcase', |
| 31 | + editable: true, |
| 32 | + deletable: true, |
| 33 | + resettable: true, |
| 34 | +}; |
| 35 | + |
| 36 | +const referencesImpl = { |
| 37 | + current: vi.fn(async () => [{ fromType: 'view', fromName: 'all_accounts', path: 'data.object' }]), |
| 38 | +}; |
| 39 | + |
| 40 | +const mockClient = { |
| 41 | + list: vi.fn(async () => []), |
| 42 | + listDrafts: vi.fn(async () => []), |
| 43 | + get: vi.fn(async () => null), |
| 44 | + getDraft: vi.fn(async () => null), |
| 45 | + layered: vi.fn(async () => LAYERED), |
| 46 | + reset: vi.fn(async () => ({ success: true })), |
| 47 | + references: vi.fn(async (...args: unknown[]) => referencesImpl.current(...args)), |
| 48 | +}; |
| 49 | + |
| 50 | +vi.mock('./useMetadata', async (importOriginal) => { |
| 51 | + const mod = await importOriginal<typeof import('./useMetadata')>(); |
| 52 | + return { |
| 53 | + ...mod, |
| 54 | + useMetadataClient: () => mockClient, |
| 55 | + useMetadataTypes: () => ({ |
| 56 | + loading: false, |
| 57 | + error: null, |
| 58 | + entries: [ |
| 59 | + { |
| 60 | + type: 'object', |
| 61 | + name: 'object', |
| 62 | + label: 'Object', |
| 63 | + allowEnvironmentOverlay: true, |
| 64 | + allowRuntimeCreate: true, |
| 65 | + schema: { |
| 66 | + type: 'object', |
| 67 | + properties: { name: { type: 'string' }, label: { type: 'string' } }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + ], |
| 71 | + }), |
| 72 | + }; |
| 73 | +}); |
| 74 | + |
| 75 | +import { MetadataResourceEditPage } from './ResourceEditPage'; |
| 76 | + |
| 77 | +const START_PATH = '/metadata/object/showcase_account'; |
| 78 | + |
| 79 | +function editor(language: 'zh' | 'en') { |
| 80 | + return render( |
| 81 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}> |
| 82 | + <MemoryRouter initialEntries={[START_PATH]}> |
| 83 | + <Routes> |
| 84 | + <Route |
| 85 | + path="/metadata/:type/:name" |
| 86 | + element={<MetadataResourceEditPage type="object" name="showcase_account" />} |
| 87 | + /> |
| 88 | + </Routes> |
| 89 | + </MemoryRouter> |
| 90 | + </I18nProvider>, |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +/** Open the References sheet (the lazy load) and return its table's header cells. */ |
| 95 | +async function headersIn(locale: 'zh-CN' | 'en-US') { |
| 96 | + await waitFor(() => expect(mockClient.layered).toHaveBeenCalled()); |
| 97 | + fireEvent.click(await screen.findByTitle(t('engine.edit.references', locale))); |
| 98 | + const name = await screen.findByText('all_accounts'); |
| 99 | + const table = name.closest('table') as HTMLElement; |
| 100 | + return within(table).getAllByRole('columnheader').map((th) => th.textContent); |
| 101 | +} |
| 102 | + |
| 103 | +afterEach(() => { |
| 104 | + cleanup(); |
| 105 | + vi.clearAllMocks(); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('References sheet column headers (#25916)', () => { |
| 109 | + it('guard: the zh rows are real zh, not English copies', () => { |
| 110 | + for (const key of ['engine.edit.refsColFromType', 'engine.edit.refsColFromName', 'engine.edit.refsColPath']) { |
| 111 | + expect(t(key, 'zh-CN')).not.toBe(t(key, 'en-US')); |
| 112 | + expect(t(key, 'zh-CN')).toMatch(/\p{Script=Han}/u); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + it('a zh session reads the column names in zh', async () => { |
| 117 | + editor('zh'); |
| 118 | + expect(await headersIn('zh-CN')).toEqual([ |
| 119 | + t('engine.edit.refsColFromType', 'zh-CN'), |
| 120 | + t('engine.edit.refsColFromName', 'zh-CN'), |
| 121 | + t('engine.edit.refsColPath', 'zh-CN'), |
| 122 | + ]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('control: an English session still reads From type / From name / Path', async () => { |
| 126 | + editor('en'); |
| 127 | + expect(await headersIn('en-US')).toEqual(['From type', 'From name', 'Path']); |
| 128 | + }); |
| 129 | +}); |
0 commit comments