|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * #25916, third pass — two renderers in this package drew English literals |
| 11 | + * whatever the session's language. |
| 12 | + * |
| 13 | + * - `data-table`: the hover "open record" affordance's title and the inline |
| 14 | + * edit row's cancel / save buttons (`table.openRecord`, |
| 15 | + * `table.cancelChanges`, `table.saveRow`); |
| 16 | + * - the `kind:'react'` page renderer: its capability notice, its empty-source |
| 17 | + * refusal, its loading line and its error titles (`reactPage.*`). |
| 18 | + * |
| 19 | + * What this file pins, through the real renderers and a real `I18nProvider`: |
| 20 | + * - under zh each surface draws the zh pack's row (expectations read from the |
| 21 | + * pack, guarded as real zh rows); |
| 22 | + * - a sentence with an inline `<code>` is ONE translated string: the code |
| 23 | + * sits where the zh string put it, and no `{{slot}}` or private-use mark |
| 24 | + * is left in the text; |
| 25 | + * - control: with no provider each surface still reads its English. |
| 26 | + */ |
| 27 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 28 | +import { cleanup, fireEvent, render, screen, within } from '@testing-library/react'; |
| 29 | +import React from 'react'; |
| 30 | +import { CAP_REACT_PAGES, ComponentRegistry, disableCapability, enableCapability } from '@object-ui/core'; |
| 31 | +import { AdapterCtx, SchemaRenderer } from '@object-ui/react'; |
| 32 | +import { I18nProvider } from '@object-ui/i18n'; |
| 33 | +import { builtInLocales } from '@object-ui/i18n/locales'; |
| 34 | +import type { PageDocumentNode } from '@object-ui/types'; |
| 35 | +// Registers the renderers at module scope, NOT inside a `beforeAll`; see |
| 36 | +// object-ui/no-dynamic-import-in-test-hook (objectui#3010/#3021). |
| 37 | +import '../renderers'; |
| 38 | + |
| 39 | +const { zh, en } = builtInLocales; |
| 40 | + |
| 41 | +/** No `{{slot}}` and no private-use slot mark survives into the rendered text. */ |
| 42 | +const LEAK = /\{\{|[\u{E000}-\u{F8FF}]/u; |
| 43 | + |
| 44 | +function withLanguage(element: React.ReactElement, language: 'zh' | null) { |
| 45 | + return render( |
| 46 | + language ? ( |
| 47 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}> |
| 48 | + {element} |
| 49 | + </I18nProvider> |
| 50 | + ) : ( |
| 51 | + element |
| 52 | + ), |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +const ALPHA = { id: 'a', name: 'Alpha', qty: 10 }; |
| 57 | + |
| 58 | +function DataTable({ onRowClick }: { onRowClick?: () => void }) { |
| 59 | + const Table = ComponentRegistry.get('data-table')!; |
| 60 | + const schema = { |
| 61 | + type: 'data-table', |
| 62 | + editable: true, |
| 63 | + singleClickEdit: true, |
| 64 | + rowActions: true, |
| 65 | + showRowNumbers: true, |
| 66 | + columns: [ |
| 67 | + { header: 'Task', accessorKey: 'name', editable: false }, |
| 68 | + { header: 'Estimate', accessorKey: 'qty', type: 'number' }, |
| 69 | + ], |
| 70 | + data: [ALPHA], |
| 71 | + onRowSave: async () => undefined, |
| 72 | + ...(onRowClick ? { onRowClick } : {}), |
| 73 | + } as any; |
| 74 | + return <Table schema={schema} />; |
| 75 | +} |
| 76 | + |
| 77 | +/** Stage an edit on Alpha's estimate, so the row draws its cancel / save buttons. */ |
| 78 | +function stageEdit(container: HTMLElement) { |
| 79 | + const cell = container.querySelectorAll('tbody tr td')[2] as HTMLElement; |
| 80 | + fireEvent.click(cell); |
| 81 | + const input = cell.querySelector('input') as HTMLInputElement; |
| 82 | + fireEvent.change(input, { target: { value: '11' } }); |
| 83 | + fireEvent.blur(input); |
| 84 | +} |
| 85 | + |
| 86 | +const PAGE = (source: string): PageDocumentNode => ({ |
| 87 | + type: 'home', |
| 88 | + kind: 'react', |
| 89 | + name: 'locale_page', |
| 90 | + label: 'Locale page', |
| 91 | + source, |
| 92 | +}); |
| 93 | + |
| 94 | +function ReactPage({ source }: { source: string }) { |
| 95 | + return ( |
| 96 | + <AdapterCtx.Provider value={{ find: async () => [] } as any}> |
| 97 | + <SchemaRenderer schema={PAGE(source)} /> |
| 98 | + </AdapterCtx.Provider> |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +afterEach(() => { |
| 103 | + cleanup(); |
| 104 | + disableCapability(CAP_REACT_PAGES); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('renderer copy under zh (#25916)', () => { |
| 108 | + it('guard: the zh values read below are real zh rows, not English copies', () => { |
| 109 | + for (const [zhValue, enValue] of [ |
| 110 | + [zh.table.openRecord, en.table.openRecord], |
| 111 | + [zh.table.cancelChanges, en.table.cancelChanges], |
| 112 | + [zh.table.saveRow, en.table.saveRow], |
| 113 | + [zh.reactPage.disabledTitle, en.reactPage.disabledTitle], |
| 114 | + [zh.reactPage.disabledBody, en.reactPage.disabledBody], |
| 115 | + [zh.reactPage.sourceRequired, en.reactPage.sourceRequired], |
| 116 | + ]) { |
| 117 | + expect(zhValue).not.toBe(enValue); |
| 118 | + expect(zhValue).toMatch(/\p{Script=Han}/u); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + it('data-table: the open affordance and the edited row buttons are titled in zh', () => { |
| 123 | + const { container } = withLanguage(<DataTable onRowClick={() => undefined} />, 'zh'); |
| 124 | + expect(screen.getByTestId('row-expand-button').getAttribute('title')).toBe(zh.table.openRecord); |
| 125 | + stageEdit(container); |
| 126 | + const row = container.querySelector('tbody tr') as HTMLElement; |
| 127 | + expect(within(row).getByTitle(zh.table.cancelChanges)).toBeTruthy(); |
| 128 | + expect(within(row).getByTitle(zh.table.saveRow)).toBeTruthy(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('react page, capability off: the notice is zh, its three <code> chips where the zh string put them', async () => { |
| 132 | + const { container } = withLanguage(<ReactPage source="function Page() { return null; }" />, 'zh'); |
| 133 | + expect(await screen.findByText(zh.reactPage.disabledTitle)).toBeTruthy(); |
| 134 | + const flag = screen.getByText('OS_PAGE_REACT_ENABLED=true', { selector: 'code' }); |
| 135 | + expect(flag.parentElement!.textContent).toBe( |
| 136 | + zh.reactPage.disabledBody |
| 137 | + .replace('{{kind}}', "kind:'react'") |
| 138 | + .replace('{{flag}}', 'OS_PAGE_REACT_ENABLED=true') |
| 139 | + .replace('{{call}}', "enableCapability('react-pages')"), |
| 140 | + ); |
| 141 | + expect(container.textContent).not.toMatch(LEAK); |
| 142 | + }); |
| 143 | + |
| 144 | + it('react page, capability on: an empty source is refused in zh, its chips in place', async () => { |
| 145 | + enableCapability(CAP_REACT_PAGES); |
| 146 | + const { container } = withLanguage(<ReactPage source=" " />, 'zh'); |
| 147 | + const source = await screen.findByText('source', { selector: 'code' }); |
| 148 | + expect(source.parentElement!.textContent).toBe( |
| 149 | + zh.reactPage.sourceRequired.replace('{{kind}}', "kind:'react'").replace('{{source}}', 'source'), |
| 150 | + ); |
| 151 | + expect(container.textContent).not.toMatch(LEAK); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +describe('control: with no provider each surface still reads its English (#25916)', () => { |
| 156 | + it('data-table', () => { |
| 157 | + const { container } = withLanguage(<DataTable onRowClick={() => undefined} />, null); |
| 158 | + expect(screen.getByTestId('row-expand-button').getAttribute('title')).toBe('Open record'); |
| 159 | + stageEdit(container); |
| 160 | + expect(screen.getByTitle('Cancel changes')).toBeTruthy(); |
| 161 | + expect(screen.getByTitle('Save row')).toBeTruthy(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('react page notice', async () => { |
| 165 | + withLanguage(<ReactPage source="function Page() { return null; }" />, null); |
| 166 | + const flag = await screen.findByText('OS_PAGE_REACT_ENABLED=true', { selector: 'code' }); |
| 167 | + expect(screen.getByText('React pages are disabled on this deployment')).toBeTruthy(); |
| 168 | + expect(flag.parentElement!.textContent).toBe( |
| 169 | + "kind:'react' pages execute author JavaScript directly in the application, so they are off unless the deployment turns them on. An operator whose page authors are trusted sets OS_PAGE_REACT_ENABLED=true on the server (a host embedding the renderer calls enableCapability('react-pages')).", |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('react page empty source', async () => { |
| 174 | + enableCapability(CAP_REACT_PAGES); |
| 175 | + withLanguage(<ReactPage source=" " />, null); |
| 176 | + const source = await screen.findByText('source', { selector: 'code' }); |
| 177 | + expect(source.parentElement!.textContent).toBe("A kind:'react' page requires a non-empty source."); |
| 178 | + }); |
| 179 | +}); |
0 commit comments