|
| 1 | +/** |
| 2 | + * DashboardDesignPage Tests |
| 3 | + * |
| 4 | + * Tests the Dashboard Design route integration (/design/dashboard/:dashboardName) |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi } from 'vitest'; |
| 8 | +import { render, screen, fireEvent, act } from '@testing-library/react'; |
| 9 | +import { MemoryRouter, Route, Routes } from 'react-router-dom'; |
| 10 | +import { DashboardDesignPage } from '../pages/DashboardDesignPage'; |
| 11 | + |
| 12 | +// Mock MetadataProvider |
| 13 | +vi.mock('../context/MetadataProvider', () => ({ |
| 14 | + useMetadata: () => ({ |
| 15 | + apps: [], |
| 16 | + objects: [], |
| 17 | + dashboards: [ |
| 18 | + { |
| 19 | + name: 'sales-dashboard', |
| 20 | + type: 'dashboard', |
| 21 | + title: 'Sales Dashboard', |
| 22 | + label: 'Sales Dashboard', |
| 23 | + columns: 3, |
| 24 | + widgets: [ |
| 25 | + { id: 'w1', title: 'Revenue', type: 'metric', layout: { x: 0, y: 0, w: 1, h: 1 } }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + ], |
| 29 | + reports: [], |
| 30 | + pages: [], |
| 31 | + loading: false, |
| 32 | + error: null, |
| 33 | + refresh: vi.fn(), |
| 34 | + }), |
| 35 | +})); |
| 36 | + |
| 37 | +// Mock AdapterProvider |
| 38 | +const { mockUpdate } = vi.hoisted(() => ({ mockUpdate: vi.fn().mockResolvedValue({}) })); |
| 39 | +vi.mock('../context/AdapterProvider', () => ({ |
| 40 | + useAdapter: () => ({ |
| 41 | + update: mockUpdate, |
| 42 | + create: vi.fn().mockResolvedValue({}), |
| 43 | + }), |
| 44 | +})); |
| 45 | + |
| 46 | +// Mock plugin-designer to avoid complex component tree |
| 47 | +vi.mock('@object-ui/plugin-designer', () => ({ |
| 48 | + DashboardEditor: ({ schema, onChange, readOnly }: any) => ( |
| 49 | + <div data-testid="dashboard-editor"> |
| 50 | + <span>DashboardEditor: {schema.title || schema.name}</span> |
| 51 | + <button data-testid="trigger-change" onClick={() => onChange({ ...schema, title: 'Updated Dashboard' })}> |
| 52 | + Change |
| 53 | + </button> |
| 54 | + {readOnly && <span>read-only</span>} |
| 55 | + </div> |
| 56 | + ), |
| 57 | +})); |
| 58 | + |
| 59 | +// Mock sonner toast |
| 60 | +vi.mock('sonner', () => ({ |
| 61 | + toast: { |
| 62 | + success: vi.fn(), |
| 63 | + error: vi.fn(), |
| 64 | + }, |
| 65 | +})); |
| 66 | + |
| 67 | +const renderWithRouter = (dashboardName: string) => |
| 68 | + render( |
| 69 | + <MemoryRouter initialEntries={[`/design/dashboard/${dashboardName}`]}> |
| 70 | + <Routes> |
| 71 | + <Route path="/design/dashboard/:dashboardName" element={<DashboardDesignPage />} /> |
| 72 | + </Routes> |
| 73 | + </MemoryRouter>, |
| 74 | + ); |
| 75 | + |
| 76 | +describe('DashboardDesignPage', () => { |
| 77 | + it('should render the dashboard editor for a known dashboard', () => { |
| 78 | + renderWithRouter('sales-dashboard'); |
| 79 | + |
| 80 | + expect(screen.getByTestId('dashboard-design-page')).toBeInTheDocument(); |
| 81 | + expect(screen.getByText(/Edit Dashboard/)).toBeInTheDocument(); |
| 82 | + expect(screen.getByTestId('dashboard-editor')).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should show 404 for unknown dashboard', () => { |
| 86 | + renderWithRouter('unknown-dashboard'); |
| 87 | + |
| 88 | + expect(screen.getByText(/Dashboard.*not found/i)).toBeInTheDocument(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should render back button', () => { |
| 92 | + renderWithRouter('sales-dashboard'); |
| 93 | + |
| 94 | + expect(screen.getByTestId('dashboard-design-back')).toBeInTheDocument(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should pass schema to the editor', () => { |
| 98 | + renderWithRouter('sales-dashboard'); |
| 99 | + |
| 100 | + expect(screen.getByText(/DashboardEditor: Sales Dashboard/)).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should call onChange when editor triggers a change', () => { |
| 104 | + renderWithRouter('sales-dashboard'); |
| 105 | + |
| 106 | + fireEvent.click(screen.getByTestId('trigger-change')); |
| 107 | + // After change, editor should reflect updated schema |
| 108 | + expect(screen.getByText(/DashboardEditor: Updated Dashboard/)).toBeInTheDocument(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should save via Ctrl+S keyboard shortcut', async () => { |
| 112 | + renderWithRouter('sales-dashboard'); |
| 113 | + |
| 114 | + await act(async () => { |
| 115 | + fireEvent.keyDown(window, { key: 's', ctrlKey: true }); |
| 116 | + }); |
| 117 | + |
| 118 | + // Should call dataSource.update with the dashboard schema |
| 119 | + expect(mockUpdate).toHaveBeenCalledWith('sys_dashboard', 'sales-dashboard', expect.objectContaining({ type: 'dashboard' })); |
| 120 | + }); |
| 121 | +}); |
0 commit comments