|
| 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#29249 — an inline `element:button` action's `description` |
| 11 | + * reaches the param dialog in the user's language. |
| 12 | + * |
| 13 | + * `InlineActionSchema` picks `description` (an `I18nLabel`: a string or a |
| 14 | + * per-locale map). The button hands its action to `useActionTextLocalizer` |
| 15 | + * before the runner, so a map collapses to the active language there; the |
| 16 | + * runner and the param-collection host have no language of their own. |
| 17 | + * |
| 18 | + * The path is the real one up to the param-collection seam: the registered |
| 19 | + * `element:button`, the real localizer, the real `ActionRunner` behind |
| 20 | + * `ActionProvider`. The dialog is this file's host, drawn with this package's |
| 21 | + * `Dialog` the way the console's `ActionParamDialog` (`@object-ui/app-shell`, |
| 22 | + * which this package cannot import) draws its line: the action's description, |
| 23 | + * else the generic `actionDialog.description`. |
| 24 | + * |
| 25 | + * Each row first asserts the dialog opened with the action's own title (the |
| 26 | + * positive control), so a missing line is never "no dialog opened". |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'; |
| 30 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 31 | +import React from 'react'; |
| 32 | +import { ComponentRegistry } from '@object-ui/core'; |
| 33 | +import type { ActionContext, ActionDef, ActionResult, ParamCollectionHandler } from '@object-ui/core'; |
| 34 | +import { ActionProvider } from '@object-ui/react'; |
| 35 | +import { I18nProvider, useObjectTranslation } from '@object-ui/i18n'; |
| 36 | +import { Dialog, DialogContent, DialogTitle, DialogDescription } from '../ui/dialog'; |
| 37 | +import '../renderers/basic/elements'; |
| 38 | + |
| 39 | +let api: Mock<(action: ActionDef, ctx: ActionContext) => Promise<ActionResult>>; |
| 40 | +let handed: Array<ActionDef | undefined>; |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + api = vi.fn(async () => ({ success: true })); |
| 44 | + handed = []; |
| 45 | +}); |
| 46 | + |
| 47 | +/** The producer's shape: a nameless inline `api` action that collects params. */ |
| 48 | +const INVITE = { |
| 49 | + type: 'api', |
| 50 | + target: '/api/v1/cloud/invitations', |
| 51 | + label: { en: 'Invite member', 'zh-CN': '邀请成员' }, |
| 52 | + params: [{ name: 'email', label: 'Email', type: 'email', required: true }], |
| 53 | +}; |
| 54 | + |
| 55 | +const DESCRIPTION = { |
| 56 | + en: 'We e-mail them an invitation link.', |
| 57 | + 'zh-CN': '我们会向对方发送一封邀请邮件。', |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * The registered renderer, read once at module scope (a component created |
| 62 | + * during render remounts on every render). An unregistered `element:button` |
| 63 | + * fails the whole file here, before any row runs. |
| 64 | + */ |
| 65 | +function registeredElementButton() { |
| 66 | + const C = ComponentRegistry.get('element:button'); |
| 67 | + if (!C) throw new Error('element:button not registered'); |
| 68 | + return C; |
| 69 | +} |
| 70 | +const ElementButton = registeredElementButton(); |
| 71 | + |
| 72 | +/** |
| 73 | + * The param-collection host: opens a dialog from what the runner hands it and |
| 74 | + * leaves the promise pending, as a dialog awaiting its Confirm does. |
| 75 | + */ |
| 76 | +function ParamHost({ action }: { action: Record<string, unknown> }) { |
| 77 | + const { t } = useObjectTranslation(); |
| 78 | + const [dialog, setDialog] = React.useState<{ title: string; description: string } | null>(null); |
| 79 | + const onParamCollection = React.useCallback<ParamCollectionHandler>((_params, dispatched) => { |
| 80 | + handed.push(dispatched); |
| 81 | + // A non-string reaching the host is drawn as nothing, so the assertions |
| 82 | + // below see the gap instead of React refusing an object child. |
| 83 | + const title = dispatched?.label; |
| 84 | + const description = dispatched?.description; |
| 85 | + setDialog({ |
| 86 | + title: typeof title === 'string' ? title : '', |
| 87 | + description: typeof description === 'string' ? description : '', |
| 88 | + }); |
| 89 | + return new Promise(() => {}); |
| 90 | + }, []); |
| 91 | + return ( |
| 92 | + <ActionProvider handlers={{ api }} onParamCollection={onParamCollection}> |
| 93 | + <ElementButton |
| 94 | + schema={{ |
| 95 | + type: 'element:button', |
| 96 | + properties: { label: { en: 'Invite member', 'zh-CN': '邀请成员' }, action }, |
| 97 | + }} |
| 98 | + /> |
| 99 | + {dialog && ( |
| 100 | + <Dialog open> |
| 101 | + <DialogContent> |
| 102 | + <DialogTitle>{dialog.title}</DialogTitle> |
| 103 | + <DialogDescription>{dialog.description || t('actionDialog.description')}</DialogDescription> |
| 104 | + </DialogContent> |
| 105 | + </Dialog> |
| 106 | + )} |
| 107 | + </ActionProvider> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function renderIn(language: string, action: Record<string, unknown>) { |
| 112 | + return render( |
| 113 | + <I18nProvider config={{ defaultLanguage: language, detectBrowserLanguage: false }} persistLanguage={false}> |
| 114 | + <ParamHost action={action} /> |
| 115 | + </I18nProvider>, |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +describe('element:button — the param dialog shows the inline description in the active language (objectstack#29249)', () => { |
| 120 | + it('a zh-CN/en map shows the zh-CN string under a zh-CN locale', async () => { |
| 121 | + renderIn('zh-CN', { ...INVITE, description: DESCRIPTION }); |
| 122 | + |
| 123 | + fireEvent.click(screen.getByRole('button', { name: '邀请成员' })); |
| 124 | + |
| 125 | + const dialog = await screen.findByRole('dialog'); |
| 126 | + expect(screen.getByRole('heading', { name: '邀请成员' })).toBeTruthy(); |
| 127 | + expect(dialog.textContent).toContain('我们会向对方发送一封邀请邮件。'); |
| 128 | + // The runner was handed one string, never the map. |
| 129 | + expect(handed).toHaveLength(1); |
| 130 | + expect(handed[0]?.description).toBe('我们会向对方发送一封邀请邮件。'); |
| 131 | + // Nothing runs until the dialog's own Confirm. |
| 132 | + expect(api).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('the same map shows the English string under an en locale', async () => { |
| 136 | + renderIn('en', { ...INVITE, description: DESCRIPTION }); |
| 137 | + |
| 138 | + fireEvent.click(screen.getByRole('button', { name: 'Invite member' })); |
| 139 | + |
| 140 | + const dialog = await screen.findByRole('dialog'); |
| 141 | + expect(screen.getByRole('heading', { name: 'Invite member' })).toBeTruthy(); |
| 142 | + expect(dialog.textContent).toContain('We e-mail them an invitation link.'); |
| 143 | + expect(handed[0]?.description).toBe('We e-mail them an invitation link.'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('no description: the dialog keeps its generic line', async () => { |
| 147 | + renderIn('zh-CN', INVITE); |
| 148 | + |
| 149 | + fireEvent.click(screen.getByRole('button', { name: '邀请成员' })); |
| 150 | + |
| 151 | + const dialog = await screen.findByRole('dialog'); |
| 152 | + expect(screen.getByRole('heading', { name: '邀请成员' })).toBeTruthy(); |
| 153 | + expect(dialog.textContent).toContain('请填写以下信息以继续操作。'); |
| 154 | + expect(handed[0]?.description).toBeUndefined(); |
| 155 | + }); |
| 156 | +}); |
0 commit comments