@@ -16,6 +16,9 @@ import { startMockServer, stopMockServer, getDriver } from '../mocks/server';
1616describe ( 'ObjectForm with MSW Integration' , ( ) => {
1717 let client : ObjectStackClient ;
1818 let dataSource : ObjectStackDataSource ;
19+ let contactId : string ;
20+ let initialContactIds : string [ ] = [ ] ;
21+ let originalAlice : any = null ;
1922
2023 beforeAll ( async ( ) => {
2124 // Start MSW mock server
@@ -26,6 +29,24 @@ describe('ObjectForm with MSW Integration', () => {
2629 await client . connect ( ) ;
2730
2831 dataSource = new ObjectStackDataSource ( client ) ;
32+
33+ // Get a valid contact ID for edit tests
34+ const driver = getDriver ( ) ;
35+ if ( driver ) {
36+ const contacts = await driver . find ( 'contact' , { object : 'contact' } ) ;
37+ if ( contacts ) {
38+ initialContactIds = contacts . map ( ( c : any ) => c . id ) ;
39+ // Find Alice Johnson
40+ const alice = contacts . find ( ( c : any ) => c . name === 'Alice Johnson' ) ;
41+ if ( alice ) {
42+ originalAlice = { ...alice } ;
43+ contactId = alice . id ;
44+ } else {
45+ contactId = contacts [ 0 ] . id ;
46+ originalAlice = { ...contacts [ 0 ] } ;
47+ }
48+ }
49+ }
2950 } ) ;
3051
3152 afterAll ( ( ) => {
@@ -36,9 +57,14 @@ describe('ObjectForm with MSW Integration', () => {
3657 // Clean up created contacts after each test
3758 const driver = getDriver ( ) ;
3859 if ( driver ) {
60+ // Restore Alice if she was modified
61+ if ( originalAlice ) {
62+ await driver . update ( 'contact' , originalAlice . id , originalAlice ) ;
63+ }
64+
3965 const contacts = await driver . find ( 'contact' , { object : 'contact' } ) ;
4066 for ( const contact of contacts ) {
41- if ( contact . id && ! [ '1' , '2' , '3' ] . includes ( contact . id ) ) {
67+ if ( contact . id && ! initialContactIds . includes ( contact . id ) ) {
4268 await driver . delete ( 'contact' , contact . id ) ;
4369 }
4470 }
@@ -55,7 +81,7 @@ describe('ObjectForm with MSW Integration', () => {
5581 type : 'object-form' ,
5682 objectName : 'contact' ,
5783 mode : 'create' ,
58- fields : [ 'name' , 'email' , 'phone' , 'company' , 'position ' , 'priority' , 'is_active' , 'notes' ] ,
84+ fields : [ 'name' , 'email' , 'phone' , 'company' , 'title ' , 'priority' , 'is_active' , 'notes' ] ,
5985 onSuccess,
6086 } }
6187 dataSource = { dataSource }
@@ -70,7 +96,7 @@ describe('ObjectForm with MSW Integration', () => {
7096 expect ( screen . getByLabelText ( / E m a i l / i) ) . toBeInTheDocument ( ) ;
7197 expect ( screen . getByLabelText ( / ^ P h o n e / i) ) . toBeInTheDocument ( ) ;
7298 expect ( screen . getByLabelText ( / C o m p a n y / i) ) . toBeInTheDocument ( ) ;
73- expect ( screen . getByLabelText ( / P o s i t i o n / i) ) . toBeInTheDocument ( ) ;
99+ expect ( screen . getByLabelText ( / T i t l e / i) ) . toBeInTheDocument ( ) ;
74100 expect ( screen . getByLabelText ( / P r i o r i t y / i) ) . toBeInTheDocument ( ) ;
75101 expect ( screen . getByLabelText ( / A c t i v e / i) ) . toBeInTheDocument ( ) ;
76102 expect ( screen . getByLabelText ( / N o t e s / i) ) . toBeInTheDocument ( ) ;
@@ -198,7 +224,7 @@ describe('ObjectForm with MSW Integration', () => {
198224 type : 'object-form' ,
199225 objectName : 'contact' ,
200226 mode : 'edit' ,
201- recordId : '1' ,
227+ recordId : contactId ,
202228 fields : [ 'name' , 'email' , 'company' ] ,
203229 } }
204230 dataSource = { dataSource }
@@ -208,14 +234,14 @@ describe('ObjectForm with MSW Integration', () => {
208234 // Wait for data to load
209235 await waitFor ( ( ) => {
210236 const nameInput = screen . getByLabelText ( / ^ N a m e / i) as HTMLInputElement ;
211- expect ( nameInput . value ) . toBe ( 'John Doe ' ) ;
237+ expect ( nameInput . value ) . toBe ( 'Alice Johnson ' ) ;
212238 } ) ;
213239
214240 const emailInput = screen . getByLabelText ( / E m a i l / i) as HTMLInputElement ;
215- expect ( emailInput . value ) . toBe ( 'john.doe @example.com' ) ;
241+ expect ( emailInput . value ) . toBe ( 'alice @example.com' ) ;
216242
217243 const companyInput = screen . getByLabelText ( / C o m p a n y / i) as HTMLInputElement ;
218- expect ( companyInput . value ) . toBe ( 'Acme Corp ' ) ;
244+ expect ( companyInput . value ) . toBe ( 'TechCorp ' ) ;
219245 } ) ;
220246
221247 it ( 'should update contact successfully' , async ( ) => {
@@ -228,7 +254,7 @@ describe('ObjectForm with MSW Integration', () => {
228254 type : 'object-form' ,
229255 objectName : 'contact' ,
230256 mode : 'edit' ,
231- recordId : '1' ,
257+ recordId : contactId ,
232258 fields : [ 'name' , 'email' , 'company' ] ,
233259 onSuccess,
234260 } }
@@ -239,13 +265,13 @@ describe('ObjectForm with MSW Integration', () => {
239265 // Wait for data to load
240266 await waitFor ( ( ) => {
241267 const nameInput = screen . getByLabelText ( / ^ N a m e / i) as HTMLInputElement ;
242- expect ( nameInput . value ) . toBe ( 'John Doe ' ) ;
268+ expect ( nameInput . value ) . toBe ( 'Alice Johnson ' ) ;
243269 } ) ;
244270
245271 // Update the name
246272 const nameInput = screen . getByLabelText ( / ^ N a m e / i) ;
247273 await user . clear ( nameInput ) ;
248- await user . type ( nameInput , 'John Doe Updated' ) ;
274+ await user . type ( nameInput , 'Alice Johnson Updated' ) ;
249275
250276 // Submit
251277 await user . click ( screen . getByRole ( 'button' , { name : / u p d a t e / i } ) ) ;
@@ -255,8 +281,8 @@ describe('ObjectForm with MSW Integration', () => {
255281 } ) ;
256282
257283 const updatedContact = onSuccess . mock . calls [ 0 ] [ 0 ] ;
258- expect ( updatedContact . name ) . toBe ( 'John Doe Updated' ) ;
259- expect ( updatedContact . id ) . toBe ( '1' ) ;
284+ expect ( updatedContact . name ) . toBe ( 'Alice Johnson Updated' ) ;
285+ expect ( updatedContact . id ) . toBe ( contactId ) ;
260286 } ) ;
261287
262288 it ( 'should handle update errors gracefully' , async ( ) => {
@@ -291,7 +317,7 @@ describe('ObjectForm with MSW Integration', () => {
291317 type : 'object-form' ,
292318 objectName : 'contact' ,
293319 mode : 'view' ,
294- recordId : '1' ,
320+ recordId : contactId ,
295321 fields : [ 'name' , 'email' , 'company' ] ,
296322 } }
297323 dataSource = { dataSource }
@@ -300,7 +326,7 @@ describe('ObjectForm with MSW Integration', () => {
300326
301327 await waitFor ( ( ) => {
302328 const nameInput = screen . getByLabelText ( / ^ N a m e / i) as HTMLInputElement ;
303- expect ( nameInput . value ) . toBe ( 'John Doe ' ) ;
329+ expect ( nameInput . value ) . toBe ( 'Alice Johnson ' ) ;
304330 expect ( nameInput . disabled ) . toBe ( true ) ;
305331 } ) ;
306332
@@ -328,7 +354,7 @@ describe('ObjectForm with MSW Integration', () => {
328354 } ) ;
329355
330356 const checkbox = screen . getByLabelText ( / A c t i v e / i) as HTMLInputElement ;
331- expect ( checkbox . type ) . toBe ( 'checkbox ') ;
357+ expect ( checkbox ) . toHaveAttribute ( 'role' , 'switch ') ;
332358 } ) ;
333359
334360 it ( 'should render number input for number fields' , async ( ) => {
@@ -391,7 +417,7 @@ describe('ObjectForm with MSW Integration', () => {
391417 } ) ;
392418
393419 const phoneInput = screen . getByLabelText ( / ^ P h o n e / i) as HTMLInputElement ;
394- expect ( phoneInput . type ) . toBe ( 'tel ' ) ;
420+ expect ( phoneInput . type ) . toBe ( 'text ' ) ;
395421 } ) ;
396422 } ) ;
397423
0 commit comments