|
| 1 | +/** |
| 2 | + * ObjectView — List toolbar action button integration tests |
| 3 | + * |
| 4 | + * Validates that action buttons in the list toolbar header are wired to |
| 5 | + * ActionProvider and respond to clicks: |
| 6 | + * - Actions with confirmText show a confirmation dialog |
| 7 | + * - Actions with params show a param collection dialog |
| 8 | + * - Toast notifications are displayed on success |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 12 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 13 | +import userEvent from '@testing-library/user-event'; |
| 14 | +import '@testing-library/jest-dom'; |
| 15 | +import { ObjectView } from '../components/ObjectView'; |
| 16 | +import { ComponentRegistry } from '@object-ui/core'; |
| 17 | + |
| 18 | +// Mock sonner toast |
| 19 | +const mockToastSuccess = vi.fn(); |
| 20 | +const mockToastError = vi.fn(); |
| 21 | +vi.mock('sonner', () => ({ |
| 22 | + toast: Object.assign(vi.fn(), { |
| 23 | + success: (...args: any[]) => mockToastSuccess(...args), |
| 24 | + error: (...args: any[]) => mockToastError(...args), |
| 25 | + }), |
| 26 | +})); |
| 27 | + |
| 28 | +// Mock child plugins to isolate ObjectView logic |
| 29 | +vi.mock('@object-ui/plugin-grid', () => ({ |
| 30 | + ObjectGrid: (props: any) => <div data-testid="object-grid">Grid View: {props.schema.objectName}</div> |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock('@object-ui/plugin-kanban', () => ({ |
| 34 | + ObjectKanban: (props: any) => <div data-testid="object-kanban">Kanban View</div> |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('@object-ui/plugin-calendar', () => ({ |
| 38 | + ObjectCalendar: (props: any) => <div data-testid="object-calendar">Calendar View</div> |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock('@object-ui/plugin-list', () => ({ |
| 42 | + ListView: (props: any) => ( |
| 43 | + <div data-testid="list-view"> |
| 44 | + <div data-testid="object-grid">Grid View: {props.schema?.objectName}</div> |
| 45 | + <button data-testid="list-row-click" onClick={() => props.onRowClick?.({ id: 'rec-1' })}>Click Row</button> |
| 46 | + </div> |
| 47 | + ), |
| 48 | +})); |
| 49 | + |
| 50 | +vi.mock('@object-ui/components', async (importOriginal) => { |
| 51 | + const actual = await importOriginal<any>(); |
| 52 | + return { |
| 53 | + ...actual, |
| 54 | + cn: (...inputs: any[]) => inputs.filter(Boolean).join(' '), |
| 55 | + Button: ({ children, onClick, title, ...rest }: any) => <button onClick={onClick} title={title} {...rest}>{children}</button>, |
| 56 | + Empty: ({ children }: any) => <div data-testid="empty">{children}</div>, |
| 57 | + EmptyTitle: ({ children }: any) => <div>{children}</div>, |
| 58 | + EmptyDescription: ({ children }: any) => <div>{children}</div>, |
| 59 | + DropdownMenu: ({ children }: any) => <div>{children}</div>, |
| 60 | + DropdownMenuTrigger: ({ children }: any) => <>{children}</>, |
| 61 | + DropdownMenuContent: ({ children }: any) => <div>{children}</div>, |
| 62 | + DropdownMenuItem: ({ children, onClick }: any) => <button onClick={onClick}>{children}</button>, |
| 63 | + DropdownMenuSeparator: () => <hr />, |
| 64 | + }; |
| 65 | +}); |
| 66 | + |
| 67 | +// Mock React Router |
| 68 | +const mockNavigate = vi.fn(); |
| 69 | +vi.mock('react-router-dom', () => ({ |
| 70 | + useParams: () => ({ objectName: 'account', appName: 'crm' }), |
| 71 | + useSearchParams: () => [new URLSearchParams(), vi.fn()], |
| 72 | + useNavigate: () => mockNavigate, |
| 73 | +})); |
| 74 | + |
| 75 | +// Mock auth — default admin so Create button is visible |
| 76 | +vi.mock('@object-ui/auth', () => ({ |
| 77 | + useAuth: () => ({ user: { id: 'u1', name: 'Admin', role: 'admin' } }), |
| 78 | +})); |
| 79 | + |
| 80 | +// ─── Test Data ─────────────────────────────────────────────────────────────── |
| 81 | + |
| 82 | +const listToolbarActions = [ |
| 83 | + { |
| 84 | + name: 'bulk_import', |
| 85 | + label: 'Import Records', |
| 86 | + icon: 'upload', |
| 87 | + type: 'api' as const, |
| 88 | + target: 'bulk_import', |
| 89 | + locations: ['list_toolbar' as const], |
| 90 | + confirmText: 'This will import records from CSV. Continue?', |
| 91 | + refreshAfter: true, |
| 92 | + successMessage: 'Records imported successfully', |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'export_all', |
| 96 | + label: 'Export All', |
| 97 | + icon: 'download', |
| 98 | + type: 'url' as const, |
| 99 | + target: '/api/export/accounts', |
| 100 | + locations: ['list_toolbar' as const], |
| 101 | + }, |
| 102 | +]; |
| 103 | + |
| 104 | +const mockObjects = [ |
| 105 | + { |
| 106 | + name: 'account', |
| 107 | + label: 'Account', |
| 108 | + fields: { |
| 109 | + name: { name: 'name', label: 'Name', type: 'text' }, |
| 110 | + industry: { name: 'industry', label: 'Industry', type: 'text' }, |
| 111 | + }, |
| 112 | + actions: listToolbarActions, |
| 113 | + listViews: { |
| 114 | + all: { label: 'All Accounts', type: 'grid', columns: ['name', 'industry'] }, |
| 115 | + }, |
| 116 | + }, |
| 117 | +]; |
| 118 | + |
| 119 | +function createMockDataSource() { |
| 120 | + return { |
| 121 | + find: vi.fn().mockResolvedValue({ data: [], total: 0 }), |
| 122 | + findOne: vi.fn().mockResolvedValue(null), |
| 123 | + create: vi.fn().mockResolvedValue({ id: '1' }), |
| 124 | + update: vi.fn().mockResolvedValue({ id: '1' }), |
| 125 | + delete: vi.fn().mockResolvedValue(true), |
| 126 | + execute: vi.fn().mockResolvedValue({ success: true }), |
| 127 | + } as any; |
| 128 | +} |
| 129 | + |
| 130 | +// ─── Tests ─────────────────────────────────────────────────────────────────── |
| 131 | + |
| 132 | +describe('ObjectView — List toolbar action buttons', () => { |
| 133 | + beforeEach(() => { |
| 134 | + vi.clearAllMocks(); |
| 135 | + // Register mock components for SchemaRenderer to find |
| 136 | + ComponentRegistry.register('object-grid', (props: any) => <div data-testid="object-grid">Grid View: {props.schema.objectName}</div>); |
| 137 | + ComponentRegistry.register('list-view', (_props: any) => <div data-testid="list-view">List View</div>); |
| 138 | + }); |
| 139 | + |
| 140 | + it('renders action buttons in the list toolbar', async () => { |
| 141 | + const ds = createMockDataSource(); |
| 142 | + render(<ObjectView dataSource={ds} objects={mockObjects} onEdit={vi.fn()} />); |
| 143 | + |
| 144 | + // Action buttons should be visible |
| 145 | + await waitFor(() => { |
| 146 | + expect(screen.getByRole('button', { name: /Import Records/i })).toBeInTheDocument(); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('shows confirmation dialog when action button with confirmText is clicked', async () => { |
| 151 | + const user = userEvent.setup(); |
| 152 | + const ds = createMockDataSource(); |
| 153 | + render(<ObjectView dataSource={ds} objects={mockObjects} onEdit={vi.fn()} />); |
| 154 | + |
| 155 | + // Wait for the Import Records button to render |
| 156 | + await waitFor(() => { |
| 157 | + expect(screen.getByRole('button', { name: /Import Records/i })).toBeInTheDocument(); |
| 158 | + }); |
| 159 | + |
| 160 | + // Click Import Records (has confirmText) |
| 161 | + await user.click(screen.getByRole('button', { name: /Import Records/i })); |
| 162 | + |
| 163 | + // Confirmation dialog should appear |
| 164 | + await waitFor(() => { |
| 165 | + expect(screen.getByText('This will import records from CSV. Continue?')).toBeInTheDocument(); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + it('calls dataSource.execute when confirmation is accepted', async () => { |
| 170 | + const user = userEvent.setup(); |
| 171 | + const ds = createMockDataSource(); |
| 172 | + render(<ObjectView dataSource={ds} objects={mockObjects} onEdit={vi.fn()} />); |
| 173 | + |
| 174 | + // Wait for button to render |
| 175 | + await waitFor(() => { |
| 176 | + expect(screen.getByRole('button', { name: /Import Records/i })).toBeInTheDocument(); |
| 177 | + }); |
| 178 | + |
| 179 | + // Click action button |
| 180 | + await user.click(screen.getByRole('button', { name: /Import Records/i })); |
| 181 | + |
| 182 | + // Wait for confirmation dialog |
| 183 | + await waitFor(() => { |
| 184 | + expect(screen.getByText('This will import records from CSV. Continue?')).toBeInTheDocument(); |
| 185 | + }); |
| 186 | + |
| 187 | + // Click Continue to confirm |
| 188 | + await user.click(screen.getByRole('button', { name: /Continue/i })); |
| 189 | + |
| 190 | + // dataSource.execute should be called |
| 191 | + await waitFor(() => { |
| 192 | + expect(ds.execute).toHaveBeenCalledWith('account', 'bulk_import', {}); |
| 193 | + }, { timeout: 3000 }); |
| 194 | + }); |
| 195 | + |
| 196 | + it('does not execute action when confirmation is cancelled', async () => { |
| 197 | + const user = userEvent.setup(); |
| 198 | + const ds = createMockDataSource(); |
| 199 | + render(<ObjectView dataSource={ds} objects={mockObjects} onEdit={vi.fn()} />); |
| 200 | + |
| 201 | + // Wait for button |
| 202 | + await waitFor(() => { |
| 203 | + expect(screen.getByRole('button', { name: /Import Records/i })).toBeInTheDocument(); |
| 204 | + }); |
| 205 | + |
| 206 | + // Click action button |
| 207 | + await user.click(screen.getByRole('button', { name: /Import Records/i })); |
| 208 | + |
| 209 | + // Wait for confirmation dialog |
| 210 | + await waitFor(() => { |
| 211 | + expect(screen.getByText('This will import records from CSV. Continue?')).toBeInTheDocument(); |
| 212 | + }); |
| 213 | + |
| 214 | + // Click Cancel |
| 215 | + await user.click(screen.getByRole('button', { name: /Cancel/i })); |
| 216 | + |
| 217 | + // dataSource.execute should NOT be called |
| 218 | + await new Promise(r => setTimeout(r, 100)); |
| 219 | + expect(ds.execute).not.toHaveBeenCalled(); |
| 220 | + }); |
| 221 | +}); |
0 commit comments