|
| 1 | +# Testing Guide for AMIS Application |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the testing infrastructure and test suites for the ObjectOS AMIS application. |
| 6 | + |
| 7 | +## Test Framework |
| 8 | + |
| 9 | +- **Test Runner**: Vitest 2.1.8 |
| 10 | +- **Test Library**: @testing-library/react 16.3.1 |
| 11 | +- **DOM Assertions**: @testing-library/jest-dom 6.9.1 |
| 12 | +- **Coverage Provider**: v8 |
| 13 | + |
| 14 | +## Running Tests |
| 15 | + |
| 16 | +```bash |
| 17 | +# Run all tests |
| 18 | +pnpm test |
| 19 | + |
| 20 | +# Run tests in watch mode |
| 21 | +pnpm test --watch |
| 22 | + |
| 23 | +# Run tests with UI |
| 24 | +pnpm test:ui |
| 25 | + |
| 26 | +# Run tests with coverage |
| 27 | +pnpm test:coverage |
| 28 | +``` |
| 29 | + |
| 30 | +## Test Structure |
| 31 | + |
| 32 | +``` |
| 33 | +apps/amis/ |
| 34 | +├── src/__tests__/ # Unit and integration tests |
| 35 | +│ ├── schemaBuilder.test.ts # Schema conversion tests |
| 36 | +│ ├── AmisRenderer.test.tsx # Component tests |
| 37 | +│ ├── ObjectPage.test.tsx # Page component tests |
| 38 | +│ ├── api.test.ts # API client tests |
| 39 | +│ └── integration.test.tsx # Integration tests |
| 40 | +├── test/ |
| 41 | +│ ├── setup.ts # Test setup and configuration |
| 42 | +│ └── testUtils.tsx # Test utilities and helpers |
| 43 | +└── vitest.config.ts # Vitest configuration |
| 44 | +``` |
| 45 | + |
| 46 | +## Test Suites |
| 47 | + |
| 48 | +### 1. Schema Builder Tests (`schemaBuilder.test.ts`) |
| 49 | + |
| 50 | +Tests the ObjectQL to AMIS schema conversion logic. |
| 51 | + |
| 52 | +**Coverage:** |
| 53 | +- Field type mapping (form and table) |
| 54 | +- CRUD schema generation |
| 55 | +- Column configuration |
| 56 | +- Form field configuration |
| 57 | +- Validation rules |
| 58 | +- Field options and lookups |
| 59 | +- Pagination settings |
| 60 | +- Bulk actions |
| 61 | + |
| 62 | +**Key Test Cases:** |
| 63 | +- ✅ 30 tests covering all field types |
| 64 | +- ✅ Form type mappings |
| 65 | +- ✅ Table column type mappings |
| 66 | +- ✅ Required field handling |
| 67 | +- ✅ Min/max validation |
| 68 | +- ✅ Select field options |
| 69 | +- ✅ Lookup field configuration |
| 70 | +- ✅ Currency and percent formatting |
| 71 | + |
| 72 | +### 2. AMIS Renderer Tests (`AmisRenderer.test.tsx`) |
| 73 | + |
| 74 | +Tests the core AMIS rendering component. |
| 75 | + |
| 76 | +**Coverage:** |
| 77 | +- Component rendering |
| 78 | +- Schema passing to AMIS |
| 79 | +- Data context handling |
| 80 | +- User context injection |
| 81 | + |
| 82 | +**Key Test Cases:** |
| 83 | +- ✅ 4 tests for component behavior |
| 84 | +- ✅ Renders without crashing |
| 85 | +- ✅ Passes schema to AMIS correctly |
| 86 | +- ✅ Merges custom data |
| 87 | +- ✅ Includes user in context |
| 88 | + |
| 89 | +### 3. Object Page Tests (`ObjectPage.test.tsx`) |
| 90 | + |
| 91 | +Tests the dynamic object page component. |
| 92 | + |
| 93 | +**Coverage:** |
| 94 | +- Loading states |
| 95 | +- Metadata fetching |
| 96 | +- Error handling |
| 97 | +- Schema rendering |
| 98 | + |
| 99 | +**Key Test Cases:** |
| 100 | +- ✅ 5 tests for page lifecycle |
| 101 | +- ✅ Shows loading spinner |
| 102 | +- ✅ Fetches metadata from API |
| 103 | +- ✅ Calls correct API endpoint |
| 104 | +- ✅ Displays error messages |
| 105 | +- ✅ Handles network errors |
| 106 | + |
| 107 | +### 4. API Client Tests (`api.test.ts`) |
| 108 | + |
| 109 | +Tests the Axios API client configuration. |
| 110 | + |
| 111 | +**Coverage:** |
| 112 | +- API client exports |
| 113 | +- HTTP methods availability |
| 114 | + |
| 115 | +**Key Test Cases:** |
| 116 | +- ✅ 1 test for API client structure |
| 117 | +- ✅ Verifies all HTTP methods exist |
| 118 | + |
| 119 | +### 5. Integration Tests (`integration.test.tsx`) |
| 120 | + |
| 121 | +Tests the complete workflow from routing to rendering. |
| 122 | + |
| 123 | +**Coverage:** |
| 124 | +- Full page loading flow |
| 125 | +- Metadata to schema conversion |
| 126 | +- Multiple object types |
| 127 | +- Error scenarios |
| 128 | +- Route changes |
| 129 | + |
| 130 | +**Key Test Cases:** |
| 131 | +- ✅ 6 integration tests |
| 132 | +- ✅ Loads contacts object page |
| 133 | +- ✅ Generates correct column count |
| 134 | +- ✅ Generates correct form fields |
| 135 | +- ✅ Handles lookup fields |
| 136 | +- ✅ Graceful error handling |
| 137 | +- ✅ Refetches on route change |
| 138 | + |
| 139 | +## Test Utilities |
| 140 | + |
| 141 | +### Mock Data (`test/testUtils.tsx`) |
| 142 | + |
| 143 | +Provides reusable mock data for tests: |
| 144 | + |
| 145 | +```typescript |
| 146 | +import { mockObjectMetadata, mockApiResponses } from '@/test/testUtils' |
| 147 | + |
| 148 | +// Use in tests |
| 149 | +const contactsMeta = mockObjectMetadata.contacts |
| 150 | +const contactsData = mockApiResponses.data.contacts |
| 151 | +``` |
| 152 | + |
| 153 | +### Custom Render |
| 154 | + |
| 155 | +```typescript |
| 156 | +import { renderWithProviders } from '@/test/testUtils' |
| 157 | + |
| 158 | +// Renders with MemoryRouter |
| 159 | +renderWithProviders(<YourComponent />, { |
| 160 | + initialRoute: '/object/contacts' |
| 161 | +}) |
| 162 | +``` |
| 163 | + |
| 164 | +## Mocking Strategy |
| 165 | + |
| 166 | +### AMIS Framework |
| 167 | + |
| 168 | +```typescript |
| 169 | +vi.mock('amis', () => ({ |
| 170 | + render: vi.fn((schema, props) => { |
| 171 | + return null // AMIS renders directly to DOM |
| 172 | + }), |
| 173 | +})) |
| 174 | +``` |
| 175 | + |
| 176 | +### API Client |
| 177 | + |
| 178 | +```typescript |
| 179 | +vi.mock('../utils/api', () => ({ |
| 180 | + default: { |
| 181 | + get: vi.fn(), |
| 182 | + post: vi.fn(), |
| 183 | + patch: vi.fn(), |
| 184 | + delete: vi.fn(), |
| 185 | + }, |
| 186 | +})) |
| 187 | +``` |
| 188 | + |
| 189 | +### CSS Imports |
| 190 | + |
| 191 | +```typescript |
| 192 | +vi.mock('amis/lib/themes/cxd.css', () => ({})) |
| 193 | +vi.mock('amis/lib/helper.css', () => ({})) |
| 194 | +vi.mock('amis/sdk/iconfont.css', () => ({})) |
| 195 | +``` |
| 196 | + |
| 197 | +## Coverage Goals |
| 198 | + |
| 199 | +| Area | Current | Goal | |
| 200 | +|------|---------|------| |
| 201 | +| Statements | TBD | 80% | |
| 202 | +| Branches | TBD | 75% | |
| 203 | +| Functions | TBD | 80% | |
| 204 | +| Lines | TBD | 80% | |
| 205 | + |
| 206 | +## Best Practices |
| 207 | + |
| 208 | +1. **Test Isolation**: Each test should be independent |
| 209 | +2. **Mock External Dependencies**: Mock API calls, AMIS framework |
| 210 | +3. **Test User Behavior**: Focus on what users do, not implementation |
| 211 | +4. **Descriptive Names**: Test names should clearly describe what they test |
| 212 | +5. **Arrange-Act-Assert**: Follow AAA pattern in test structure |
| 213 | + |
| 214 | +## Example Test |
| 215 | + |
| 216 | +```typescript |
| 217 | +describe('Feature', () => { |
| 218 | + it('should do something when condition is met', async () => { |
| 219 | + // Arrange |
| 220 | + const mockData = { foo: 'bar' } |
| 221 | + ;(apiClient.get as any).mockResolvedValueOnce({ data: mockData }) |
| 222 | + |
| 223 | + // Act |
| 224 | + renderWithProviders(<Component />, { initialRoute: '/test' }) |
| 225 | + |
| 226 | + // Assert |
| 227 | + await waitFor(() => { |
| 228 | + expect(screen.getByText('Expected Text')).toBeInTheDocument() |
| 229 | + }) |
| 230 | + }) |
| 231 | +}) |
| 232 | +``` |
| 233 | + |
| 234 | +## Common Issues |
| 235 | + |
| 236 | +### Tests Timing Out |
| 237 | + |
| 238 | +If tests timeout, increase the wait time: |
| 239 | + |
| 240 | +```typescript |
| 241 | +await waitFor( |
| 242 | + () => { |
| 243 | + expect(screen.getByText('Text')).toBeInTheDocument() |
| 244 | + }, |
| 245 | + { timeout: 5000 } // 5 seconds |
| 246 | +) |
| 247 | +``` |
| 248 | + |
| 249 | +### Mock Not Working |
| 250 | + |
| 251 | +Ensure mocks are defined before imports: |
| 252 | + |
| 253 | +```typescript |
| 254 | +// ✅ Good - Mock first |
| 255 | +vi.mock('../utils/api') |
| 256 | +import apiClient from '../utils/api' |
| 257 | + |
| 258 | +// ❌ Bad - Import first |
| 259 | +import apiClient from '../utils/api' |
| 260 | +vi.mock('../utils/api') |
| 261 | +``` |
| 262 | + |
| 263 | +### CSS Import Errors |
| 264 | + |
| 265 | +Always mock CSS imports: |
| 266 | + |
| 267 | +```typescript |
| 268 | +vi.mock('amis/lib/themes/cxd.css', () => ({})) |
| 269 | +``` |
| 270 | + |
| 271 | +## Future Test Additions |
| 272 | + |
| 273 | +- [ ] E2E tests with Playwright |
| 274 | +- [ ] Visual regression tests |
| 275 | +- [ ] Performance tests |
| 276 | +- [ ] Accessibility tests |
| 277 | +- [ ] API integration tests with real server |
| 278 | + |
| 279 | +## Test Statistics |
| 280 | + |
| 281 | +- **Total Test Files**: 5 |
| 282 | +- **Total Tests**: 46 |
| 283 | +- **Passing**: 46 (100%) |
| 284 | +- **Failing**: 0 |
| 285 | +- **Average Duration**: ~2.5s |
| 286 | + |
| 287 | +## Continuous Integration |
| 288 | + |
| 289 | +Tests are run automatically on: |
| 290 | +- Every commit |
| 291 | +- Every pull request |
| 292 | +- Before deployment |
| 293 | + |
| 294 | +All tests must pass before merging to main branch. |
0 commit comments