|
| 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#30535 — the NON-grid selection bar (kanban / calendar / gallery |
| 11 | + * / …) applies the grid bar's own gate to a declared bulk action, through the |
| 12 | + * grid's own `useBulkActionItems`: |
| 13 | + * |
| 14 | + * - an action whose `requiredPermissions` the caller does not hold is not |
| 15 | + * drawn (ADR-0066 D4; unknown capabilities fail open, as on the grid); |
| 16 | + * - `visible` is evaluated once per selected record, and the action is drawn |
| 17 | + * when at least one selected record passes; |
| 18 | + * - a name repeated in `bulkActions` draws one button. |
| 19 | + * |
| 20 | + * The grid's half is pinned in plugin-grid's `bulkActionCapabilityGate.test.tsx` |
| 21 | + * and `bulk-action-visible.test.tsx`; this file pins the same gate on the bar |
| 22 | + * ListView renders for every other view. |
| 23 | + */ |
| 24 | + |
| 25 | +import React from 'react'; |
| 26 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 27 | +import { render, waitFor, screen, cleanup } from '@testing-library/react'; |
| 28 | +import { ComponentRegistry } from '@object-ui/core'; |
| 29 | +import { ActionProvider, SchemaRendererProvider } from '@object-ui/react'; |
| 30 | +import type { ListViewSchema } from '@object-ui/types'; |
| 31 | +import { ListView } from '../ListView'; |
| 32 | + |
| 33 | +type Row = { id: string; name: string; status: string }; |
| 34 | + |
| 35 | +function makeDataSource(objectName: string, rows: Row[], actions: unknown[]) { |
| 36 | + return { |
| 37 | + find: vi.fn().mockResolvedValue(rows.map(r => ({ ...r }))), |
| 38 | + findOne: vi.fn(), |
| 39 | + create: vi.fn(), |
| 40 | + update: vi.fn(), |
| 41 | + delete: vi.fn(), |
| 42 | + getObjectSchema: vi.fn().mockResolvedValue({ |
| 43 | + name: objectName, |
| 44 | + fields: { id: { type: 'text' }, name: { type: 'text' }, status: { type: 'text' } }, |
| 45 | + actions, |
| 46 | + }), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +/** A gallery stand-in that selects `rows` as soon as it mounts. */ |
| 51 | +function registerSelectingGallery(rows: Row[]) { |
| 52 | + ComponentRegistry.register('object-gallery', (props: any) => { |
| 53 | + const onRowSelect = props.onRowSelect; |
| 54 | + React.useEffect(() => { |
| 55 | + onRowSelect?.(rows); |
| 56 | + }, [onRowSelect]); |
| 57 | + return <div data-testid="gallery-stub" />; |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +function renderGallery( |
| 62 | + objectName: string, |
| 63 | + bulkActions: string[], |
| 64 | + ds: ReturnType<typeof makeDataSource>, |
| 65 | + user?: unknown, |
| 66 | +) { |
| 67 | + const schema: ListViewSchema = { |
| 68 | + type: 'list-view', |
| 69 | + objectName, |
| 70 | + viewType: 'gallery', |
| 71 | + fields: ['name'], |
| 72 | + bulkActions, |
| 73 | + }; |
| 74 | + const tree = ( |
| 75 | + <SchemaRendererProvider dataSource={ds as any}> |
| 76 | + <ListView schema={schema} dataSource={ds as any} onBulkAction={vi.fn()} /> |
| 77 | + </SchemaRendererProvider> |
| 78 | + ); |
| 79 | + return render(user === undefined ? tree : <ActionProvider context={{ user } as any}>{tree}</ActionProvider>); |
| 80 | +} |
| 81 | + |
| 82 | +const OPEN: Row = { id: 'l1', name: 'Lead One', status: 'open' }; |
| 83 | +const CLOSED: Row = { id: 'l2', name: 'Lead Two', status: 'closed' }; |
| 84 | + |
| 85 | +const ACTIONS = [ |
| 86 | + { name: 'tag_lead', label: 'Tag' }, |
| 87 | + { name: 'push_down', label: 'Push Down', requiredPermissions: ['plan.push'] }, |
| 88 | + { name: 'reopen', label: 'Reopen', visible: "record.status == 'closed'" }, |
| 89 | +]; |
| 90 | + |
| 91 | +describe('ListView non-grid bulk bar applies the grid bar gate (objectstack#30535)', () => { |
| 92 | + let prevGallery: ReturnType<typeof ComponentRegistry.get>; |
| 93 | + |
| 94 | + beforeEach(() => { |
| 95 | + prevGallery = ComponentRegistry.get('object-gallery'); |
| 96 | + vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 97 | + }); |
| 98 | + |
| 99 | + afterEach(() => { |
| 100 | + cleanup(); |
| 101 | + vi.restoreAllMocks(); |
| 102 | + if (prevGallery) ComponentRegistry.register('object-gallery', prevGallery); |
| 103 | + else ComponentRegistry.unregister('object-gallery'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('does not draw an action whose requiredPermissions the caller lacks', async () => { |
| 107 | + registerSelectingGallery([OPEN]); |
| 108 | + const ds = makeDataSource('lead_g1', [OPEN], ACTIONS); |
| 109 | + renderGallery('lead_g1', ['tag_lead', 'push_down'], ds, { id: 'u1', systemPermissions: ['setup.access'] }); |
| 110 | + |
| 111 | + await screen.findByTestId('bulk-action-tag_lead'); |
| 112 | + expect(screen.queryByTestId('bulk-action-push_down')).toBeNull(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('draws it when the caller holds every declared capability', async () => { |
| 116 | + registerSelectingGallery([OPEN]); |
| 117 | + const ds = makeDataSource('lead_g2', [OPEN], ACTIONS); |
| 118 | + renderGallery('lead_g2', ['tag_lead', 'push_down'], ds, { id: 'u1', systemPermissions: ['plan.push'] }); |
| 119 | + |
| 120 | + expect(await screen.findByTestId('bulk-action-push_down')).toHaveTextContent('Push Down'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('draws it when the caller capabilities are unknown (fail open, as the grid)', async () => { |
| 124 | + registerSelectingGallery([OPEN]); |
| 125 | + const ds = makeDataSource('lead_g3', [OPEN], ACTIONS); |
| 126 | + renderGallery('lead_g3', ['push_down'], ds); |
| 127 | + |
| 128 | + expect(await screen.findByTestId('bulk-action-push_down')).toBeInTheDocument(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('evaluates visible per selected record: no passing record, no button', async () => { |
| 132 | + registerSelectingGallery([OPEN]); |
| 133 | + const ds = makeDataSource('lead_v1', [OPEN], ACTIONS); |
| 134 | + renderGallery('lead_v1', ['tag_lead', 'reopen'], ds); |
| 135 | + |
| 136 | + await screen.findByTestId('bulk-action-tag_lead'); |
| 137 | + expect(screen.queryByTestId('bulk-action-reopen')).toBeNull(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('evaluates visible per selected record: one passing record draws the button', async () => { |
| 141 | + registerSelectingGallery([OPEN, CLOSED]); |
| 142 | + const ds = makeDataSource('lead_v2', [OPEN, CLOSED], ACTIONS); |
| 143 | + renderGallery('lead_v2', ['tag_lead', 'reopen'], ds); |
| 144 | + |
| 145 | + expect(await screen.findByTestId('bulk-action-reopen')).toHaveTextContent('Reopen'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('draws a repeated name once, with no duplicate React key', async () => { |
| 149 | + registerSelectingGallery([OPEN]); |
| 150 | + const error = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 151 | + const ds = makeDataSource('lead_d1', [OPEN], ACTIONS); |
| 152 | + renderGallery('lead_d1', ['tag_lead', 'tag_lead', 'delete', 'delete'], ds); |
| 153 | + |
| 154 | + await screen.findByTestId('bulk-action-tag_lead'); |
| 155 | + await waitFor(() => expect(screen.getAllByTestId('bulk-action-tag_lead')).toHaveLength(1)); |
| 156 | + expect(screen.getAllByTestId('bulk-action-delete')).toHaveLength(1); |
| 157 | + const keyErrors = error.mock.calls.filter(c => String(c[0]).includes('same key')); |
| 158 | + expect(keyErrors).toEqual([]); |
| 159 | + }); |
| 160 | +}); |
0 commit comments