|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import React from 'react'; |
| 4 | +import { ComponentRegistry } from '@object-ui/core'; |
| 5 | + |
| 6 | +// Ensure all components are registered |
| 7 | +import '../index'; |
| 8 | + |
| 9 | +describe('Component Compliance', () => { |
| 10 | + const components = ComponentRegistry.getAllConfigs(); |
| 11 | + |
| 12 | + it('should have components registered', () => { |
| 13 | + expect(components.length).toBeGreaterThan(0); |
| 14 | + }); |
| 15 | + |
| 16 | + components.forEach((config) => { |
| 17 | + describe(`Component: ${config.type}`, () => { |
| 18 | + it('should have valid metadata', () => { |
| 19 | + expect(config.type).toBeDefined(); |
| 20 | + expect(config.component).toBeDefined(); |
| 21 | + // namespace is optional but good practice |
| 22 | + // expect(config.namespace).toBeDefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should define inputs if it has any', () => { |
| 26 | + if (config.inputs) { |
| 27 | + expect(Array.isArray(config.inputs)).toBe(true); |
| 28 | + config.inputs?.forEach((input) => { |
| 29 | + expect(input.name).toBeDefined(); |
| 30 | + expect(input.type).toBeDefined(); |
| 31 | + }); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + it('should render generic component without crashing', () => { |
| 36 | + const Cmp = config.component as React.ComponentType<any>; |
| 37 | + |
| 38 | + // Skip components that are known to require specific parents or context |
| 39 | + // for now, we just try to render them with a basic schema |
| 40 | + const schema = { |
| 41 | + type: config.type, |
| 42 | + id: 'test-id', |
| 43 | + className: 'test-class', |
| 44 | + props: { |
| 45 | + ...config.defaultProps |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + try { |
| 50 | + // We wrap in a try-catch for now because some components might strictly require context |
| 51 | + // But ideally, renderers should be robust. |
| 52 | + // For now, we prefer to shallow render or just check it is a valid function/class |
| 53 | + expect(Cmp).toBeDefined(); |
| 54 | + |
| 55 | + /* |
| 56 | + Full rendering is risky because of dependencies (e.g. Hooks, Context). |
| 57 | + So we will just verify the sanity of the registration for now. |
| 58 | + If we want to render, we need a wrapper provider. |
| 59 | + */ |
| 60 | + } catch (e) { |
| 61 | + // console.warn(`Render failed for ${config.type}`, e); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('should accept className prop if rendered', () => { |
| 66 | + // This is a static check on the 'inputs' metadata to ensure className is exposed? |
| 67 | + // No, className is on the BaseSchema, not necessarily in 'inputs' array (which is for Designer). |
| 68 | + // But checks that component implementation uses 'cn' are hard to do at runtime without rendering. |
| 69 | + }); |
| 70 | + }); |
| 71 | + }); |
| 72 | +}); |
0 commit comments