|
| 1 | +# Component and Renderer Testing Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the comprehensive automated testing infrastructure for ObjectUI components and renderers. The tests are designed to automatically discover display, accessibility, and structural issues in UI components. |
| 6 | + |
| 7 | +## Test Architecture |
| 8 | + |
| 9 | +### Test Utilities (`test-utils.tsx`) |
| 10 | + |
| 11 | +A suite of helper functions that provide automated checks for common display issues: |
| 12 | + |
| 13 | +#### `renderComponent(schema, options)` |
| 14 | +Renders a component from its schema definition for testing. |
| 15 | + |
| 16 | +```typescript |
| 17 | +const { container } = renderComponent({ |
| 18 | + type: 'button', |
| 19 | + label: 'Click Me', |
| 20 | +}); |
| 21 | +``` |
| 22 | + |
| 23 | +#### `checkAccessibility(element)` |
| 24 | +Validates accessibility attributes and identifies common a11y issues: |
| 25 | +- Missing ARIA labels on interactive elements |
| 26 | +- Missing form labels |
| 27 | +- Missing alt attributes on images |
| 28 | + |
| 29 | +Returns: |
| 30 | +```typescript |
| 31 | +{ |
| 32 | + hasRole: boolean; |
| 33 | + hasAriaLabel: boolean; |
| 34 | + hasAriaDescribedBy: boolean; |
| 35 | + issues: string[]; // List of detected issues |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +#### `checkDOMStructure(container)` |
| 40 | +Analyzes DOM structure for potential issues: |
| 41 | +- Empty components |
| 42 | +- Excessive nesting (>20 levels) |
| 43 | +- Missing content |
| 44 | + |
| 45 | +Returns: |
| 46 | +```typescript |
| 47 | +{ |
| 48 | + hasContent: boolean; |
| 49 | + isEmpty: boolean; |
| 50 | + hasChildren: boolean; |
| 51 | + nestedDepth: number; |
| 52 | + issues: string[]; // List of detected issues |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +#### `checkStyling(element)` |
| 57 | +Validates component styling: |
| 58 | +- Class presence |
| 59 | +- Tailwind CSS usage |
| 60 | +- Inline styles |
| 61 | + |
| 62 | +Returns: |
| 63 | +```typescript |
| 64 | +{ |
| 65 | + hasClasses: boolean; |
| 66 | + hasTailwindClasses: boolean; |
| 67 | + hasInlineStyles: boolean; |
| 68 | + classes: string[]; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +#### `validateComponentRegistration(componentType)` |
| 73 | +Verifies component registration in ComponentRegistry: |
| 74 | +- Component is registered |
| 75 | +- Has configuration |
| 76 | +- Has renderer function |
| 77 | +- Has label and inputs |
| 78 | +- Has default props |
| 79 | + |
| 80 | +Returns: |
| 81 | +```typescript |
| 82 | +{ |
| 83 | + isRegistered: boolean; |
| 84 | + hasConfig: boolean; |
| 85 | + hasRenderer: boolean; |
| 86 | + hasLabel: boolean; |
| 87 | + hasInputs: boolean; |
| 88 | + hasDefaultProps: boolean; |
| 89 | + config: any; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +#### `getAllDisplayIssues(container)` |
| 94 | +Comprehensive check that runs all validation checks and returns aggregated issues: |
| 95 | + |
| 96 | +```typescript |
| 97 | +const issues = getAllDisplayIssues(container); |
| 98 | +// Returns array of issue descriptions |
| 99 | +``` |
| 100 | + |
| 101 | +## Test Coverage |
| 102 | + |
| 103 | +### Component Categories |
| 104 | + |
| 105 | +The test suite covers all major component categories: |
| 106 | + |
| 107 | +1. **Basic Components** (`basic-renderers.test.tsx`) |
| 108 | + - Text, Div, Span, Image, Icon, Separator, HTML |
| 109 | + |
| 110 | +2. **Form Components** (`form-renderers.test.tsx`) |
| 111 | + - Button, Input, Textarea, Select, Checkbox, Switch |
| 112 | + - Radio Group, Slider, Label, Email, Password |
| 113 | + |
| 114 | +3. **Layout Components** (`layout-data-renderers.test.tsx`) |
| 115 | + - Container, Grid, Flex |
| 116 | + |
| 117 | +4. **Data Display Components** (`layout-data-renderers.test.tsx`) |
| 118 | + - List, Tree View, Badge, Avatar, Alert |
| 119 | + - Breadcrumb, Statistic, Kbd |
| 120 | + |
| 121 | +5. **Feedback Components** (`feedback-overlay-renderers.test.tsx`) |
| 122 | + - Loading, Spinner, Progress, Skeleton, Empty, Toast |
| 123 | + |
| 124 | +6. **Overlay Components** (`feedback-overlay-renderers.test.tsx`) |
| 125 | + - Dialog, Alert Dialog, Sheet, Drawer |
| 126 | + - Popover, Tooltip, Dropdown Menu, Context Menu |
| 127 | + - Hover Card, Menubar |
| 128 | + |
| 129 | +7. **Disclosure Components** (`complex-disclosure-renderers.test.tsx`) |
| 130 | + - Accordion, Collapsible, Toggle Group |
| 131 | + |
| 132 | +8. **Complex Components** (`complex-disclosure-renderers.test.tsx`) |
| 133 | + - Timeline, Data Table, Chatbot, Carousel |
| 134 | + - Scroll Area, Resizable, Filter Builder, Calendar View, Table |
| 135 | + |
| 136 | +## What the Tests Check |
| 137 | + |
| 138 | +### 1. Component Registration |
| 139 | +Every component should be properly registered: |
| 140 | +```typescript |
| 141 | +it('should be properly registered', () => { |
| 142 | + const validation = validateComponentRegistration('button'); |
| 143 | + expect(validation.isRegistered).toBe(true); |
| 144 | + expect(validation.hasConfig).toBe(true); |
| 145 | + expect(validation.hasLabel).toBe(true); |
| 146 | +}); |
| 147 | +``` |
| 148 | + |
| 149 | +### 2. Rendering Without Errors |
| 150 | +Components should render without throwing errors: |
| 151 | +```typescript |
| 152 | +it('should render without issues', () => { |
| 153 | + const { container } = renderComponent({ |
| 154 | + type: 'button', |
| 155 | + label: 'Test', |
| 156 | + }); |
| 157 | + expect(container).toBeDefined(); |
| 158 | +}); |
| 159 | +``` |
| 160 | + |
| 161 | +### 3. Accessibility |
| 162 | +Components should be accessible: |
| 163 | +```typescript |
| 164 | +it('should have proper accessibility', () => { |
| 165 | + const { container } = renderComponent({ |
| 166 | + type: 'button', |
| 167 | + label: 'Click Me', |
| 168 | + }); |
| 169 | + |
| 170 | + const issues = getAllDisplayIssues(container); |
| 171 | + const a11yIssues = issues.filter(i => i.includes('accessible')); |
| 172 | + expect(a11yIssues).toHaveLength(0); |
| 173 | +}); |
| 174 | +``` |
| 175 | + |
| 176 | +### 4. DOM Structure |
| 177 | +Components should have valid DOM structure: |
| 178 | +```typescript |
| 179 | +it('should have valid structure', () => { |
| 180 | + const { container } = renderComponent({ |
| 181 | + type: 'container', |
| 182 | + body: [{ type: 'text', content: 'Content' }], |
| 183 | + }); |
| 184 | + |
| 185 | + const domCheck = checkDOMStructure(container); |
| 186 | + expect(domCheck.hasContent).toBe(true); |
| 187 | + expect(domCheck.isEmpty).toBe(false); |
| 188 | + expect(domCheck.nestedDepth).toBeLessThan(20); |
| 189 | +}); |
| 190 | +``` |
| 191 | + |
| 192 | +### 5. Props and Variants |
| 193 | +Components should support their documented props: |
| 194 | +```typescript |
| 195 | +it('should support variants', () => { |
| 196 | + const variants = ['default', 'secondary', 'destructive']; |
| 197 | + |
| 198 | + variants.forEach(variant => { |
| 199 | + const { container } = renderComponent({ |
| 200 | + type: 'button', |
| 201 | + label: 'Test', |
| 202 | + variant, |
| 203 | + }); |
| 204 | + expect(container).toBeDefined(); |
| 205 | + }); |
| 206 | +}); |
| 207 | +``` |
| 208 | + |
| 209 | +## Running Tests |
| 210 | + |
| 211 | +### Run All Tests |
| 212 | +```bash |
| 213 | +pnpm test |
| 214 | +``` |
| 215 | + |
| 216 | +### Run Component Tests Only |
| 217 | +```bash |
| 218 | +pnpm vitest run packages/components/src/__tests__/ |
| 219 | +``` |
| 220 | + |
| 221 | +### Run Specific Test File |
| 222 | +```bash |
| 223 | +pnpm vitest run packages/components/src/__tests__/form-renderers.test.tsx |
| 224 | +``` |
| 225 | + |
| 226 | +### Watch Mode |
| 227 | +```bash |
| 228 | +pnpm test:watch |
| 229 | +``` |
| 230 | + |
| 231 | +### Coverage Report |
| 232 | +```bash |
| 233 | +pnpm test:coverage |
| 234 | +``` |
| 235 | + |
| 236 | +### Interactive UI |
| 237 | +```bash |
| 238 | +pnpm test:ui |
| 239 | +``` |
| 240 | + |
| 241 | +## Test Results |
| 242 | + |
| 243 | +Current test coverage: |
| 244 | +- **150 total tests** across all component categories |
| 245 | +- **140 passing** (93% pass rate) |
| 246 | +- **10 failing** - identifying real schema/prop mismatches that need fixing |
| 247 | + |
| 248 | +The failing tests are valuable as they automatically discovered: |
| 249 | +- Components with missing props |
| 250 | +- Schema mismatches (e.g., `content` vs `html`, `text` vs `content`) |
| 251 | +- Missing default values |
| 252 | +- Incorrect prop expectations |
| 253 | + |
| 254 | +## Adding New Tests |
| 255 | + |
| 256 | +### For New Components |
| 257 | + |
| 258 | +When adding a new component, create tests following this pattern: |
| 259 | + |
| 260 | +```typescript |
| 261 | +describe('MyComponent Renderer', () => { |
| 262 | + it('should be properly registered', () => { |
| 263 | + const validation = validateComponentRegistration('my-component'); |
| 264 | + expect(validation.isRegistered).toBe(true); |
| 265 | + expect(validation.hasConfig).toBe(true); |
| 266 | + }); |
| 267 | + |
| 268 | + it('should render without issues', () => { |
| 269 | + const { container } = renderComponent({ |
| 270 | + type: 'my-component', |
| 271 | + // ... required props |
| 272 | + }); |
| 273 | + |
| 274 | + expect(container).toBeDefined(); |
| 275 | + const issues = getAllDisplayIssues(container); |
| 276 | + expect(issues).toHaveLength(0); |
| 277 | + }); |
| 278 | + |
| 279 | + it('should support required props', () => { |
| 280 | + const { container } = renderComponent({ |
| 281 | + type: 'my-component', |
| 282 | + requiredProp: 'value', |
| 283 | + }); |
| 284 | + |
| 285 | + expect(container.textContent).toContain('value'); |
| 286 | + }); |
| 287 | +}); |
| 288 | +``` |
| 289 | + |
| 290 | +### For Display Issue Detection |
| 291 | + |
| 292 | +Add specific checks for known issues: |
| 293 | + |
| 294 | +```typescript |
| 295 | +it('should not have excessive nesting', () => { |
| 296 | + const { container } = renderComponent({ |
| 297 | + type: 'complex-component', |
| 298 | + data: complexData, |
| 299 | + }); |
| 300 | + |
| 301 | + const domCheck = checkDOMStructure(container); |
| 302 | + expect(domCheck.nestedDepth).toBeLessThan(20); |
| 303 | +}); |
| 304 | + |
| 305 | +it('should have proper ARIA attributes', () => { |
| 306 | + const { container } = renderComponent({ |
| 307 | + type: 'interactive-component', |
| 308 | + }); |
| 309 | + |
| 310 | + const button = container.querySelector('button'); |
| 311 | + const a11y = checkAccessibility(button); |
| 312 | + expect(a11y.issues).toHaveLength(0); |
| 313 | +}); |
| 314 | +``` |
| 315 | + |
| 316 | +## Benefits |
| 317 | + |
| 318 | +This testing infrastructure provides: |
| 319 | + |
| 320 | +1. **Automated Issue Detection** - Tests automatically find display and accessibility issues |
| 321 | +2. **Regression Prevention** - Catches breaking changes in component rendering |
| 322 | +3. **Documentation** - Tests serve as examples of how components should be used |
| 323 | +4. **Confidence** - High test coverage ensures components work as expected |
| 324 | +5. **Quick Feedback** - Fast test execution helps during development |
| 325 | + |
| 326 | +## Future Enhancements |
| 327 | + |
| 328 | +Potential improvements: |
| 329 | + |
| 330 | +1. Visual regression testing with screenshot comparison |
| 331 | +2. Performance benchmarking for complex components |
| 332 | +3. Cross-browser testing |
| 333 | +4. Responsive design testing |
| 334 | +5. Theme variation testing |
| 335 | +6. Integration tests with SchemaRenderer |
0 commit comments