|
| 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#11344 — `ActionSchema.outcomeMessages` must SURVIVE the hop on all |
| 11 | + * four declared action surfaces, and on the `action:bar` path a registered |
| 12 | + * action actually takes to them. |
| 13 | + * |
| 14 | + * Each action renderer hands the runner an explicit key whitelist, so a key the |
| 15 | + * runner reads stays dropped until every whitelist carries it — the |
| 16 | + * objectstack#6837 shape. `check:action-forward-parity` asks that statically, |
| 17 | + * once the runner reads the key off the forwarded def; these rows measure the |
| 18 | + * same thing at run time, at the artefact the user sees: the success toast. |
| 19 | + * |
| 20 | + * ## Why the toast, and why the answer carries a `message` |
| 21 | + * |
| 22 | + * Every row's handler answers `{ outcome: 'archived', name: 'prod', message }`. |
| 23 | + * With the map dropped, the toast would read `successMessage` (the second |
| 24 | + * rung); with the retired server-message rung back, it would read `message`. |
| 25 | + * Only the forwarded map, picked by `outcome` and interpolated, produces |
| 26 | + * `Environment prod archived` — so neither regression can pass a row. |
| 27 | + * |
| 28 | + * Each row first asserts the handler ran exactly once (the positive control), so |
| 29 | + * a wrong toast is never "the harness executed nothing". |
| 30 | + * |
| 31 | + * ## The `action:bar` row is the producer path |
| 32 | + * |
| 33 | + * An object's registered actions reach a record header as `action:bar` members, |
| 34 | + * and the bar spreads each member WHOLE onto an `action:button` node |
| 35 | + * (`renderMember` in `action-bar.tsx`). So the map on a registered action |
| 36 | + * arrives at `action:button` without anyone authoring it there; dropped by that |
| 37 | + * whitelist, it never reached the runner. `element:button` is absent for the |
| 38 | + * reason `action-onSuccess-forward.test.tsx` gives: the spec's |
| 39 | + * `InlineActionSchema` pick list does not carry the key. |
| 40 | + */ |
| 41 | + |
| 42 | +import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'; |
| 43 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 44 | +import React from 'react'; |
| 45 | +import { ComponentRegistry } from '@object-ui/core'; |
| 46 | +import type { ActionContext, ActionDef, ActionResult, ToastHandler } from '@object-ui/core'; |
| 47 | +import { ActionProvider } from '@object-ui/react'; |
| 48 | +// Module-scope side-effect imports — these renderers register themselves with |
| 49 | +// the ComponentRegistry, and the light `dom` project does not load the |
| 50 | +// `@object-ui/components` graph. Module scope, not a `beforeAll`, per |
| 51 | +// AGENTS.md §测试纪律. |
| 52 | +import '../action-button'; |
| 53 | +import '../action-icon'; |
| 54 | +import '../action-group'; |
| 55 | +import '../action-menu'; |
| 56 | +import '../action-bar'; |
| 57 | + |
| 58 | +/** The handler's own return value — what `outcome` and `${result.*}` read. */ |
| 59 | +const PAYLOAD = { outcome: 'archived', name: 'prod', message: 'Environment prod archived (server English).' }; |
| 60 | + |
| 61 | +const OUTCOME_MESSAGES = { |
| 62 | + archived: 'Environment ${result.name} archived', |
| 63 | + already_archived: 'Environment ${result.name} was already archived', |
| 64 | +}; |
| 65 | + |
| 66 | +/** The registered action, as an object's `actions` array declares it. */ |
| 67 | +const declaration = (extra: Record<string, unknown> = {}) => ({ |
| 68 | + name: 'delete_environment', |
| 69 | + label: 'Delete environment', |
| 70 | + type: 'script', |
| 71 | + locations: ['record_header'], |
| 72 | + outcomeMessages: OUTCOME_MESSAGES, |
| 73 | + successMessage: 'Environment ${result.name} updated', |
| 74 | + ...extra, |
| 75 | +}); |
| 76 | + |
| 77 | +/** As a NODE handed straight to a leaf, the execution type is `actionType` (objectui#7415). */ |
| 78 | +const asNode = (component: string, decl: Record<string, unknown>) => { |
| 79 | + const { type, ...rest } = decl; |
| 80 | + return { ...rest, type: component, actionType: type }; |
| 81 | +}; |
| 82 | + |
| 83 | +let script: Mock<(action: ActionDef, ctx: ActionContext) => Promise<ActionResult>>; |
| 84 | +let toast: Mock<ToastHandler>; |
| 85 | + |
| 86 | +beforeEach(() => { |
| 87 | + script = vi.fn(async () => ({ success: true, data: PAYLOAD })); |
| 88 | + toast = vi.fn(); |
| 89 | +}); |
| 90 | + |
| 91 | +function surface(type: string, schema: Record<string, unknown>) { |
| 92 | + const C = ComponentRegistry.get(type); |
| 93 | + if (!C) throw new Error(`${type} is not registered`); |
| 94 | + return <C schema={schema as never} />; |
| 95 | +} |
| 96 | + |
| 97 | +const renderSurface = (node: React.ReactNode) => |
| 98 | + render( |
| 99 | + <ActionProvider handlers={{ script }} onToast={toast}> |
| 100 | + {node} |
| 101 | + </ActionProvider>, |
| 102 | + ); |
| 103 | + |
| 104 | +/** The positive control, the forward, and the toast the user reads. */ |
| 105 | +async function expectOutcomeToast() { |
| 106 | + await waitFor(() => expect(script).toHaveBeenCalledTimes(1)); |
| 107 | + expect(script.mock.calls[0][0].outcomeMessages).toEqual(OUTCOME_MESSAGES); |
| 108 | + await waitFor(() => expect(toast).toHaveBeenCalledTimes(1)); |
| 109 | + expect(toast.mock.calls[0][0]).toBe('Environment prod archived'); |
| 110 | + expect(toast.mock.calls[0][1]).toEqual(expect.objectContaining({ type: 'success' })); |
| 111 | +} |
| 112 | + |
| 113 | +describe('ActionSchema.outcomeMessages reaches the success toast from every declared surface (objectui#11344)', () => { |
| 114 | + it('action:bar → action:button — a registered action spread onto the button keeps its outcome copy', async () => { |
| 115 | + const Bar = ComponentRegistry.get('action:bar'); |
| 116 | + if (!Bar) throw new Error('action:bar is not registered'); |
| 117 | + // Both ceilings high, so the member stays inline on any viewport (see |
| 118 | + // `action-bar-member-type-resolution.test.tsx`). |
| 119 | + renderSurface( |
| 120 | + <Bar |
| 121 | + schema={{ type: 'action:bar', maxVisible: 10, mobileMaxVisible: 10, actions: [declaration()] } as never} |
| 122 | + />, |
| 123 | + ); |
| 124 | + |
| 125 | + fireEvent.click(screen.getByRole('button', { name: 'Delete environment' })); |
| 126 | + |
| 127 | + await expectOutcomeToast(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('action:button — a node carrying the map', async () => { |
| 131 | + renderSurface(surface('action:button', asNode('action:button', declaration()))); |
| 132 | + |
| 133 | + fireEvent.click(screen.getByRole('button', { name: 'Delete environment' })); |
| 134 | + |
| 135 | + await expectOutcomeToast(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('action:icon — the icon-only surface', async () => { |
| 139 | + renderSurface(surface('action:icon', asNode('action:icon', declaration()))); |
| 140 | + |
| 141 | + fireEvent.click(screen.getByRole('button', { name: 'Delete environment' })); |
| 142 | + |
| 143 | + await expectOutcomeToast(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('action:group — an inline group member', async () => { |
| 147 | + renderSurface(surface('action:group', { type: 'action:group', actions: [declaration()] })); |
| 148 | + |
| 149 | + fireEvent.click(screen.getByRole('button', { name: 'Delete environment' })); |
| 150 | + |
| 151 | + await expectOutcomeToast(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('action:menu — an overflow item', async () => { |
| 155 | + // `autoTrigger` runs `handleExecute`, the function a click on the menu item |
| 156 | + // calls, without opening the Radix dropdown (see |
| 157 | + // `action-onSuccess-forward.test.tsx`). |
| 158 | + renderSurface(surface('action:menu', { type: 'action:menu', actions: [declaration({ autoTrigger: true })] })); |
| 159 | + |
| 160 | + await expectOutcomeToast(); |
| 161 | + }); |
| 162 | +}); |
0 commit comments