Skip to content

Commit 9c6bb85

Browse files
committed
test(console): pin the param dialog's submit verb in both handlers, the dialog and the resolver
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibzPbVCsG2npDkDqikd
1 parent 5536a4a commit 9c6bb85

6 files changed

Lines changed: 598 additions & 0 deletions

‎ui/packages/app-shell/src/hooks/__tests__/useConsoleActionRuntime.overrideNotice.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ vi.mock('@object-ui/i18n', async (importOriginal) => ({
6060
actionParamText: (_o: any, _a: any, _p: any, _attr: any, fallback: any) => fallback,
6161
actionParamOptionLabel: (_o: any, _a: any, _p: any, _v: any, fallback: any) => fallback,
6262
actionDescription: (_o: any, _a: any, fallback: any) => bundleDescription ?? fallback,
63+
actionSubmitText: (_o: any, _a: any, fallback: any) => fallback,
6364
}),
6465
useObjectTranslation: () => ({
6566
t: (key: string, options?: any) =>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
* ActionParamDialog draws the action's `submitText` on its submit button
11+
* (`ActionSchema.submitText`, objectstack#28158): the verb the button performs
12+
* ("Send", "Log Call") in place of the generic Confirm. The caller hands it the
13+
* already-resolved text; the dialog falls back to `actionDialog.confirm` when
14+
* there is none, and says `actionDialog.uploading` while a file param uploads,
15+
* whatever the verb. No i18n provider is mounted, so `t()` answers the key —
16+
* which is what lets these assertions tell the fallback from the verb.
17+
*
18+
* The upload widget is stubbed the way `ActionParamDialog.uploading.test.tsx`
19+
* stubs it, so the mid-upload window is deterministic.
20+
*/
21+
import { describe, it, expect, vi } from 'vitest';
22+
import { render, screen, fireEvent } from '@testing-library/react';
23+
import type { ActionParamDef } from '@object-ui/core';
24+
25+
vi.mock('@object-ui/fields', async (importActual) => {
26+
const actual = await importActual<typeof import('@object-ui/fields')>();
27+
return {
28+
...actual,
29+
getLazyFieldWidget: (type: string) => {
30+
if (type === 'file') {
31+
return function FakeUploadWidget({ onUploadingChange }: { onUploadingChange?: (u: boolean) => void }) {
32+
return (
33+
<button type="button" data-testid="start-upload" onClick={() => onUploadingChange?.(true)}>
34+
start
35+
</button>
36+
);
37+
};
38+
}
39+
return actual.getLazyFieldWidget(type);
40+
},
41+
};
42+
});
43+
44+
// Import AFTER the mock is registered.
45+
const { ActionParamDialog } = await import('./ActionParamDialog');
46+
47+
const TEXT_PARAMS: ActionParamDef[] = [{ name: 'subject', label: 'Subject', type: 'text' }];
48+
49+
function openDialog(params: ActionParamDef[], submitText?: string) {
50+
const resolve = vi.fn();
51+
render(
52+
<ActionParamDialog
53+
state={{ open: true, params, title: 'Send Email', submitText, resolve }}
54+
onOpenChange={() => {}}
55+
/>,
56+
);
57+
return resolve;
58+
}
59+
60+
describe('ActionParamDialog — the submit button names the action\'s verb (objectstack#28158)', () => {
61+
it('draws the action\'s `submitText` on the submit button', async () => {
62+
openDialog(TEXT_PARAMS, 'Send');
63+
const button = await screen.findByRole('button', { name: 'Send' });
64+
expect(button).toBeTruthy();
65+
expect(screen.queryByText('actionDialog.confirm')).toBeNull();
66+
});
67+
68+
it('falls back to the generic Confirm when the action names no verb', async () => {
69+
openDialog(TEXT_PARAMS);
70+
expect(await screen.findByRole('button', { name: 'actionDialog.confirm' })).toBeTruthy();
71+
});
72+
73+
it('falls back to Confirm for an empty string, never an empty button', async () => {
74+
openDialog(TEXT_PARAMS, '');
75+
expect(await screen.findByRole('button', { name: 'actionDialog.confirm' })).toBeTruthy();
76+
});
77+
78+
it('the verb button submits like Confirm does', async () => {
79+
const resolve = openDialog(TEXT_PARAMS, 'Send');
80+
fireEvent.change(await screen.findByLabelText(/Subject/), { target: { value: 'Hello' } });
81+
fireEvent.click(screen.getByRole('button', { name: 'Send' }));
82+
expect(resolve).toHaveBeenCalledWith({ subject: 'Hello' });
83+
});
84+
85+
it('says Uploading while a file param uploads, whatever the verb', async () => {
86+
openDialog([{ name: 'doc', label: 'Doc', type: 'file' }], 'Send');
87+
fireEvent.click(await screen.findByTestId('start-upload'));
88+
89+
const uploading = screen.getByText('actionDialog.uploading').closest('button')!;
90+
expect(uploading).toBeDisabled();
91+
expect(screen.queryByRole('button', { name: 'Send' })).toBeNull();
92+
});
93+
});

‎ui/packages/app-shell/src/views/RecordDetailView.paramRuntimeParity-6431.test.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ const ACTION = {
212212
name: 'create_environment',
213213
label: 'Create environment',
214214
description: 'Provisions a new environment for this org.',
215+
submitText: 'Create',
215216
} as any;
216217

217218
/** The fields `ActionParamDialog.tsx` reads off `state`. Written out rather
@@ -231,6 +232,8 @@ const DIALOG_READS = [
231232
// the dialog, so a refusal keeps the typed values. Called without one here,
232233
// so it is written `undefined`; the key is on both runtimes' state.
233234
'run',
235+
// The submit button's verb (`submitText`), resolved by each runtime.
236+
'submitText',
234237
'title',
235238
];
236239

@@ -566,6 +569,8 @@ describe('app-shell — both param runtimes reset ActionParamDialog the same way
566569
expect(viewClosed.open).toBe(hookClosed.open);
567570
expect(viewClosed.title).toBe(hookClosed.title);
568571
expect(viewClosed.description).toBe(hookClosed.description);
572+
expect(viewClosed.submitText).toBe('Create');
573+
expect(viewClosed.submitText).toBe(hookClosed.submitText);
569574
expect(viewClosed.params.map((p: any) => p.name))
570575
.toEqual(hookClosed.params.map((p: any) => p.name));
571576
expect(typeof viewClosed.resolve).toBe(typeof hookClosed.resolve);

0 commit comments

Comments
 (0)