|
| 1 | +/** |
| 2 | + * ViewConfigPanel Tests |
| 3 | + * |
| 4 | + * Tests for the Airtable-style inline view configuration panel. |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi } from 'vitest'; |
| 8 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 9 | +import '@testing-library/jest-dom'; |
| 10 | +import { ViewConfigPanel } from '../components/ViewConfigPanel'; |
| 11 | + |
| 12 | +// Mock i18n — return keys as-is for test assertions |
| 13 | +vi.mock('@object-ui/i18n', () => ({ |
| 14 | + useObjectTranslation: () => ({ |
| 15 | + t: (key: string, params?: Record<string, any>) => { |
| 16 | + if (params?.count !== undefined) return key.replace('{{count}}', String(params.count)); |
| 17 | + return key; |
| 18 | + }, |
| 19 | + }), |
| 20 | +})); |
| 21 | + |
| 22 | +// Mock components to simple HTML elements |
| 23 | +vi.mock('@object-ui/components', () => ({ |
| 24 | + Button: ({ children, onClick, title, ...props }: any) => ( |
| 25 | + <button onClick={onClick} title={title} {...props}>{children}</button> |
| 26 | + ), |
| 27 | +})); |
| 28 | + |
| 29 | +const mockActiveView = { |
| 30 | + id: 'all', |
| 31 | + label: 'All Records', |
| 32 | + type: 'grid', |
| 33 | + columns: ['name', 'stage', 'amount'], |
| 34 | + filter: [{ field: 'stage', operator: '=', value: 'active' }], |
| 35 | + sort: [{ field: 'name', order: 'asc' }], |
| 36 | +}; |
| 37 | + |
| 38 | +const mockObjectDef = { |
| 39 | + name: 'opportunity', |
| 40 | + label: 'Opportunity', |
| 41 | + description: 'Track sales pipeline and deals', |
| 42 | + fields: { |
| 43 | + name: { label: 'Name', type: 'text' }, |
| 44 | + stage: { label: 'Stage', type: 'select' }, |
| 45 | + amount: { label: 'Amount', type: 'currency' }, |
| 46 | + }, |
| 47 | +}; |
| 48 | + |
| 49 | +describe('ViewConfigPanel', () => { |
| 50 | + it('renders nothing when closed', () => { |
| 51 | + render( |
| 52 | + <ViewConfigPanel |
| 53 | + open={false} |
| 54 | + onClose={vi.fn()} |
| 55 | + activeView={mockActiveView} |
| 56 | + objectDef={mockObjectDef} |
| 57 | + /> |
| 58 | + ); |
| 59 | + |
| 60 | + expect(screen.queryByTestId('view-config-panel')).not.toBeInTheDocument(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('renders panel with all sections when open', () => { |
| 64 | + render( |
| 65 | + <ViewConfigPanel |
| 66 | + open={true} |
| 67 | + onClose={vi.fn()} |
| 68 | + activeView={mockActiveView} |
| 69 | + objectDef={mockObjectDef} |
| 70 | + /> |
| 71 | + ); |
| 72 | + |
| 73 | + expect(screen.getByTestId('view-config-panel')).toBeInTheDocument(); |
| 74 | + |
| 75 | + // Check section headers |
| 76 | + expect(screen.getByText('console.objectView.page')).toBeInTheDocument(); |
| 77 | + expect(screen.getByText('console.objectView.data')).toBeInTheDocument(); |
| 78 | + expect(screen.getByText('console.objectView.appearance')).toBeInTheDocument(); |
| 79 | + expect(screen.getByText('console.objectView.userFilters')).toBeInTheDocument(); |
| 80 | + expect(screen.getByText('console.objectView.userActions')).toBeInTheDocument(); |
| 81 | + expect(screen.getByText('console.objectView.advanced')).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('displays view title', () => { |
| 85 | + render( |
| 86 | + <ViewConfigPanel |
| 87 | + open={true} |
| 88 | + onClose={vi.fn()} |
| 89 | + activeView={mockActiveView} |
| 90 | + objectDef={mockObjectDef} |
| 91 | + /> |
| 92 | + ); |
| 93 | + |
| 94 | + expect(screen.getByText('All Records')).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('displays object description', () => { |
| 98 | + render( |
| 99 | + <ViewConfigPanel |
| 100 | + open={true} |
| 101 | + onClose={vi.fn()} |
| 102 | + activeView={mockActiveView} |
| 103 | + objectDef={mockObjectDef} |
| 104 | + /> |
| 105 | + ); |
| 106 | + |
| 107 | + expect(screen.getByText('Track sales pipeline and deals')).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('shows "no description" when object has no description', () => { |
| 111 | + render( |
| 112 | + <ViewConfigPanel |
| 113 | + open={true} |
| 114 | + onClose={vi.fn()} |
| 115 | + activeView={mockActiveView} |
| 116 | + objectDef={{ ...mockObjectDef, description: undefined }} |
| 117 | + /> |
| 118 | + ); |
| 119 | + |
| 120 | + expect(screen.getByText('console.objectView.noDescription')).toBeInTheDocument(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('displays column count', () => { |
| 124 | + render( |
| 125 | + <ViewConfigPanel |
| 126 | + open={true} |
| 127 | + onClose={vi.fn()} |
| 128 | + activeView={mockActiveView} |
| 129 | + objectDef={mockObjectDef} |
| 130 | + /> |
| 131 | + ); |
| 132 | + |
| 133 | + // 3 columns configured |
| 134 | + expect(screen.getByText('console.objectView.columnsConfigured'.replace('{{count}}', '3'))).toBeInTheDocument(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('displays object source name', () => { |
| 138 | + render( |
| 139 | + <ViewConfigPanel |
| 140 | + open={true} |
| 141 | + onClose={vi.fn()} |
| 142 | + activeView={mockActiveView} |
| 143 | + objectDef={mockObjectDef} |
| 144 | + /> |
| 145 | + ); |
| 146 | + |
| 147 | + expect(screen.getByText('Opportunity')).toBeInTheDocument(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('displays view type', () => { |
| 151 | + render( |
| 152 | + <ViewConfigPanel |
| 153 | + open={true} |
| 154 | + onClose={vi.fn()} |
| 155 | + activeView={mockActiveView} |
| 156 | + objectDef={mockObjectDef} |
| 157 | + /> |
| 158 | + ); |
| 159 | + |
| 160 | + expect(screen.getByText('Grid')).toBeInTheDocument(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('calls onClose when close button is clicked', () => { |
| 164 | + const onClose = vi.fn(); |
| 165 | + render( |
| 166 | + <ViewConfigPanel |
| 167 | + open={true} |
| 168 | + onClose={onClose} |
| 169 | + activeView={mockActiveView} |
| 170 | + objectDef={mockObjectDef} |
| 171 | + /> |
| 172 | + ); |
| 173 | + |
| 174 | + const closeBtn = screen.getByTitle('console.objectView.closePanel'); |
| 175 | + fireEvent.click(closeBtn); |
| 176 | + |
| 177 | + expect(onClose).toHaveBeenCalledOnce(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('updates content when activeView changes', () => { |
| 181 | + const { rerender } = render( |
| 182 | + <ViewConfigPanel |
| 183 | + open={true} |
| 184 | + onClose={vi.fn()} |
| 185 | + activeView={mockActiveView} |
| 186 | + objectDef={mockObjectDef} |
| 187 | + /> |
| 188 | + ); |
| 189 | + |
| 190 | + expect(screen.getByText('All Records')).toBeInTheDocument(); |
| 191 | + expect(screen.getByText('Grid')).toBeInTheDocument(); |
| 192 | + |
| 193 | + // Switch to kanban view |
| 194 | + rerender( |
| 195 | + <ViewConfigPanel |
| 196 | + open={true} |
| 197 | + onClose={vi.fn()} |
| 198 | + activeView={{ id: 'pipeline', label: 'Pipeline', type: 'kanban', columns: ['name'] }} |
| 199 | + objectDef={mockObjectDef} |
| 200 | + /> |
| 201 | + ); |
| 202 | + |
| 203 | + expect(screen.getByText('Pipeline')).toBeInTheDocument(); |
| 204 | + expect(screen.getByText('Kanban')).toBeInTheDocument(); |
| 205 | + }); |
| 206 | + |
| 207 | + it('shows "None" for empty filters and columns', () => { |
| 208 | + render( |
| 209 | + <ViewConfigPanel |
| 210 | + open={true} |
| 211 | + onClose={vi.fn()} |
| 212 | + activeView={{ id: 'empty', label: 'Empty View', type: 'grid' }} |
| 213 | + objectDef={mockObjectDef} |
| 214 | + /> |
| 215 | + ); |
| 216 | + |
| 217 | + // Should show "None" for columns, filters |
| 218 | + const noneTexts = screen.getAllByText('console.objectView.none'); |
| 219 | + expect(noneTexts.length).toBeGreaterThanOrEqual(2); |
| 220 | + }); |
| 221 | +}); |
0 commit comments