Skip to content

Commit

Permalink
115046: Fixed same type entity relationships with same leftward/right…
Browse files Browse the repository at this point in the history
…ward type displaying twice
  • Loading branch information
alexandrevryghem committed Jun 7, 2024
1 parent 4ccd459 commit 051a297
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,25 +316,44 @@ describe('EditItemRelationshipsService', () => {
describe('relationshipMatchesBothSameTypes', () => {
it('should return true if both left and right type of the relationship type are the same and match the provided itemtype', (done) => {
const relationshipType = Object.assign(new RelationshipType(), {
leftType: createSuccessfulRemoteDataObject$({ id: 'sameType' }),
leftType: createSuccessfulRemoteDataObject$({ id: 'sameType' }),
rightType: createSuccessfulRemoteDataObject$({ id:'sameType' }),
leftwardType: 'isDepartmentOfDivision',
rightwardType: 'isDivisionOfDepartment',
});
const itemType = Object.assign(new ItemType(), { id: 'sameType' } );

const result = service.relationshipMatchesBothSameTypes(relationshipType, itemType);
const result = service.shouldDisplayBothRelationshipSides(relationshipType, itemType);
result.subscribe((resultValue) => {
expect(resultValue).toBeTrue();
done();
});
});
it('should return false if both left and right type of the relationship type are the same and match the provided itemtype but the leftwardType & rightwardType is identical', (done) => {
const relationshipType = Object.assign(new RelationshipType(), {
leftType: createSuccessfulRemoteDataObject$({ id: 'sameType' }),
rightType: createSuccessfulRemoteDataObject$({ id: 'sameType' }),
leftwardType: 'isOrgUnitOfOrgUnit',
rightwardType: 'isOrgUnitOfOrgUnit',
});
const itemType = Object.assign(new ItemType(), { id: 'sameType' });

const result = service.shouldDisplayBothRelationshipSides(relationshipType, itemType);
result.subscribe((resultValue) => {
expect(resultValue).toBeFalse();
done();
});
});
it('should return false if both left and right type of the relationship type are the same and do not match the provided itemtype', (done) => {
const relationshipType = Object.assign(new RelationshipType(), {
leftType: createSuccessfulRemoteDataObject$({ id: 'sameType' }),
rightType: createSuccessfulRemoteDataObject$({ id: 'sameType' }),
leftwardType: 'isDepartmentOfDivision',
rightwardType: 'isDivisionOfDepartment',
});
const itemType = Object.assign(new ItemType(), { id: 'something-else' } );

const result = service.relationshipMatchesBothSameTypes(relationshipType, itemType);
const result = service.shouldDisplayBothRelationshipSides(relationshipType, itemType);
result.subscribe((resultValue) => {
expect(resultValue).toBeFalse();
done();
Expand All @@ -344,10 +363,12 @@ describe('EditItemRelationshipsService', () => {
const relationshipType = Object.assign(new RelationshipType(), {
leftType: createSuccessfulRemoteDataObject$({ id: 'leftType' }),
rightType: createSuccessfulRemoteDataObject$({ id: 'rightType' }),
leftwardType: 'isAuthorOfPublication',
rightwardType: 'isPublicationOfAuthor',
});
const itemType = Object.assign(new ItemType(), { id: 'leftType' } );

const result = service.relationshipMatchesBothSameTypes(relationshipType, itemType);
const result = service.shouldDisplayBothRelationshipSides(relationshipType, itemType);
result.subscribe((resultValue) => {
expect(resultValue).toBeFalse();
done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,16 @@ export class EditItemRelationshipsService {
);
}

relationshipMatchesBothSameTypes(relationshipType: RelationshipType, itemType: ItemType): Observable<boolean> {
/**
* Whether both side of the relationship need to be displayed on the edit relationship page or not.
*
* @param relationshipType The relationship type
* @param itemType The item type
*/
shouldDisplayBothRelationshipSides(relationshipType: RelationshipType, itemType: ItemType): Observable<boolean> {
return this.getRelationshipLeftAndRightType(relationshipType).pipe(
map(([leftType, rightType]: [ItemType, ItemType]) => {
return leftType.id === itemType.id && rightType.id === itemType.id;
return leftType.id === itemType.id && rightType.id === itemType.id && relationshipType.leftwardType !== relationshipType.rightwardType;
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ng-container *ngIf="bothItemsMatchType$ | async">
<ng-container *ngIf="shouldDisplayBothRelationshipSides$ | async">
<ds-edit-relationship-list
[url]="url"
[item]="item"
Expand All @@ -17,7 +17,7 @@
></ds-edit-relationship-list>
</ng-container>

<ng-container *ngIf="(bothItemsMatchType$ | async) === false">
<ng-container *ngIf="(shouldDisplayBothRelationshipSides$ | async) === false">
<ds-edit-relationship-list
[url]="url"
[item]="item"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
waitForAsync,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { cold } from 'jasmine-marbles';
import { of as observableOf } from 'rxjs';

import { Item } from '../../../../core/shared/item.model';
Expand Down Expand Up @@ -42,7 +43,7 @@ describe('EditRelationshipListWrapperComponent', () => {

editItemRelationshipsService = jasmine.createSpyObj('editItemRelationshipsService', {
isProvidedItemTypeLeftType: observableOf(true),
relationshipMatchesBothSameTypes: observableOf(false),
shouldDisplayBothRelationshipSides: observableOf(false),
});


Expand Down Expand Up @@ -82,10 +83,10 @@ describe('EditRelationshipListWrapperComponent', () => {
});
it('should set currentItemIsLeftItem$ and bothItemsMatchType$ based on the provided relationshipType, itemType and item', () => {
expect(editItemRelationshipsService.isProvidedItemTypeLeftType).toHaveBeenCalledWith(relationshipType, leftType, item);
expect(editItemRelationshipsService.relationshipMatchesBothSameTypes).toHaveBeenCalledWith(relationshipType, leftType);
expect(editItemRelationshipsService.shouldDisplayBothRelationshipSides).toHaveBeenCalledWith(relationshipType, leftType);

expect(comp.currentItemIsLeftItem$.getValue()).toEqual(true);
expect(comp.bothItemsMatchType$.getValue()).toEqual(false);
expect(comp.shouldDisplayBothRelationshipSides$).toBeObservable(cold('(a|)', { a: false }));
});
});

Expand All @@ -109,7 +110,7 @@ describe('EditRelationshipListWrapperComponent', () => {

describe('when the current item is both left and right', () => {
it('should render two relationship list sections', () => {
(editItemRelationshipsService.relationshipMatchesBothSameTypes as jasmine.Spy).and.returnValue(observableOf(true));
(editItemRelationshipsService.shouldDisplayBothRelationshipSides as jasmine.Spy).and.returnValue(observableOf(true));
comp.ngOnInit();
fixture.detectChanges();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class EditRelationshipListWrapperComponent implements OnInit, OnDestroy {

isRightItem$ = new BehaviorSubject(false);

bothItemsMatchType$: BehaviorSubject<boolean> = new BehaviorSubject(undefined);
shouldDisplayBothRelationshipSides$: Observable<boolean>;

/**
* Array to track all subscriptions and unsubscribe them onDestroy
Expand All @@ -99,10 +99,7 @@ export class EditRelationshipListWrapperComponent implements OnInit, OnDestroy {
this.currentItemIsLeftItem$.next(nextValue);
}));

this.subs.push(this.editItemRelationshipsService.relationshipMatchesBothSameTypes(this.relationshipType, this.itemType)
.subscribe((nextValue: boolean) => {
this.bothItemsMatchType$.next(nextValue);
}));
this.shouldDisplayBothRelationshipSides$ = this.editItemRelationshipsService.shouldDisplayBothRelationshipSides(this.relationshipType, this.itemType);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ export class EditRelationshipComponent implements OnChanges {
this.leftItem$,
this.rightItem$,
]).pipe(
map((items: Item[]) =>
items.find((item) => item.uuid !== this.editItem.uuid),
),
map(([leftItem, rightItem]: [Item, Item]) => leftItem.uuid === this.editItem.uuid ? rightItem : leftItem),
take(1),
).subscribe((relatedItem) => {
this.relatedItem$.next(relatedItem);
Expand Down

0 comments on commit 051a297

Please sign in to comment.