|
| 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 — both React owners of an `ActionRunner` install the |
| 11 | + * viewer's display locale on it (`useActionRunnerDisplayLocale`), so a copy |
| 12 | + * template's formatters render in `useDisplayLocale()` instead of `en-US`. |
| 13 | + * |
| 14 | + * Measured by running a real action through a real provider under a real |
| 15 | + * `I18nProvider` and `LocalizationProvider`, and reading what the host's |
| 16 | + * confirm handler and `onToast` receive. Expected strings come from `Intl` |
| 17 | + * itself, so a newer ICU cannot drift them. |
| 18 | + */ |
| 19 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 20 | +import { act, cleanup, renderHook } from '@testing-library/react'; |
| 21 | +import React from 'react'; |
| 22 | +import { I18nProvider, LocalizationProvider, useObjectTranslation } from '@object-ui/i18n'; |
| 23 | +import type { ActionResult, ToastHandler } from '@object-ui/core'; |
| 24 | +import { ActionProvider, useAction } from '../ActionContext'; |
| 25 | +import { useActionRunner } from '../../hooks/useActionRunner'; |
| 26 | + |
| 27 | +afterEach(() => cleanup()); |
| 28 | + |
| 29 | +const AMOUNT = 1234567.5; |
| 30 | +const money = (tag: string) => new Intl.NumberFormat(tag, { style: 'currency', currency: 'EUR' }).format(AMOUNT); |
| 31 | +const CONFIG = { defaultLanguage: 'en', detectBrowserLanguage: false } as const; |
| 32 | +const SUCCEEDS = async (): Promise<ActionResult> => ({ success: true, data: { total: AMOUNT } }); |
| 33 | +const SUCCESS_COPY = { dialect: 'template' as const, source: 'Total {{ result.total | currency:EUR }}' }; |
| 34 | + |
| 35 | +function providerHarness(locale: string | undefined, onToast: ToastHandler, onConfirm?: (m: string) => Promise<boolean>) { |
| 36 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 37 | + <I18nProvider config={CONFIG} persistLanguage={false}> |
| 38 | + <LocalizationProvider value={{ locale }}> |
| 39 | + <ActionProvider |
| 40 | + context={{ record: { amount: AMOUNT } }} |
| 41 | + onToast={onToast} |
| 42 | + onConfirm={onConfirm} |
| 43 | + handlers={{ script: SUCCEEDS }} |
| 44 | + > |
| 45 | + {children} |
| 46 | + </ActionProvider> |
| 47 | + </LocalizationProvider> |
| 48 | + </I18nProvider> |
| 49 | + ); |
| 50 | + return renderHook(() => ({ action: useAction(), i18n: useObjectTranslation() }), { wrapper }); |
| 51 | +} |
| 52 | + |
| 53 | +describe('<ActionProvider> — copy templates format in the display locale (objectstack#26232)', () => { |
| 54 | + it('successMessage formats in the tenant display locale', async () => { |
| 55 | + expect(money('de-DE')).not.toBe(money('en-US')); |
| 56 | + const onToast = vi.fn<ToastHandler>(); |
| 57 | + const { result } = providerHarness('de-DE', onToast); |
| 58 | + |
| 59 | + await act(async () => { |
| 60 | + await result.current.action.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY }); |
| 61 | + }); |
| 62 | + |
| 63 | + expect(onToast).toHaveBeenCalledWith(`Total ${money('de-DE')}`, expect.objectContaining({ type: 'success' })); |
| 64 | + }); |
| 65 | + |
| 66 | + it('confirmText formats in the display locale; a `locale` param is a value it prints', async () => { |
| 67 | + const onConfirm = vi.fn(async (_message: string) => false); |
| 68 | + const { result } = providerHarness('de-DE', vi.fn<ToastHandler>(), onConfirm); |
| 69 | + |
| 70 | + await act(async () => { |
| 71 | + await result.current.action.execute({ |
| 72 | + type: 'script', |
| 73 | + name: 'reprice', |
| 74 | + confirmText: 'Set {{ record.amount | currency:EUR }} ({{ params.locale }})?', |
| 75 | + params: { locale: 'fr-FR' }, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + expect(onConfirm.mock.calls[0][0]).toBe(`Set ${money('de-DE')} (fr-FR)?`); |
| 80 | + }); |
| 81 | + |
| 82 | + it('with no tenant locale, the UI language is the display locale, and a switch reaches the built runner', async () => { |
| 83 | + const onToast = vi.fn<ToastHandler>(); |
| 84 | + const { result } = providerHarness(undefined, onToast); |
| 85 | + const runnerBefore = result.current.action.runner; |
| 86 | + |
| 87 | + await act(async () => { |
| 88 | + await result.current.action.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY }); |
| 89 | + }); |
| 90 | + expect(onToast).toHaveBeenLastCalledWith(`Total ${money('en')}`, expect.anything()); |
| 91 | + |
| 92 | + await act(async () => { |
| 93 | + await result.current.i18n.changeLanguage('ja'); |
| 94 | + }); |
| 95 | + expect(result.current.action.runner).toBe(runnerBefore); |
| 96 | + |
| 97 | + await act(async () => { |
| 98 | + await result.current.action.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY }); |
| 99 | + }); |
| 100 | + expect(onToast).toHaveBeenLastCalledWith(`Total ${money('ja')}`, expect.anything()); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('useActionRunner — copy templates format in the display locale (objectstack#26232)', () => { |
| 105 | + it('successMessage formats in the tenant display locale', async () => { |
| 106 | + const onToast = vi.fn<ToastHandler>(); |
| 107 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 108 | + <I18nProvider config={CONFIG} persistLanguage={false}> |
| 109 | + <LocalizationProvider value={{ locale: 'de-DE' }}>{children}</LocalizationProvider> |
| 110 | + </I18nProvider> |
| 111 | + ); |
| 112 | + const { result } = renderHook(() => useActionRunner({ context: {}, onToast }), { wrapper }); |
| 113 | + result.current.runner.registerHandler('script', SUCCEEDS); |
| 114 | + |
| 115 | + await act(async () => { |
| 116 | + await result.current.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY }); |
| 117 | + }); |
| 118 | + |
| 119 | + expect(onToast).toHaveBeenCalledWith(`Total ${money('de-DE')}`, expect.objectContaining({ type: 'success' })); |
| 120 | + }); |
| 121 | +}); |
0 commit comments