|
| 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 | + * `useConsoleActionRuntime`'s param-collection handler resolves the action's |
| 11 | + * `submitText` — the verb on the param dialog's submit button — the way it |
| 12 | + * resolves `description` (objectstack#28158): |
| 13 | + * |
| 14 | + * 1. the translation bundle's `objects.OBJECT._actions.ACTION.submitText` |
| 15 | + * (`globalActions.ACTION.submitText` for an object-less action), through |
| 16 | + * `useObjectLabel().actionSubmitText`; |
| 17 | + * 2. else the authored inline locale map, collapsed to the active language; |
| 18 | + * 3. else the authored string. |
| 19 | + * |
| 20 | + * The bundle lookup itself (which node an action reads) is `@object-ui/i18n`'s |
| 21 | + * and is pinned there (`useObjectLabel-actionSubmitText-28158.test.tsx`). Here |
| 22 | + * the resolver is a stand-in that records what it is asked, so this file pins |
| 23 | + * the HANDLER's half: which key object and name it asks with, that the |
| 24 | + * fallback it offers is already one string in the active language, and that |
| 25 | + * the answer reaches the dialog. `RecordDetailView`'s handler, the second |
| 26 | + * reader of the same seam, has its own copy of these pins. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 30 | +import { render, act } from '@testing-library/react'; |
| 31 | +import React from 'react'; |
| 32 | + |
| 33 | +vi.mock('react-router-dom', () => ({ useNavigate: () => vi.fn() })); |
| 34 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 35 | + ...(await importOriginal<Record<string, unknown>>()), |
| 36 | + useAuth: () => ({ user: { id: 'u1', name: 'User', image: null }, activeOrganization: null }), |
| 37 | + createAuthenticatedFetch: () => vi.fn(), |
| 38 | +})); |
| 39 | + |
| 40 | +/** The bundle, keyed `KEY_OBJECT|ACTION` (`global` for no key object). */ |
| 41 | +let bundle: Record<string, string> = {}; |
| 42 | +const asked: Array<{ objectName: string | undefined; actionName: string | undefined; fallback: unknown }> = []; |
| 43 | +const LANGUAGE = 'zh-CN'; |
| 44 | + |
| 45 | +// Partial mock: `@object-ui/components` reaches for `createSafeTranslation` at |
| 46 | +// import time, so a total double breaks the module graph. |
| 47 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 48 | + ...(await importOriginal<typeof import('@object-ui/i18n')>()), |
| 49 | + useObjectLabel: () => ({ |
| 50 | + fieldLabel: (_o: any, _n: any, l: any) => l, |
| 51 | + fieldOptionLabel: (_o: any, _f: any, _v: any, l: any) => l, |
| 52 | + actionParamText: (_o: any, _a: any, _p: any, _attr: any, fallback: any) => fallback, |
| 53 | + actionParamOptionLabel: (_o: any, _a: any, _p: any, _v: any, fallback: any) => fallback, |
| 54 | + actionDescription: (_o: any, _a: any, fallback: any) => fallback, |
| 55 | + actionSubmitText: (objectName: string | undefined, actionName: string | undefined, fallback?: string) => { |
| 56 | + asked.push({ objectName, actionName, fallback }); |
| 57 | + return bundle[`${objectName ?? 'global'}|${actionName}`] ?? (fallback || undefined); |
| 58 | + }, |
| 59 | + }), |
| 60 | + useObjectTranslation: () => ({ |
| 61 | + t: (key: string, options?: any) => String(options?.defaultValue ?? key), |
| 62 | + language: LANGUAGE, |
| 63 | + }), |
| 64 | +})); |
| 65 | + |
| 66 | +vi.mock('../useActionModal', () => ({ |
| 67 | + useActionModal: () => ({ |
| 68 | + modalHandler: vi.fn(async () => ({ success: true })), |
| 69 | + modalElement: null, |
| 70 | + closeModal: () => {}, |
| 71 | + resolveModalTarget: vi.fn(async () => null), |
| 72 | + }), |
| 73 | +})); |
| 74 | + |
| 75 | +let paramDialogState: any = null; |
| 76 | +vi.mock('../../views/ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 77 | +vi.mock('../../views/ActionParamDialog', () => ({ |
| 78 | + ActionParamDialog: ({ state }: any) => { |
| 79 | + if (state?.open) paramDialogState = state; |
| 80 | + return null; |
| 81 | + }, |
| 82 | +})); |
| 83 | +vi.mock('../../views/ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 84 | +vi.mock('../../views/FlowRunner', () => ({ FlowRunner: () => null })); |
| 85 | +vi.mock('sonner', () => { |
| 86 | + const fn: any = vi.fn(); |
| 87 | + fn.error = vi.fn(); |
| 88 | + fn.success = vi.fn(); |
| 89 | + return { toast: fn }; |
| 90 | +}); |
| 91 | + |
| 92 | +import { useConsoleActionRuntime } from '../useConsoleActionRuntime'; |
| 93 | + |
| 94 | +const PARAMS = [{ name: 'subject', label: 'Subject', type: 'text' }] as any[]; |
| 95 | + |
| 96 | +function Harness({ onReady }: { onReady: (fn: any) => void }) { |
| 97 | + const runtime = useConsoleActionRuntime({ dataSource: {}, objects: [] }); |
| 98 | + const ready = React.useRef(false); |
| 99 | + if (!ready.current) { |
| 100 | + ready.current = true; |
| 101 | + onReady(runtime.actionProviderProps.onParamCollection); |
| 102 | + } |
| 103 | + return <>{runtime.dialogs}</>; |
| 104 | +} |
| 105 | + |
| 106 | +async function collectParams(dispatch: Record<string, unknown>) { |
| 107 | + paramDialogState = null; |
| 108 | + let collect: any; |
| 109 | + render(<Harness onReady={(fn) => { collect = fn; }} />); |
| 110 | + await act(async () => { |
| 111 | + void collect(PARAMS, dispatch as any); |
| 112 | + await Promise.resolve(); |
| 113 | + }); |
| 114 | + expect(paramDialogState).toBeTruthy(); |
| 115 | +} |
| 116 | + |
| 117 | +beforeEach(() => { |
| 118 | + bundle = {}; |
| 119 | + asked.length = 0; |
| 120 | + paramDialogState = null; |
| 121 | +}); |
| 122 | + |
| 123 | +describe('useConsoleActionRuntime — the param dialog\'s submit verb (objectstack#28158)', () => { |
| 124 | + it('hands the dialog the authored string', async () => { |
| 125 | + await collectParams({ name: 'send_email', label: 'Send Email', objectName: 'crm_contact', submitText: 'Send' }); |
| 126 | + expect(paramDialogState.submitText).toBe('Send'); |
| 127 | + expect(asked).toEqual([{ objectName: 'crm_contact', actionName: 'send_email', fallback: 'Send' }]); |
| 128 | + }); |
| 129 | + |
| 130 | + it('prefers the bundle entry over the authored string', async () => { |
| 131 | + bundle['crm_contact|send_email'] = '发送'; |
| 132 | + await collectParams({ name: 'send_email', label: 'Send Email', objectName: 'crm_contact', submitText: 'Send' }); |
| 133 | + expect(paramDialogState.submitText).toBe('发送'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('collapses an inline locale map to the active language BEFORE offering it as the fallback', async () => { |
| 137 | + await collectParams({ |
| 138 | + name: 'send_email', |
| 139 | + label: 'Send Email', |
| 140 | + objectName: 'crm_contact', |
| 141 | + submitText: { en: 'Send', 'zh-CN': '发出' }, |
| 142 | + }); |
| 143 | + expect(paramDialogState.submitText).toBe('发出'); |
| 144 | + expect(asked[0].fallback).toBe('发出'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('asks the global node for an object-less action', async () => { |
| 148 | + bundle['global|log_call'] = '记录通话'; |
| 149 | + await collectParams({ name: 'log_call', label: 'Log Call', submitText: 'Log Call' }); |
| 150 | + expect(asked[0]).toMatchObject({ objectName: undefined, actionName: 'log_call' }); |
| 151 | + expect(paramDialogState.submitText).toBe('记录通话'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('hands the dialog nothing when the action names no verb and the bundle has none — the dialog says Confirm', async () => { |
| 155 | + await collectParams({ name: 'send_email', label: 'Send Email', objectName: 'crm_contact' }); |
| 156 | + expect(paramDialogState.submitText).toBeUndefined(); |
| 157 | + expect(asked[0].fallback).toBeUndefined(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments