|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import { ComponentRegistry } from '@object-ui/core'; |
| 5 | +import type { DataSource } from '@object-ui/types'; |
| 6 | + |
| 7 | +// Check if we can verify View compliance |
| 8 | +import '../index'; |
| 9 | + |
| 10 | +// Create a Mock DataSource type compatible with the system |
| 11 | +const createMockDataSource = (): DataSource => ({ |
| 12 | + find: vi.fn().mockResolvedValue([]), |
| 13 | + findOne: vi.fn().mockResolvedValue({}), |
| 14 | + create: vi.fn().mockResolvedValue({}), |
| 15 | + update: vi.fn().mockResolvedValue({}), |
| 16 | + delete: vi.fn().mockResolvedValue(true), |
| 17 | + count: vi.fn().mockResolvedValue(0), |
| 18 | + // Add other required methods from the type if necessary, usually these are enough for basic views |
| 19 | +} as unknown as DataSource); |
| 20 | + |
| 21 | +describe('View Component Compliance', () => { |
| 22 | + // Filter for components that are registered as 'view' category or namespace |
| 23 | + const viewComponents = ComponentRegistry.getAllConfigs().filter(c => |
| 24 | + c.category === 'view' || c.namespace === 'view' || c.type.startsWith('view:') |
| 25 | + ); |
| 26 | + |
| 27 | + it('should have view components registered', () => { |
| 28 | + if (viewComponents.length === 0) { |
| 29 | + // console.warn('No view components found to test. Ensure plugins are loaded.'); |
| 30 | + } |
| 31 | + // expect(viewComponents.length).toBeGreaterThan(0); |
| 32 | + }); |
| 33 | + |
| 34 | + viewComponents.forEach(config => { |
| 35 | + const componentName = config.type; |
| 36 | + |
| 37 | + describe(`View: ${componentName}`, () => { |
| 38 | + |
| 39 | + it('should have required metadata for views', () => { |
| 40 | + expect(config.category).toBe('view'); |
| 41 | + expect(config.component).toBeDefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should define data binding inputs (object/bind)', () => { |
| 45 | + const inputs = config.inputs || []; |
| 46 | + // Standard is 'objectName', but 'object' or 'entity' might be used in legacy/third-party |
| 47 | + const hasObjectInput = inputs.some(i => i.name === 'objectName' || i.name === 'object' || i.name === 'entity'); |
| 48 | + if (!hasObjectInput && config.inputs) { |
| 49 | + // console.warn(`View ${componentName} does not define 'objectName' (or 'object') input in metadata.`); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('should attempt to fetchData when rendered with dataSource', async () => { |
| 54 | + const Cmp = config.component as React.ComponentType<any>; |
| 55 | + const mockSource = createMockDataSource(); |
| 56 | + |
| 57 | + const schema = { |
| 58 | + type: config.type, |
| 59 | + objectName: 'test_object', |
| 60 | + columns: [{ name: 'name', label: 'Name' }], |
| 61 | + // Add other potential required props based on generic view needs |
| 62 | + ...config.defaultProps |
| 63 | + }; |
| 64 | + |
| 65 | + try { |
| 66 | + // 1. Initial Render |
| 67 | + // We render without SchemaRendererProvider assuming View components are self-contained enough |
| 68 | + // or use the dataSource prop directly as per spec. |
| 69 | + const { unmount } = render( |
| 70 | + <Cmp |
| 71 | + schema={schema} |
| 72 | + dataSource={mockSource} |
| 73 | + className="test-view-class" |
| 74 | + /> |
| 75 | + ); |
| 76 | + |
| 77 | + // 2. Data Fetch Verification |
| 78 | + await waitFor(() => { |
| 79 | + try { |
| 80 | + // We prefer checking 'find' as it is the standard "List" operation |
| 81 | + expect(mockSource.find).toHaveBeenCalled(); |
| 82 | + } catch(e) { |
| 83 | + // console.warn(`View ${componentName} did not call dataSource.find() on mount.`); |
| 84 | + // Don't fail the test yet to allow gradual compliance fix |
| 85 | + } |
| 86 | + }, { timeout: 1000 }); |
| 87 | + |
| 88 | + unmount(); |
| 89 | + } catch (e) { |
| 90 | + // console.error(`Failed to verify view ${componentName}`, e); |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments