-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathObjectForm.msw.test.tsx
More file actions
130 lines (111 loc) · 4.04 KB
/
Copy pathObjectForm.msw.test.tsx
File metadata and controls
130 lines (111 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { describe, it, expect, vi, beforeAll, afterAll, afterEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { ObjectForm } from './ObjectForm';
import { ObjectStackAdapter } from '@object-ui/data-objectstack';
import { setupServer } from 'msw/node';
import { http, HttpResponse } from 'msw';
import { registerAllFields } from '@object-ui/fields';
import React from 'react';
// @ts-expect-error - Import from examples
import { ContactObject } from '../../../examples/crm/src/objects/contact.object';
// Register widget renderers
registerAllFields();
const BASE_URL = process.env.OBJECTSTACK_API_URL || 'http://localhost';
// --- Mock Data ---
const mockSchema = ContactObject;
const mockRecord = {
_id: '1',
name: 'Alice Johnson',
email: 'alice@example.com',
status: 'Active'
};
// --- MSW Setup ---
const handlers = [
// OPTIONS handler for CORS preflight
http.options(`${BASE_URL}/*`, () => {
return new HttpResponse(null, {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,PUT,DELETE,CONNECT,OPTIONS,TRACE,PATCH',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
});
}),
// Health check / Connection check (ObjectStackClient often pings root or /api/v1)
http.get(`${BASE_URL}/api/v1`, () => {
return HttpResponse.json({ status: 'ok', version: '1.0.0' });
}),
// Mock Schema Fetch: GET /api/v1/meta/object/:name
http.get(`${BASE_URL}/api/v1/meta/object/:name`, ({ params }) => {
const { name } = params;
if (name === 'contact') {
return HttpResponse.json(mockSchema);
}
return new HttpResponse(null, { status: 404 });
}),
// Mock Record Fetch: GET /api/v1/data/:object/:id
http.get(`${BASE_URL}/api/v1/data/:object/:id`, ({ params }) => {
const { object, id } = params;
if (object === 'contact' && id === '1') {
return HttpResponse.json(mockRecord);
}
return new HttpResponse(null, { status: 404 });
})
];
const server = setupServer(...handlers);
// --- Test Suite ---
describe('ObjectForm with ObjectStack/MSW', () => {
// Only start MSW if we are NOT using a real server
if (!process.env.OBJECTSTACK_API_URL) {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
}
// Create real adapter instance pointing to MSW or Real Server
const dataSource = new ObjectStackAdapter({
baseUrl: BASE_URL,
// Add custom fetch for environment that might need it, or rely on global fetch
// fetch: global.fetch
});
it('loads schema and renders form fields', async () => {
render(
<ObjectForm
schema={{
type: 'object-form',
objectName: 'contact', // Triggers schema fetch
mode: 'create'
}}
dataSource={dataSource} // Logic moves from mock fn to real adapter + MSW
/>
);
// Verify fields appear (async as schema loads via HTTP)
await waitFor(() => {
// Changed from 'Full Name' to 'Name' based on CRM example schema
expect(screen.getByText('Name')).toBeInTheDocument();
}, { timeout: 2000 }); // Give slight buffer for network mock
expect(screen.getByText('Email')).toBeInTheDocument();
expect(screen.getByText('Status')).toBeInTheDocument();
});
it('loads record data in edit mode', async () => {
render(
<ObjectForm
schema={{
type: 'object-form',
objectName: 'contact',
mode: 'edit',
recordId: '1'
}}
dataSource={dataSource}
/>
);
// Initial load of schema logic + data fetch
await waitFor(() => {
// Changed from 'Full Name' to 'Name'
expect(screen.getByRole('textbox', { name: /Name/i })).toHaveValue('Alice Johnson');
}, { timeout: 2000 }); // Give slight buffer for network mock
// Changed from 'Email Address' to 'Email'
expect(screen.getByRole('textbox', { name: /Email/i })).toHaveValue('alice@example.com');
});
});