|
22 | 22 | */ |
23 | 23 |
|
24 | 24 | import '@testing-library/jest-dom/vitest'; |
| 25 | +import * as React from 'react'; |
25 | 26 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
26 | 27 | import { render, screen, cleanup, fireEvent, waitFor, within } from '@testing-library/react'; |
27 | 28 | import { MemoryRouter } from 'react-router-dom'; |
| 29 | +import { createI18n, I18nProvider, transformSpecTranslations } from '@object-ui/i18n'; |
| 30 | +// Pays the dynamic `@objectstack/lint` load the pillar's source reaches at collection, not inside a case (check:test-source-alias). |
| 31 | +import '@objectstack/lint'; |
28 | 32 |
|
29 | 33 | const PKG = 'com.acme.app'; |
30 | 34 | const LONG_LABEL = 'Escalate overdue high-priority support cases to the regional on-call manager'; |
@@ -100,16 +104,12 @@ afterEach(() => { |
100 | 104 | vi.unstubAllGlobals(); |
101 | 105 | }); |
102 | 106 |
|
103 | | -/** The rail (the `nav` holding the search box) once every row has rendered. */ |
104 | | -async function renderRail(): Promise<HTMLElement> { |
105 | | - render( |
106 | | - <MemoryRouter initialEntries={[`/studio/${PKG}/automations`]}> |
107 | | - <AutomationsPillar packageId={PKG} /> |
108 | | - </MemoryRouter>, |
109 | | - ); |
110 | | - const search = await screen.findByPlaceholderText(t('engine.studio.designer.search', 'en'), undefined, { timeout: 8000 }); |
| 107 | +/** The rail (the `nav` holding the search box) once every row has rendered; `wrap` puts a provider around the pillar. */ |
| 108 | +async function renderRail(wrap: (pillar: React.ReactElement) => React.ReactElement = (pillar) => pillar): Promise<HTMLElement> { |
| 109 | + render(<MemoryRouter initialEntries={[`/studio/${PKG}/automations`]}>{wrap(<AutomationsPillar packageId={PKG} />)}</MemoryRouter>); |
| 110 | + const search = await screen.findByTestId('auto-rail-search', undefined, { timeout: 8000 }); |
111 | 111 | const rail = search.closest('nav') as HTMLElement; |
112 | | - await waitFor(() => expect(within(rail).getByRole('button', { name: /welcome_new_member/ })).toBeInTheDocument(), { |
| 112 | + await waitFor(() => expect(rail.querySelector('[data-testid="auto-rail-row-welcome_new_member"]')).toBeInTheDocument(), { |
113 | 113 | timeout: 8000, |
114 | 114 | }); |
115 | 115 | return rail; |
@@ -173,3 +173,50 @@ describe('a flow’s name in the Automations rail is not cut to a stub (objectui |
173 | 173 | expect(row.getAttribute('title') ?? '').toMatch(new RegExp(`^${LONG_LABEL} · `)); |
174 | 174 | }); |
175 | 175 | }); |
| 176 | + |
| 177 | +describe('a rail row names its flow by the package’s translated label (objectstack#35319)', () => { |
| 178 | + // The package's zh-CN pack: a label for two of the four flows. The other two keep their authored copy. |
| 179 | + const ZH_CN = { |
| 180 | + flows: { |
| 181 | + nightly_digest: { label: '夜间摘要' }, |
| 182 | + welcome_new_member: { label: '欢迎新成员' }, |
| 183 | + }, |
| 184 | + }; |
| 185 | + /** The console's i18n with the pack loaded under zh-CN, speaking `language`. */ |
| 186 | + const withPack = (language: string) => (pillar: React.ReactElement) => { |
| 187 | + const instance = createI18n({ defaultLanguage: language, detectBrowserLanguage: false }); |
| 188 | + instance.addResourceBundle('zh-CN', 'translation', transformSpecTranslations(ZH_CN), true, true); |
| 189 | + return ( |
| 190 | + <I18nProvider instance={instance} persistLanguage={false}> |
| 191 | + {pillar} |
| 192 | + </I18nProvider> |
| 193 | + ); |
| 194 | + }; |
| 195 | + // By its test id: under a zh-CN provider the box's placeholder is the Chinese one. |
| 196 | + const search = (rail: HTMLElement, value: string) => |
| 197 | + fireEvent.change(within(rail).getByTestId('auto-rail-search'), { target: { value } }); |
| 198 | + |
| 199 | + it('zh-CN: a flow the pack labels reads by that label, on the row and in its title; the rest stay authored', async () => { |
| 200 | + const rail = await renderRail(withPack('zh-CN')); |
| 201 | + expect(rowNames(rail)).toEqual(['夜间摘要', 'Approve order', LONG_LABEL, '欢迎新成员']); |
| 202 | + const row = rail.querySelector('[data-testid="auto-rail-row-nightly_digest"]') as HTMLElement; |
| 203 | + expect(row.getAttribute('title') ?? '').toMatch(/^夜间摘要 · /); |
| 204 | + }); |
| 205 | + |
| 206 | + it('zh-CN: the search matches the label the row shows, and still the authored label and the machine name', async () => { |
| 207 | + const rail = await renderRail(withPack('zh-CN')); |
| 208 | + search(rail, '夜间'); |
| 209 | + expect(rowNames(rail)).toEqual(['夜间摘要']); |
| 210 | + search(rail, 'DIGEST'); |
| 211 | + expect(rowNames(rail)).toEqual(['夜间摘要']); |
| 212 | + search(rail, 'welcome_new'); |
| 213 | + expect(rowNames(rail)).toEqual(['欢迎新成员']); |
| 214 | + }); |
| 215 | + |
| 216 | + it('en: the zh-CN pack does not speak for English, so every row reads as authored', async () => { |
| 217 | + const rail = await renderRail(withPack('en')); |
| 218 | + expect(rowNames(rail)).toEqual(['Nightly digest', 'Approve order', LONG_LABEL, 'welcome_new_member']); |
| 219 | + search(rail, '夜间'); |
| 220 | + expect(rowNames(rail)).toEqual([]); |
| 221 | + }); |
| 222 | +}); |
0 commit comments