|
| 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#26781 — a declared `danger` action on `action:bar` draws with |
| 11 | + * the destructive button style and confirms through the destructive button. |
| 12 | + * |
| 13 | + * The producer is `examples/app-todo`'s `DeleteCompletedAction` |
| 14 | + * (`delete_completed`: `locations: ['list_toolbar']`, `variant: 'danger'`, |
| 15 | + * `confirmText`). The list page hands an object's actions to `action:bar` as |
| 16 | + * they are served, and `action:bar` spreads each one onto an `action:button` |
| 17 | + * member with `variant: action.variant || schema.variant`. Before |
| 18 | + * objectstack#26779 the Button primitive had no `danger` variant, so that |
| 19 | + * member drew with no variant style and its confirm stayed neutral; the code |
| 20 | + * path is fixed by `toButtonVariant` / `toRunnerVariant` (`../runner-variant`). |
| 21 | + * `action-variant-confirm-26665.test.tsx` drives `action:button` / icon / |
| 22 | + * group / menu directly; this file drives the declared action through the bar. |
| 23 | + * |
| 24 | + * The two fixtures copy the app-todo literals key for key, `ai` block |
| 25 | + * included, because that is the shape the served metadata carries. The bar |
| 26 | + * copies `objectViewHeaderToolbar.ts`'s list toolbar (`location`, `size`, |
| 27 | + * `variant: 'outline'`), so the bar's own variant is in play. The control, |
| 28 | + * app-todo's `export_csv` from the same toolbar, declares no variant and draws |
| 29 | + * the bar's `outline`, which tells "the member drew its own variant" apart from |
| 30 | + * "every member draws destructive". |
| 31 | + * |
| 32 | + * The confirm is read from the confirm handler's own call, the artefact the |
| 33 | + * host's dialog (`ActionConfirmDialog` in `@object-ui/app-shell`) is drawn |
| 34 | + * from: `destructive: true` is what paints its confirm button destructive. |
| 35 | + */ |
| 36 | + |
| 37 | +import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'; |
| 38 | +import { render, screen, waitFor, fireEvent, within } from '@testing-library/react'; |
| 39 | +import React from 'react'; |
| 40 | +import { ComponentRegistry } from '@object-ui/core'; |
| 41 | +import type { ActionContext, ActionDef, ActionResult, ConfirmationHandler } from '@object-ui/core'; |
| 42 | +import { ActionProvider } from '@object-ui/react'; |
| 43 | +// Module-scope side-effect imports: `action:bar` resolves its members and its |
| 44 | +// overflow menu through the ComponentRegistry at render time, and the light |
| 45 | +// `dom` project does not load the `@object-ui/components` graph. Module scope, |
| 46 | +// not a `beforeAll`, per AGENTS.md §测试纪律. |
| 47 | +import '../action-bar'; |
| 48 | +import '../action-button'; |
| 49 | +import '../action-menu'; |
| 50 | + |
| 51 | +/** `examples/app-todo` `DeleteCompletedAction`, as declared. */ |
| 52 | +const DELETE_COMPLETED = { |
| 53 | + name: 'delete_completed', |
| 54 | + label: 'Delete Completed', |
| 55 | + objectName: 'todo_task', |
| 56 | + icon: 'trash', |
| 57 | + type: 'script', |
| 58 | + target: 'deleteCompletedTasks', |
| 59 | + locations: ['list_toolbar'], |
| 60 | + variant: 'danger', |
| 61 | + confirmText: 'Permanently delete all completed tasks? This cannot be undone.', |
| 62 | + successMessage: 'Completed tasks deleted!', |
| 63 | + refreshAfter: true, |
| 64 | + ai: { |
| 65 | + exposed: true, |
| 66 | + description: |
| 67 | + 'Permanently delete every completed todo task. Destructive and irreversible — only after the user confirms.', |
| 68 | + }, |
| 69 | +}; |
| 70 | + |
| 71 | +/** `examples/app-todo` `ExportToCsvAction`, as declared: the same toolbar, no variant. */ |
| 72 | +const EXPORT_CSV = { |
| 73 | + name: 'export_csv', |
| 74 | + label: 'Export to CSV', |
| 75 | + objectName: 'todo_task', |
| 76 | + icon: 'download', |
| 77 | + type: 'script', |
| 78 | + target: 'exportTasksToCSV', |
| 79 | + locations: ['list_toolbar'], |
| 80 | + successMessage: 'Export completed!', |
| 81 | + refreshAfter: false, |
| 82 | + ai: { |
| 83 | + exposed: true, |
| 84 | + description: 'Export the current list of todo tasks to a downloadable CSV file.', |
| 85 | + }, |
| 86 | +}; |
| 87 | + |
| 88 | +let script: Mock<(action: ActionDef, ctx: ActionContext) => Promise<ActionResult>>; |
| 89 | +let confirm: Mock<ConfirmationHandler>; |
| 90 | + |
| 91 | +beforeEach(() => { |
| 92 | + script = vi.fn(async () => ({ success: true })); |
| 93 | + // A cancel: the question is what is measured, not the run. |
| 94 | + confirm = vi.fn(async () => false); |
| 95 | +}); |
| 96 | + |
| 97 | +function renderToolbar() { |
| 98 | + const Bar = ComponentRegistry.get('action:bar'); |
| 99 | + if (!Bar) throw new Error('action:bar is not registered'); |
| 100 | + const schema = { |
| 101 | + type: 'action:bar', |
| 102 | + location: 'list_toolbar', |
| 103 | + actions: [DELETE_COMPLETED, EXPORT_CSV], |
| 104 | + size: 'sm', |
| 105 | + variant: 'outline', |
| 106 | + }; |
| 107 | + render( |
| 108 | + <ActionProvider handlers={{ script }} onConfirm={confirm}> |
| 109 | + <Bar schema={schema as never} /> |
| 110 | + </ActionProvider>, |
| 111 | + ); |
| 112 | + return screen.getByRole('toolbar'); |
| 113 | +} |
| 114 | + |
| 115 | +describe('action:bar draws and confirms a declared `danger` action destructive (objectstack#26781)', () => { |
| 116 | + it('the app-todo `delete_completed` member draws with the destructive button style', () => { |
| 117 | + const toolbar = renderToolbar(); |
| 118 | + const del = within(toolbar).getByRole('button', { name: 'Delete Completed' }); |
| 119 | + expect(del.className).toContain('bg-destructive'); |
| 120 | + expect(del.className).not.toContain('border-input'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('CONTROL: `export_csv` in the same bar declares no variant and draws the bar\'s `outline`', () => { |
| 124 | + const toolbar = renderToolbar(); |
| 125 | + const csv = within(toolbar).getByRole('button', { name: 'Export to CSV' }); |
| 126 | + expect(csv.className).toContain('border-input'); |
| 127 | + expect(csv.className).not.toContain('bg-destructive'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('clicking `delete_completed` asks its confirmText through the destructive confirm button', async () => { |
| 131 | + const toolbar = renderToolbar(); |
| 132 | + fireEvent.click(within(toolbar).getByRole('button', { name: 'Delete Completed' })); |
| 133 | + await waitFor(() => expect(confirm).toHaveBeenCalledTimes(1)); |
| 134 | + expect(script).not.toHaveBeenCalled(); |
| 135 | + const [message, options] = confirm.mock.calls[0]; |
| 136 | + expect(message).toBe(DELETE_COMPLETED.confirmText); |
| 137 | + expect(options).toEqual({ |
| 138 | + title: 'Delete Completed', |
| 139 | + confirmText: 'Delete Completed', |
| 140 | + destructive: true, |
| 141 | + }); |
| 142 | + }); |
| 143 | +}); |
0 commit comments