@@ -4,7 +4,8 @@ import { setupTest } from 'ember-qunit';
4
4
5
5
import Model , { attr , belongsTo , hasMany } from '@ember-data/model' ;
6
6
import type Store from '@ember-data/store' ;
7
- import { StableRecordIdentifier } from '@ember-data/types/q/identifier' ;
7
+ import { recordIdentifierFor } from '@ember-data/store' ;
8
+ import type { StableRecordIdentifier } from '@ember-data/types/q/identifier' ;
8
9
9
10
class App extends Model {
10
11
@attr declare name : string ;
@@ -394,6 +395,58 @@ module('Integration | Relationships | Rollback', function (hooks) {
394
395
assert . arrayStrictEquals ( changed , [ ] , 'belongsTo has rolled back' ) ;
395
396
assert . strictEqual ( config . app , store . peekRecord ( 'app' , '1' ) as App , 'belongsTo has rolled back' ) ;
396
397
} ) ;
398
+
399
+ test ( 'relationship rollback can be repeated' , function ( assert ) {
400
+ class Message extends Model {
401
+ @attr declare msg : string ;
402
+ }
403
+ class Job extends Model {
404
+ @attr declare name : string ;
405
+ @hasMany ( 'message' , { async : false , inverse : null } ) declare messages : Message [ ] ;
406
+ }
407
+
408
+ this . owner . register ( 'model:job' , Job ) ;
409
+ this . owner . register ( 'model:message' , Message ) ;
410
+ const store = this . owner . lookup ( 'service:store' ) as Store ;
411
+
412
+ const job = store . push ( {
413
+ data : {
414
+ id : '1' ,
415
+ type : 'job' ,
416
+ attributes : {
417
+ name : 'First Job' ,
418
+ } ,
419
+ } ,
420
+ } ) as Job ;
421
+
422
+ const msg1 = store . push ( {
423
+ data : {
424
+ id : '1' ,
425
+ type : 'message' ,
426
+ attributes : {
427
+ msg : 'First Message' ,
428
+ } ,
429
+ } ,
430
+ } ) as Message ;
431
+ assert . strictEqual ( job . messages . length , 0 , 'job has 0 messages' ) ;
432
+ const jobIdentifier = recordIdentifierFor ( job ) ;
433
+
434
+ // add message, assert state, rollback, assert state is clean
435
+ job . messages . push ( msg1 ) ;
436
+ assert . strictEqual ( job . messages . length , 1 , 'job has 1 message' ) ;
437
+
438
+ const rollbackResult = store . cache . rollbackRelationships ( jobIdentifier ) ;
439
+ assert . strictEqual ( rollbackResult . length , 1 , '1 rollbackRelations' ) ;
440
+ assert . strictEqual ( job . messages . length , 0 , 'job has no message' ) ;
441
+
442
+ // repeat the scenario to add a message and rollback
443
+ job . messages . push ( msg1 ) ;
444
+ assert . strictEqual ( job . messages . length , 1 , 'job has 1 message' ) ;
445
+
446
+ const rollbackResult2 = store . cache . rollbackRelationships ( jobIdentifier ) ;
447
+ assert . strictEqual ( rollbackResult2 . length , 1 , '1 rollbackRelations' ) ;
448
+ assert . strictEqual ( job . messages . length , 0 , 'job has no message' ) ;
449
+ } ) ;
397
450
} ) ;
398
451
399
452
module ( '<cache>.changedRelationships' , function ( ) {
0 commit comments