|
| 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#26665 — a `destructive` action block confirms through the |
| 11 | + * destructive button. |
| 12 | + * |
| 13 | + * The runner paints its confirm button destructive when the action it runs |
| 14 | + * declares `variant: 'danger'` (`confirmOptions`, objectstack#26580). The four |
| 15 | + * action blocks speak the spec's BLOCK dialect, where that value is spelled |
| 16 | + * `destructive`, and before this card none of them forwarded `variant` at all, |
| 17 | + * so a destructive block asked its question through a neutral button. Each |
| 18 | + * renderer now forwards its variant through `toRunnerVariant`. |
| 19 | + * |
| 20 | + * Every row reads the confirm handler's own call, the artefact the dialog is |
| 21 | + * drawn from, and first asserts it was called once (the positive control). The |
| 22 | + * `outline` row is the control for the mapping: a value the action dialect has |
| 23 | + * no word for forwards nothing, so that question stays neutral. |
| 24 | + */ |
| 25 | + |
| 26 | +import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'; |
| 27 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 28 | +import React from 'react'; |
| 29 | +import { ComponentRegistry } from '@object-ui/core'; |
| 30 | +import type { ActionContext, ActionDef, ActionResult, ConfirmationHandler } from '@object-ui/core'; |
| 31 | +import { ActionProvider } from '@object-ui/react'; |
| 32 | +// Module-scope side-effect imports — these renderers register themselves with |
| 33 | +// the ComponentRegistry, and the light `dom` project does not load the |
| 34 | +// `@object-ui/components` graph. Module scope, not a `beforeAll`, per |
| 35 | +// AGENTS.md §测试纪律. |
| 36 | +import '../action-button'; |
| 37 | +import '../action-icon'; |
| 38 | +import '../action-group'; |
| 39 | +import '../action-menu'; |
| 40 | +import { toRunnerVariant } from '../runner-variant'; |
| 41 | + |
| 42 | +const ACTION = { |
| 43 | + name: 'purge_history', |
| 44 | + label: 'Purge history', |
| 45 | + confirmText: 'This cannot be undone.', |
| 46 | +}; |
| 47 | + |
| 48 | +let script: Mock<(action: ActionDef, ctx: ActionContext) => Promise<ActionResult>>; |
| 49 | +let confirm: Mock<ConfirmationHandler>; |
| 50 | + |
| 51 | +beforeEach(() => { |
| 52 | + script = vi.fn(async () => ({ success: true })); |
| 53 | + // A cancel: the question is what is measured, not the run. |
| 54 | + confirm = vi.fn(async () => false); |
| 55 | +}); |
| 56 | + |
| 57 | +function surface(type: string, schema: Record<string, unknown>) { |
| 58 | + const C = ComponentRegistry.get(type); |
| 59 | + if (!C) throw new Error(`${type} is not registered`); |
| 60 | + return <C schema={schema as never} />; |
| 61 | +} |
| 62 | + |
| 63 | +/** Each surface, carrying one action declared with `variant`, and how to run it. */ |
| 64 | +const SURFACES: Array<[string, (variant: string) => Record<string, unknown>, boolean]> = [ |
| 65 | + ['action:button', (variant) => ({ ...ACTION, type: 'action:button', actionType: 'script', variant }), true], |
| 66 | + ['action:icon', (variant) => ({ ...ACTION, type: 'action:icon', actionType: 'script', icon: 'trash', variant }), true], |
| 67 | + ['action:group', (variant) => ({ type: 'action:group', actions: [{ ...ACTION, type: 'script', variant }] }), true], |
| 68 | + // `autoTrigger` runs `handleExecute`, the function a click on the menu item |
| 69 | + // calls, without opening the Radix dropdown (see `action-onSuccess-forward.test.tsx`). |
| 70 | + ['action:menu', (variant) => ({ type: 'action:menu', actions: [{ ...ACTION, type: 'script', variant, autoTrigger: true }] }), false], |
| 71 | +]; |
| 72 | + |
| 73 | +async function confirmOptionsFor(type: string, schema: Record<string, unknown>, click: boolean) { |
| 74 | + render( |
| 75 | + <ActionProvider handlers={{ script }} onConfirm={confirm}> |
| 76 | + {surface(type, schema)} |
| 77 | + </ActionProvider>, |
| 78 | + ); |
| 79 | + if (click) fireEvent.click(screen.getByRole('button', { name: 'Purge history' })); |
| 80 | + await waitFor(() => expect(confirm).toHaveBeenCalledTimes(1)); |
| 81 | + expect(script).not.toHaveBeenCalled(); |
| 82 | + return confirm.mock.calls[0][1]; |
| 83 | +} |
| 84 | + |
| 85 | +describe.each(SURFACES)('%s forwards its variant in the runner dialect (objectstack#26665)', (type, schemaFor, click) => { |
| 86 | + it('`destructive` confirms through the destructive button', async () => { |
| 87 | + expect(await confirmOptionsFor(type, schemaFor('destructive'), click)).toEqual({ |
| 88 | + title: 'Purge history', |
| 89 | + confirmText: 'Purge history', |
| 90 | + destructive: true, |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('CONTROL: `outline` has no action-dialect word, so the question stays neutral', async () => { |
| 95 | + expect(await confirmOptionsFor(type, schemaFor('outline'), click)).toEqual({ |
| 96 | + title: 'Purge history', |
| 97 | + confirmText: 'Purge history', |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('toRunnerVariant (objectstack#26665)', () => { |
| 103 | + it('maps the block dialect onto the action dialect', () => { |
| 104 | + expect(toRunnerVariant('destructive')).toBe('danger'); |
| 105 | + expect(toRunnerVariant('primary')).toBe('primary'); |
| 106 | + expect(toRunnerVariant('secondary')).toBe('secondary'); |
| 107 | + expect(toRunnerVariant('ghost')).toBe('ghost'); |
| 108 | + expect(toRunnerVariant('link')).toBe('link'); |
| 109 | + expect(toRunnerVariant('default')).toBeUndefined(); |
| 110 | + expect(toRunnerVariant('outline')).toBeUndefined(); |
| 111 | + expect(toRunnerVariant(undefined)).toBeUndefined(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments