|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import { ObjectView } from '../components/ObjectView'; |
| 4 | +import { ComponentRegistry } from '@object-ui/core'; |
| 5 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 6 | + |
| 7 | +// Import all plugins to simulate main.tsx |
| 8 | +import '@object-ui/plugin-grid'; |
| 9 | +import '@object-ui/plugin-kanban'; |
| 10 | +import '@object-ui/plugin-calendar'; |
| 11 | +import '@object-ui/plugin-gantt'; |
| 12 | +import '@object-ui/plugin-charts'; |
| 13 | +import '@object-ui/plugin-list'; |
| 14 | +import '@object-ui/plugin-detail'; |
| 15 | +import '@object-ui/plugin-timeline'; |
| 16 | +import '@object-ui/plugin-map'; |
| 17 | + |
| 18 | +// Mock UI components from @object-ui/components that use Layout/Radix to simplify DOM |
| 19 | +vi.mock('@object-ui/components', async () => { |
| 20 | + const actual = await vi.importActual('@object-ui/components'); |
| 21 | + return { |
| 22 | + ...actual, |
| 23 | + // Override components that might be troublesome in JSDOM or just too verbose |
| 24 | + // We keep basic structure to allow finding by text/label |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +// Mock React Router |
| 29 | +const mockSetSearchParams = vi.fn(); |
| 30 | +let mockSearchParams = new URLSearchParams(); |
| 31 | + |
| 32 | +vi.mock('react-router-dom', () => ({ |
| 33 | + useParams: () => ({ objectName: 'project_task' }), |
| 34 | + useSearchParams: () => [mockSearchParams, mockSetSearchParams], |
| 35 | +})); |
| 36 | + |
| 37 | +const mockDataSource = { |
| 38 | + find: vi.fn().mockResolvedValue([]), |
| 39 | + findOne: vi.fn().mockResolvedValue({}), |
| 40 | + create: vi.fn().mockResolvedValue({}), |
| 41 | + update: vi.fn().mockResolvedValue({}), |
| 42 | + delete: vi.fn().mockResolvedValue(true), |
| 43 | + getObjectSchema: vi.fn().mockResolvedValue(null) // Mock metadata retrieval |
| 44 | +}; |
| 45 | + |
| 46 | +const mockObjects = [ |
| 47 | + { |
| 48 | + name: 'project_task', |
| 49 | + label: 'Project Task', |
| 50 | + fields: { |
| 51 | + name: { label: 'Name', type: 'text' }, |
| 52 | + due_date: { label: 'Due Date', type: 'date' }, |
| 53 | + status: { label: 'Status', type: 'select', options: ['Todo', 'Done'] }, |
| 54 | + location: { label: 'Location', type: 'location' } |
| 55 | + }, |
| 56 | + list_views: { |
| 57 | + all: { label: 'All Tasks', type: 'grid' }, |
| 58 | + board: { label: 'Board', type: 'kanban', groupBy: 'status' }, |
| 59 | + schedule: { label: 'Schedule', type: 'calendar', dateField: 'due_date' }, |
| 60 | + roadmap: { label: 'Roadmap', type: 'gantt', startDateField: 'start', endDateField: 'end' }, |
| 61 | + history: { label: 'History', type: 'timeline', dateField: 'due_date' }, |
| 62 | + sites: { label: 'Sites', type: 'map', locationField: 'location' } |
| 63 | + } |
| 64 | + } |
| 65 | +]; |
| 66 | + |
| 67 | +describe('Console View Switching Integration', () => { |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + vi.clearAllMocks(); |
| 71 | + mockSearchParams = new URLSearchParams(); |
| 72 | + }); |
| 73 | + |
| 74 | + // Helper to render with Provider |
| 75 | + const renderObjectView = () => { |
| 76 | + return render( |
| 77 | + <SchemaRendererProvider dataSource={mockDataSource}> |
| 78 | + <ObjectView dataSource={mockDataSource} objects={mockObjects} onEdit={vi.fn()} /> |
| 79 | + </SchemaRendererProvider> |
| 80 | + ); |
| 81 | + }; |
| 82 | + |
| 83 | + it('renders all view tabs', () => { |
| 84 | + renderObjectView(); |
| 85 | + |
| 86 | + expect(screen.getByText('All Tasks')).toBeInTheDocument(); |
| 87 | + expect(screen.getByText('Board')).toBeInTheDocument(); |
| 88 | + expect(screen.getByText('Schedule')).toBeInTheDocument(); |
| 89 | + expect(screen.getByText('Roadmap')).toBeInTheDocument(); |
| 90 | + expect(screen.getByText('History')).toBeInTheDocument(); |
| 91 | + expect(screen.getByText('Sites')).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('switches to Timeline view correctly', () => { |
| 95 | + // Force view to 'history' (timeline) |
| 96 | + mockSearchParams.set('view', 'history'); |
| 97 | + |
| 98 | + renderObjectView(); |
| 99 | + |
| 100 | + // Should NOT show "Unknown component type" |
| 101 | + expect(screen.queryByText(/Unknown component type/i)).not.toBeInTheDocument(); |
| 102 | + |
| 103 | + expect(ComponentRegistry.has('object-timeline')).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('switches to Map view correctly', () => { |
| 107 | + mockSearchParams.set('view', 'sites'); |
| 108 | + renderObjectView(); |
| 109 | + |
| 110 | + expect(screen.queryByText(/Unknown component type/i)).not.toBeInTheDocument(); |
| 111 | + expect(ComponentRegistry.has('object-map')).toBe(true); |
| 112 | + }); |
| 113 | + |
| 114 | + it('switches to Gantt view correctly', () => { |
| 115 | + mockSearchParams.set('view', 'roadmap'); |
| 116 | + renderObjectView(); |
| 117 | + |
| 118 | + expect(screen.queryByText(/Unknown component type/i)).not.toBeInTheDocument(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments