@@ -3,7 +3,12 @@ import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
33import { provideMockActions } from '@ngrx/effects/testing' ;
44import { Store } from '@ngrx/store' ;
55import { RelationshipEffects } from './relationship.effects' ;
6- import { AddRelationshipAction , RelationshipActionTypes , RemoveRelationshipAction } from './relationship.actions' ;
6+ import {
7+ AddRelationshipAction ,
8+ RelationshipActionTypes ,
9+ RemoveRelationshipAction ,
10+ ReplaceRelationshipAction
11+ } from './relationship.actions' ;
712import { Item } from '../../../../../core/shared/item.model' ;
813import { MetadataValue } from '../../../../../core/shared/metadata.models' ;
914import { RelationshipTypeDataService } from '../../../../../core/data/relationship-type-data.service' ;
@@ -23,6 +28,7 @@ import { SelectableListService } from '../../../../object-list/selectable-list/s
2328import { cold , hot } from 'jasmine-marbles' ;
2429import { DEBOUNCE_TIME_OPERATOR } from '../../../../../core/shared/operators' ;
2530import { last } from 'rxjs/operators' ;
31+ import { ItemDataService } from '../../../../../core/data/item-data.service' ;
2632
2733describe ( 'RelationshipEffects' , ( ) => {
2834 let relationEffects : RelationshipEffects ;
@@ -51,6 +57,7 @@ describe('RelationshipEffects', () => {
5157 let notificationsService ;
5258 let translateService ;
5359 let selectableListService ;
60+ let itemService ;
5461
5562 function init ( ) {
5663 testUUID1 = '20e24c2f-a00a-467c-bdee-c929e79bf08d' ;
@@ -93,8 +100,8 @@ describe('RelationshipEffects', () => {
93100 getRelationshipByItemsAndLabel :
94101 ( ) => observableOf ( relationship ) ,
95102 deleteRelationship : ( ) => observableOf ( new RestResponse ( true , 200 , 'OK' ) ) ,
96- addRelationship : ( ) => observableOf ( new RestResponse ( true , 200 , 'OK' ) )
97-
103+ addRelationship : ( ) => createSuccessfulRemoteDataObject$ ( new Relationship ( ) ) ,
104+ update : ( ) => createSuccessfulRemoteDataObject$ ( new Relationship ( ) ) ,
98105 } ;
99106 mockRelationshipTypeService = {
100107 getRelationshipTypeByLabelAndTypes :
@@ -108,6 +115,9 @@ describe('RelationshipEffects', () => {
108115 findSelectedByCondition : observableOf ( { } ) ,
109116 deselectSingle : { }
110117 } ) ;
118+ itemService = jasmine . createSpyObj ( 'itemService' , {
119+ patch : createSuccessfulRemoteDataObject$ ( new Item ( ) ) ,
120+ } ) ;
111121 }
112122
113123 beforeEach ( waitForAsync ( ( ) => {
@@ -118,6 +128,7 @@ describe('RelationshipEffects', () => {
118128 provideMockActions ( ( ) => actions ) ,
119129 { provide : RelationshipTypeDataService , useValue : mockRelationshipTypeService } ,
120130 { provide : RelationshipDataService , useValue : mockRelationshipService } ,
131+ { provide : ItemDataService , useValue : itemService } ,
121132 {
122133 provide : SubmissionObjectDataService , useValue : {
123134 findById : ( ) => createSuccessfulRemoteDataObject$ ( new WorkspaceItem ( ) )
@@ -140,6 +151,7 @@ describe('RelationshipEffects', () => {
140151 identifier = ( relationEffects as any ) . createIdentifier ( leftItem , rightItem , relationshipType . leftwardType ) ;
141152 spyOn ( ( relationEffects as any ) , 'addRelationship' ) . and . stub ( ) ;
142153 spyOn ( ( relationEffects as any ) , 'removeRelationship' ) . and . stub ( ) ;
154+ spyOn ( ( relationEffects as any ) , 'replaceRelationship' ) . and . stub ( ) ;
143155 } ) ;
144156
145157 describe ( 'mapLastActions$' , ( ) => {
@@ -208,6 +220,73 @@ describe('RelationshipEffects', () => {
208220 } ) ;
209221 } ) ;
210222
223+ describe ( 'When a REPLACE_RELATIONSHIP action is triggered' , ( ) => {
224+ describe ( 'When it\'s the first time for this identifier' , ( ) => {
225+ let action ;
226+
227+ it ( 'should set the current value debounceMap and the value of the initialActionMap to REPLACE_RELATIONSHIP' , ( ) => {
228+ action = new ReplaceRelationshipAction ( leftItem , rightItem , true , 0 , 'dc.subject' , relationshipType . leftwardType , '1234' ) ;
229+ actions = hot ( '--a-|' , { a : action } ) ;
230+ const expected = cold ( '--b-|' , { b : undefined } ) ;
231+ expect ( relationEffects . mapLastActions$ ) . toBeObservable ( expected ) ;
232+
233+ expect ( ( relationEffects as any ) . initialActionMap [ identifier ] ) . toBe ( action . type ) ;
234+ expect ( ( relationEffects as any ) . debounceMap [ identifier ] . value ) . toBe ( action . type ) ;
235+ } ) ;
236+ } ) ;
237+
238+ describe ( 'When it\'s not the first time for this identifier' , ( ) => {
239+ let action ;
240+ const testActionType = 'TEST_TYPE' ;
241+ beforeEach ( ( ) => {
242+ ( relationEffects as any ) . initialActionMap [ identifier ] = testActionType ;
243+ ( relationEffects as any ) . debounceMap [ identifier ] = new BehaviorSubject < string > ( testActionType ) ;
244+ } ) ;
245+
246+ it ( 'should set the current value debounceMap to REPLACE_RELATIONSHIP but not change the value of the initialActionMap' , ( ) => {
247+ action = new ReplaceRelationshipAction ( leftItem , rightItem , true , 0 , 'dc.subject' , relationshipType . leftwardType , '1234' ) ;
248+ actions = hot ( '--a-|' , { a : action } ) ;
249+
250+ const expected = cold ( '--b-|' , { b : undefined } ) ;
251+ expect ( relationEffects . mapLastActions$ ) . toBeObservable ( expected ) ;
252+
253+ expect ( ( relationEffects as any ) . initialActionMap [ identifier ] ) . toBe ( testActionType ) ;
254+ expect ( ( relationEffects as any ) . debounceMap [ identifier ] . value ) . toBe ( action . type ) ;
255+ } ) ;
256+ } ) ;
257+
258+ describe ( 'When the initialActionMap contains a REPLACE_RELATIONSHIP action' , ( ) => {
259+ let action ;
260+ describe ( 'When the last value in the debounceMap is also a REPLACE_RELATIONSHIP action' , ( ) => {
261+ beforeEach ( ( ) => {
262+ spyOn ( ( relationEffects as any ) . relationshipService , 'update' ) . and . callThrough ( ) ;
263+ ( ( relationEffects as any ) . debounceTime as jasmine . Spy ) . and . returnValue ( ( v ) => v ) ;
264+ ( relationEffects as any ) . initialActionMap [ identifier ] = RelationshipActionTypes . REPLACE_RELATIONSHIP ;
265+ } ) ;
266+
267+ it ( 'should call replaceRelationship on the effect' , ( ) => {
268+ action = new ReplaceRelationshipAction ( leftItem , rightItem , true , 0 , 'dc.subject' , relationshipType . leftwardType , '1234' ) ;
269+ actions = hot ( '--a-|' , { a : action } ) ;
270+ const expected = cold ( '--b-|' , { b : undefined } ) ;
271+ expect ( relationEffects . mapLastActions$ ) . toBeObservable ( expected ) ;
272+ expect ( ( relationEffects as any ) . replaceRelationship ) . toHaveBeenCalledWith ( leftItem , rightItem , true , 0 , 'dc.subject' , relationshipType . leftwardType , '1234' , undefined ) ;
273+ } ) ;
274+ } ) ;
275+
276+ describe ( 'When the last value in the debounceMap is instead a REMOVE_RELATIONSHIP action' , ( ) => {
277+ it ( 'should <b>not</b> call removeRelationship or replaceRelationship on the effect' , ( ) => {
278+ const actiona = new ReplaceRelationshipAction ( leftItem , rightItem , true , 0 , 'dc.subject' , relationshipType . leftwardType , '1234' ) ;
279+ const actionb = new RemoveRelationshipAction ( leftItem , rightItem , relationshipType . leftwardType , '1234' ) ;
280+ actions = hot ( '--ab-|' , { a : actiona , b : actionb } ) ;
281+ const expected = cold ( '--bb-|' , { b : undefined } ) ;
282+ expect ( relationEffects . mapLastActions$ ) . toBeObservable ( expected ) ;
283+ expect ( ( relationEffects as any ) . replaceRelationship ) . not . toHaveBeenCalled ( ) ;
284+ expect ( ( relationEffects as any ) . removeRelationship ) . not . toHaveBeenCalled ( ) ;
285+ } ) ;
286+ } ) ;
287+ } ) ;
288+ } ) ;
289+
211290 describe ( 'When an REMOVE_RELATIONSHIP action is triggered' , ( ) => {
212291 describe ( 'When it\'s the first time for this identifier' , ( ) => {
213292 let action ;
0 commit comments