|
| 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 | + * objectui#13397 — a record-scoped `type: 'flow'` action launched from a list |
| 11 | + * toolbar with a row count other than 1 is refused BEFORE its param dialog, |
| 12 | + * with a translated toast and no request. objectui#13384 gave the `script` |
| 13 | + * handler that early refusal (`refuseBeforeInput`); the console `flowHandler` |
| 14 | + * ran the same resolver only at dispatch time, after the whole dialog, and |
| 15 | + * showed its English `error`. |
| 16 | + * |
| 17 | + * The session here reads zh-CN: the translator answers from the shipped zh |
| 18 | + * pack, so a refusal that reaches the toast untranslated reads English and |
| 19 | + * fails the text pin. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 23 | +import { render, screen, fireEvent, waitFor, renderHook, act } from '@testing-library/react'; |
| 24 | +import React from 'react'; |
| 25 | + |
| 26 | +const navigateSpy = vi.fn(); |
| 27 | +vi.mock('react-router-dom', () => ({ useNavigate: () => navigateSpy })); |
| 28 | + |
| 29 | +const authFetchSpy = vi.fn(); |
| 30 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 31 | + ...(await importOriginal<Record<string, unknown>>()), |
| 32 | + useAuth: () => ({ user: { id: 'u1', name: 'User', image: null }, activeOrganization: null }), |
| 33 | + createAuthenticatedFetch: () => authFetchSpy, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock('@object-ui/i18n', async (importOriginal) => { |
| 37 | + const { builtInLocales } = await import('@object-ui/i18n/locales'); |
| 38 | + // The zh-CN pack, read by dotted key; a key it lacks answers its default. |
| 39 | + const zhT = (key: string, options?: { defaultValue?: string }) => { |
| 40 | + const hit = key.split('.').reduce<unknown>( |
| 41 | + (node, part) => (node && typeof node === 'object' ? (node as Record<string, unknown>)[part] : undefined), |
| 42 | + builtInLocales.zh, |
| 43 | + ); |
| 44 | + return typeof hit === 'string' ? hit : String(options?.defaultValue ?? key); |
| 45 | + }; |
| 46 | + return { |
| 47 | + ...(await importOriginal<Record<string, unknown>>()), |
| 48 | + useObjectLabel: () => ({ |
| 49 | + fieldLabel: (_o: unknown, _n: unknown, l: string) => l, |
| 50 | + fieldOptionLabel: (_o: unknown, _f: unknown, _v: unknown, l: string) => l, |
| 51 | + actionParamText: (_o: unknown, _a: unknown, _p: unknown, _attr: unknown, fallback: string) => fallback, |
| 52 | + actionParamOptionLabel: (_o: unknown, _a: unknown, _p: unknown, _v: unknown, fallback: string) => fallback, |
| 53 | + actionDescription: (_o: unknown, _a: unknown, fallback: string) => fallback, |
| 54 | + }), |
| 55 | + useObjectTranslation: () => ({ t: zhT, language: 'zh' }), |
| 56 | + }; |
| 57 | +}); |
| 58 | + |
| 59 | +vi.mock('../useActionModal', () => ({ |
| 60 | + useActionModal: () => ({ |
| 61 | + modalHandler: vi.fn(async () => ({ success: true })), |
| 62 | + modalElement: null, |
| 63 | + closeModal: () => {}, |
| 64 | + resolveModalTarget: vi.fn(async () => null), |
| 65 | + }), |
| 66 | +})); |
| 67 | + |
| 68 | +// The param dialog renders a marker while it is open — the "did a dialog open" |
| 69 | +// probe. The other dialogs stay inert. |
| 70 | +vi.mock('../../views/ActionParamDialog', () => ({ |
| 71 | + ActionParamDialog: ({ state }: { state: { open?: boolean } }) => |
| 72 | + state?.open ? <div data-testid="param-dialog" /> : null, |
| 73 | +})); |
| 74 | +vi.mock('../../views/ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 75 | +vi.mock('../../views/ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 76 | +vi.mock('../../views/FlowRunner', () => ({ FlowRunner: () => null })); |
| 77 | + |
| 78 | +vi.mock('sonner', () => ({ toast: Object.assign(vi.fn(), { error: vi.fn(), success: vi.fn() }) })); |
| 79 | + |
| 80 | +import { toast } from 'sonner'; |
| 81 | +import { useConsoleActionRuntime, ConsoleActionRuntimeProvider } from '../useConsoleActionRuntime'; |
| 82 | +import { useAction } from '@object-ui/react'; |
| 83 | +import { resolveServerActionRecordId, type ActionContext, type ActionDef } from '@object-ui/core'; |
| 84 | + |
| 85 | +const ZH_SELECT_ROW_FIRST = '此操作针对单条记录,请先选择一行。'; |
| 86 | +const ZH_SELECT_EXACTLY_ONE_ROW = '此操作针对单条记录,请只选择一行。'; |
| 87 | + |
| 88 | +/** A record-scoped flow with a param dialog, mounted on rows AND the toolbar. */ |
| 89 | +const REASSIGN = { |
| 90 | + type: 'flow', |
| 91 | + name: 'reassign', |
| 92 | + target: 'reassign_wizard', |
| 93 | + locations: ['list_item', 'list_toolbar'], |
| 94 | + params: [{ name: 'owner', label: 'Owner', type: 'text', required: true }], |
| 95 | +}; |
| 96 | +const launch = (extra: Record<string, unknown> = {}): ActionDef => ({ ...JSON.parse(JSON.stringify(REASSIGN)), ...extra }); |
| 97 | +const rowsContext = (rows: Record<string, unknown>[]): ActionContext => ({ selectedRecords: rows }); |
| 98 | +const toastError = vi.mocked(toast.error); |
| 99 | + |
| 100 | +beforeEach(() => { |
| 101 | + authFetchSpy.mockReset(); |
| 102 | + toastError.mockClear(); |
| 103 | + vi.mocked(toast.success).mockClear(); |
| 104 | +}); |
| 105 | + |
| 106 | +describe('flowHandler.refuseBeforeInput — the resolver its dispatch runs (objectui#13397)', () => { |
| 107 | + const runtime = () => renderHook(() => useConsoleActionRuntime({ dataSource: {}, objects: [], objectName: 'inv' })).result.current; |
| 108 | + |
| 109 | + it.each([ |
| 110 | + ['0 rows', [] as Record<string, unknown>[], 'actions.selectRowFirst'], |
| 111 | + ['2 rows', [{ id: 'a' }, { id: 'b' }], 'actions.selectExactlyOneRow'], |
| 112 | + ])('%s: answers the resolver\'s refusal with its pack key', (_label, rows, errorKey) => { |
| 113 | + const context = rowsContext(rows); |
| 114 | + const rule = resolveServerActionRecordId(launch(), context); |
| 115 | + |
| 116 | + expect(runtime().flowHandler.refuseBeforeInput?.(launch(), context)).toEqual({ error: rule.error, errorKey }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('1 row: no refusal', () => { |
| 120 | + expect(runtime().flowHandler.refuseBeforeInput?.(launch(), rowsContext([{ id: 'a' }]))) |
| 121 | + .toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('control: an object-level toolbar flow with 0 rows is not refused', () => { |
| 125 | + expect(runtime().flowHandler.refuseBeforeInput?.( |
| 126 | + launch({ locations: ['list_toolbar'] }), |
| 127 | + rowsContext([]), |
| 128 | + )).toBeUndefined(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('control: a dialog that itself collects `recordId` is not judged before it runs', () => { |
| 132 | + const collectsRecordId = launch({ params: [{ name: 'recordId', label: 'Record', type: 'text' }] }); |
| 133 | + |
| 134 | + expect(runtime().flowHandler.refuseBeforeInput?.(collectsRecordId, rowsContext([]))) |
| 135 | + .toBeUndefined(); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('a toolbar flow launch through the console runtime (objectui#13397)', () => { |
| 140 | + function Probe({ rows, action }: { rows: Record<string, unknown>[]; action: ActionDef }) { |
| 141 | + const { execute, updateContext } = useAction(); |
| 142 | + return ( |
| 143 | + <button |
| 144 | + onClick={() => { |
| 145 | + updateContext({ selectedRecords: rows }); |
| 146 | + void execute(action); |
| 147 | + }} |
| 148 | + > |
| 149 | + run-flow |
| 150 | + </button> |
| 151 | + ); |
| 152 | + } |
| 153 | + const mount = (rows: Record<string, unknown>[], action: ActionDef = launch()) => { |
| 154 | + render( |
| 155 | + <ConsoleActionRuntimeProvider dataSource={{}} objects={[]} objectName="inv"> |
| 156 | + <Probe rows={rows} action={action} /> |
| 157 | + </ConsoleActionRuntimeProvider>, |
| 158 | + ); |
| 159 | + fireEvent.click(screen.getByText('run-flow')); |
| 160 | + }; |
| 161 | + |
| 162 | + it.each([ |
| 163 | + ['0 rows', [] as Record<string, unknown>[], ZH_SELECT_ROW_FIRST], |
| 164 | + ['2 rows', [{ id: 'a' }, { id: 'b' }], ZH_SELECT_EXACTLY_ONE_ROW], |
| 165 | + ])('%s: refused before the param dialog, with a zh-CN toast and no request', async (_label, rows, zh) => { |
| 166 | + mount(rows); |
| 167 | + |
| 168 | + await waitFor(() => expect(toastError).toHaveBeenCalledWith(zh)); |
| 169 | + expect(screen.queryByTestId('param-dialog')).toBeNull(); |
| 170 | + expect(authFetchSpy).not.toHaveBeenCalled(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('1 row: the param dialog opens, as today, and nothing is refused', async () => { |
| 174 | + mount([{ id: 'rec_1' }]); |
| 175 | + |
| 176 | + await screen.findByTestId('param-dialog'); |
| 177 | + expect(toastError).not.toHaveBeenCalled(); |
| 178 | + expect(authFetchSpy).not.toHaveBeenCalled(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('control: an object-level toolbar flow with 0 rows still opens its dialog', async () => { |
| 182 | + mount([], launch({ locations: ['list_toolbar'] })); |
| 183 | + |
| 184 | + await screen.findByTestId('param-dialog'); |
| 185 | + expect(toastError).not.toHaveBeenCalled(); |
| 186 | + }); |
| 187 | +}); |
| 188 | + |
| 189 | +describe('flowHandler called directly: the dispatch-time refusal is translated (objectui#13397)', () => { |
| 190 | + it.each([ |
| 191 | + ['0 rows', [] as Record<string, unknown>[], ZH_SELECT_ROW_FIRST], |
| 192 | + ['2 rows', [{ id: 'a' }, { id: 'b' }], ZH_SELECT_EXACTLY_ONE_ROW], |
| 193 | + ])('%s: refuses in zh-CN through the pack key, with no request', async (_label, rows, zh) => { |
| 194 | + const { result } = renderHook(() => useConsoleActionRuntime({ dataSource: {}, objects: [], objectName: 'inv' })); |
| 195 | + |
| 196 | + let res: unknown; |
| 197 | + await act(async () => { |
| 198 | + res = await result.current.flowHandler(launch({ params: {} }), rowsContext(rows)); |
| 199 | + }); |
| 200 | + |
| 201 | + expect(res).toEqual({ success: false, error: zh }); |
| 202 | + expect(authFetchSpy).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | +}); |
0 commit comments