Skip to content

Commit 5cda2b2

Browse files
Copilothotlong
andcommitted
Add ObjectForm unit tests and update documentation
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 3756a89 commit 5cda2b2

2 files changed

Lines changed: 365 additions & 53 deletions

File tree

examples/msw-object-form/README.md

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ graph TD
2323
- **MSW Integration**: Complete ObjectStack runtime running in the browser via Service Worker
2424
- **Full CRUD Operations**: Create, Read, Update, Delete contacts with validation
2525
- **Real Logic**: Schema validation, defaults, and field types enforced by ObjectStack Kernel
26-
- **Comprehensive Tests**: Test suite covering all form operations and field types
26+
- **Testing Support**: MSW server setup for Node.js test environment
2727
- **Zero Backend**: Develop and test frontend forms before backend exists
2828

2929
## 📦 What's Inside
@@ -33,18 +33,15 @@ graph TD
3333
- **`src/App.tsx`** - Main application component with ObjectForm integration
3434
- **`src/components/ContactList.tsx`** - Contact list component with edit/delete
3535
- **`src/dataSource.ts`** - ObjectStack DataSource adapter for ObjectForm
36-
- **`src/mocks/browser.ts`** - MSW setup with ObjectStack Kernel
36+
- **`src/mocks/browser.ts`** - MSW setup with ObjectStack Kernel (for browser)
37+
- **`src/mocks/server.ts`** - MSW setup for Node.js testing environment
3738
- **`objectstack.config.ts`** - Contact object schema definition
3839

3940
### Test Files
4041

41-
- **`src/__tests__/ObjectForm.test.tsx`** - Comprehensive test suite covering:
42-
- Create mode with field validation
43-
- Edit mode with data loading and updates
44-
- View mode (read-only)
45-
- Different field types (text, email, phone, checkbox, number, textarea)
46-
- Form callbacks (onSuccess, onCancel, onError)
47-
- Data persistence in MSW memory
42+
- **`src/__tests__/MSWServer.test.tsx`** - ✅ **Working Tests** - MSW server integration tests
43+
- **`src/__tests__/ObjectFormUnit.test.tsx`** - Unit tests with mock DataSource (partial)
44+
- **`src/__tests__/ObjectForm.test.tsx`** - Integration tests (requires HTTP interception setup)
4845

4946
## 🚀 Getting Started
5047

@@ -74,9 +71,12 @@ Open [http://localhost:5173](http://localhost:5173) to view the app. You can:
7471
### Testing
7572

7673
```bash
77-
# Run tests
74+
# Run all tests
7875
pnpm test
7976

77+
# Run specific test file
78+
pnpm test MSWServer.test.tsx
79+
8080
# Run tests in watch mode
8181
pnpm test:watch
8282

@@ -122,44 +122,25 @@ const dataSource = new ObjectStackDataSource(client);
122122
/>
123123
```
124124

125-
### Testing with MSW
125+
### Testing with MSW Server
126126

127127
```tsx
128-
import { startMockServer } from './mocks/browser';
129-
import { ObjectStackClient } from '@objectstack/client';
128+
import { startMockServer, stopMockServer, getDriver } from './mocks/server';
130129

131-
// Start MSW before tests
130+
// Start MSW server for tests
132131
beforeAll(async () => {
133132
await startMockServer();
134-
135-
client = new ObjectStackClient({ baseUrl: '' });
136-
await client.connect();
137-
138-
dataSource = new ObjectStackDataSource(client);
139133
});
140134

141-
// Test form creation
142-
it('should create a contact', async () => {
143-
render(
144-
<ObjectForm
145-
schema={{
146-
type: 'object-form',
147-
objectName: 'contact',
148-
mode: 'create',
149-
fields: ['name', 'email'],
150-
onSuccess,
151-
}}
152-
dataSource={dataSource}
153-
/>
154-
);
155-
156-
await user.type(screen.getByLabelText(/name/i), 'Test User');
157-
await user.type(screen.getByLabelText(/email/i), 'test@example.com');
158-
await user.click(screen.getByRole('button', { name: /create/i }));
159-
160-
await waitFor(() => {
161-
expect(onSuccess).toHaveBeenCalled();
162-
});
135+
afterAll(() => {
136+
stopMockServer();
137+
});
138+
139+
// Test with direct driver access
140+
it('should initialize with data', async () => {
141+
const driver = getDriver();
142+
const contacts = await driver!.find('contact', {});
143+
expect(contacts).toHaveLength(3);
163144
});
164145
```
165146

@@ -214,18 +195,30 @@ Implement custom validation in the ObjectForm schema:
214195

215196
## 🧪 Test Coverage
216197

217-
The test suite covers:
218-
219-
✅ Create mode with all field types
220-
✅ Form validation (required fields, field types)
221-
✅ Default values from schema
222-
✅ Edit mode with data loading
223-
✅ Update operations
224-
✅ View mode (read-only)
225-
✅ Error handling
226-
✅ Form callbacks (onSuccess, onCancel, onError)
227-
✅ Data persistence in MSW memory
228-
✅ Various field types (text, email, phone, number, checkbox, textarea)
198+
### ✅ Working Tests
199+
200+
**MSW Server Integration** (`MSWServer.test.tsx`):
201+
- ✅ MSW server initialization with data
202+
- ✅ Direct driver CRUD operations
203+
- ✅ Data persistence
204+
205+
### 🔨 In Progress
206+
207+
**ObjectForm Unit Tests** (`ObjectFormUnit.test.tsx`):
208+
- ✅ Form rendering with different field types
209+
- ✅ Field type detection
210+
- ⚠️ Form submission (needs adjustment for react-hook-form)
211+
- ⚠️ Callbacks (requires proper event handling)
212+
213+
**ObjectForm Integration** (`ObjectForm.test.tsx`):
214+
- ⚠️ Requires HTTP interception setup in test environment
215+
216+
## 📝 Notes
217+
218+
- The app works perfectly in the browser with MSW worker
219+
- MSW server setup works great for Node.js test environment
220+
- Direct driver access is tested and working
221+
- HTTP interception in tests requires additional configuration with happy-dom
229222

230223
## 📄 License
231224

0 commit comments

Comments
 (0)