@@ -405,4 +405,76 @@ describe('Models - Temp Ids', function() {
405
405
assert ( ! thing . hasOwnProperty ( '__isTemp' ) , '__isTemp was removed from thing' )
406
406
assert ( ! clone . hasOwnProperty ( '__isTemp' ) , '__isTemp was removed from clone' )
407
407
} )
408
+
409
+ it ( 'Clone gets _id after save (create only called once)' , async function ( ) {
410
+ // Test ensures subsequent calls to clone.save() do not call create
411
+ const { makeServicePlugin, BaseModel } = feathersVuex ( feathersClient , {
412
+ idField : '_id' ,
413
+ serverAlias : 'temp-ids'
414
+ } )
415
+ class Thing extends BaseModel {
416
+ public static modelName = 'Thing'
417
+ public constructor ( data ?, options ?) {
418
+ super ( data , options )
419
+ }
420
+ }
421
+ const store = new Vuex . Store < RootState > ( {
422
+ plugins : [
423
+ makeServicePlugin ( {
424
+ Model : Thing ,
425
+ service : feathersClient . service ( 'things' )
426
+ } )
427
+ ]
428
+ } )
429
+
430
+ // Manually set the result in a hook to simulate the server request.
431
+ let createCalled = false
432
+ feathersClient . service ( 'things' ) . hooks ( {
433
+ before : {
434
+ create : [
435
+ context => {
436
+ assert ( createCalled === false , 'Create is only called once' )
437
+ createCalled = true
438
+ context . result = { _id : 42 , ...context . data }
439
+ return context
440
+ }
441
+ ] ,
442
+ patch : [
443
+ context => {
444
+ assert ( context . data . __isClone , 'Patch called on clone' )
445
+ assert ( context . id === 42 , 'context has correct ID' )
446
+ assert ( context . data . _id === 42 , 'patch called with correct _id' )
447
+ assert (
448
+ context . data . description === 'Thing 3' ,
449
+ 'patch called with correct description'
450
+ )
451
+ context . result = { ...context . data }
452
+ return context
453
+ }
454
+ ]
455
+ }
456
+ } )
457
+
458
+ // Create instance and clone
459
+ const thing = new Thing ( { description : 'Thing 1' } )
460
+ const clone = thing . clone ( )
461
+ assert ( thing . __id === clone . __id , "clone has thing's tempId" )
462
+ assert ( clone . description === 'Thing 1' , "clone got thing's description" )
463
+ assert ( ! thing . hasOwnProperty ( '_id' ) , 'thing has no _id' )
464
+ assert ( ! clone . hasOwnProperty ( '_id' ) , 'clone has no _id' )
465
+
466
+ // Modify clone and save
467
+ clone . description = 'Thing 2'
468
+ const response = await clone . save ( )
469
+ assert ( response === thing , 'response from clone.save() is thing' )
470
+ assert ( thing . _id === 42 , 'thing got _id' )
471
+ assert ( thing . description === 'Thing 2' , "thing got clone's changes" )
472
+ assert ( clone . _id === response . _id , 'clone got _id' )
473
+
474
+ // Modify clone again and save again
475
+ clone . description = 'Thing 3'
476
+ const response2 = await clone . save ( )
477
+ assert ( response2 === thing , 'response2 is still thing' )
478
+ assert ( thing . description === 'Thing 3' , "thing got clone's new changes" )
479
+ } )
408
480
} )
0 commit comments