|
| 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 | + * objectstack#26232 — a copy template's formatters render in the viewer's |
| 11 | + * display locale, and a root named `locale` never sets it. |
| 12 | + * |
| 13 | + * Measured before this card: `fillTemplateHoles` passed its roots as the |
| 14 | + * template engine's `extra`, and the engine read its formatter locale from |
| 15 | + * `extra.locale`. So a param named `locale` set the locale of every formatted |
| 16 | + * value in the sentence, and with no such param every viewer read `en-US`. |
| 17 | + * The locale now rides the engine's reserved channel: the host installs its |
| 18 | + * display locale on the runner (`setDisplayLocale`), and the filler passes it |
| 19 | + * apart from the roots. |
| 20 | + * |
| 21 | + * Expected strings come from `Intl` itself, so a newer ICU cannot drift them. |
| 22 | + */ |
| 23 | +import { describe, it, expect, vi } from 'vitest'; |
| 24 | +import { ActionRunner, type ActionDef } from '../ActionRunner'; |
| 25 | +import { fillTemplateHoles } from '../actionCopyTemplate'; |
| 26 | + |
| 27 | +const AMOUNT = 1234567.5; |
| 28 | +const money = (tag: string) => new Intl.NumberFormat(tag, { style: 'currency', currency: 'EUR' }).format(AMOUNT); |
| 29 | + |
| 30 | +async function confirmTextFor(runner: ActionRunner, def: ActionDef, callContext?: object): Promise<unknown> { |
| 31 | + const confirm = vi.fn().mockResolvedValue(false); |
| 32 | + runner.setConfirmHandler(confirm); |
| 33 | + await runner.execute(def, callContext); |
| 34 | + expect(confirm).toHaveBeenCalledTimes(1); |
| 35 | + return confirm.mock.calls[0][0]; |
| 36 | +} |
| 37 | + |
| 38 | +describe('fillTemplateHoles keeps the formatter locale apart from the roots (objectstack#26232)', () => { |
| 39 | + it('a root named `locale` is a value a hole prints, not the formatter locale', () => { |
| 40 | + expect(money('de-DE')).not.toBe(money('en-US')); |
| 41 | + expect(fillTemplateHoles('{{ amount | currency:EUR }} ({{ locale }})', { amount: AMOUNT, locale: 'de-DE' })) |
| 42 | + .toBe(`${money('en-US')} (de-DE)`); |
| 43 | + }); |
| 44 | + |
| 45 | + it('formats in the locale the caller passes', () => { |
| 46 | + expect(fillTemplateHoles('{{ amount | currency:EUR }}', { amount: AMOUNT }, 'de-DE')).toBe(money('de-DE')); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('the runner fills its copy templates in the display locale it was given (objectstack#26232)', () => { |
| 51 | + const CONFIRM: ActionDef = { |
| 52 | + name: 'reprice', |
| 53 | + onClick: () => {}, |
| 54 | + confirmText: 'Set {{ record.amount | currency:EUR }} ({{ params.locale }})?', |
| 55 | + params: { locale: 'fr-FR' }, |
| 56 | + }; |
| 57 | + const runner = () => new ActionRunner({ objectName: 'crm_deal', record: { amount: AMOUNT } }); |
| 58 | + |
| 59 | + it('CONTROL: with no display locale installed, the formatters render en-US as before', async () => { |
| 60 | + expect(await confirmTextFor(runner(), CONFIRM)).toBe(`Set ${money('en-US')} (fr-FR)?`); |
| 61 | + }); |
| 62 | + |
| 63 | + it('confirmText: the installed display locale is the formatter locale', async () => { |
| 64 | + const r = runner(); |
| 65 | + r.setDisplayLocale(() => 'de-DE'); |
| 66 | + expect(await confirmTextFor(r, CONFIRM)).toBe(`Set ${money('de-DE')} (fr-FR)?`); |
| 67 | + }); |
| 68 | + |
| 69 | + it('the display locale is read per fill, so a switch reaches a runner built before it', async () => { |
| 70 | + const r = runner(); |
| 71 | + let current = 'de-DE'; |
| 72 | + r.setDisplayLocale(() => current); |
| 73 | + expect(await confirmTextFor(r, CONFIRM)).toBe(`Set ${money('de-DE')} (fr-FR)?`); |
| 74 | + current = 'ja-JP'; |
| 75 | + expect(await confirmTextFor(r, CONFIRM)).toBe(`Set ${money('ja-JP')} (fr-FR)?`); |
| 76 | + }); |
| 77 | + |
| 78 | + it('a dispatch with a callContext keeps the display locale', async () => { |
| 79 | + const r = new ActionRunner({ objectName: 'crm_deal' }); |
| 80 | + r.setDisplayLocale(() => 'de-DE'); |
| 81 | + expect(await confirmTextFor(r, CONFIRM, { record: { amount: AMOUNT } })).toBe(`Set ${money('de-DE')} (fr-FR)?`); |
| 82 | + }); |
| 83 | + |
| 84 | + it('successMessage: formats `result` in the display locale', async () => { |
| 85 | + const r = runner(); |
| 86 | + const toast = vi.fn(); |
| 87 | + r.setToastHandler(toast); |
| 88 | + r.setDisplayLocale(() => 'de-DE'); |
| 89 | + r.registerHandler('script', async () => ({ success: true, data: { total: AMOUNT, locale: 'fr-FR' } })); |
| 90 | + await r.execute({ |
| 91 | + type: 'script', |
| 92 | + name: 'reprice', |
| 93 | + successMessage: { dialect: 'template', source: 'Total {{ result.total | currency:EUR }} ({{ result.locale }})' }, |
| 94 | + }); |
| 95 | + expect(toast.mock.calls[0][0]).toBe(`Total ${money('de-DE')} (fr-FR)`); |
| 96 | + }); |
| 97 | +}); |
0 commit comments