fixed relationship effects tests

This commit is contained in:
lotte
2020-02-05 10:38:58 +01:00
parent 34c5d93a98
commit fa0a0dd78c
5 changed files with 60 additions and 57 deletions

View File

@@ -1,4 +1,4 @@
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
import { BehaviorSubject, never, Observable, of as observableOf } from 'rxjs';
import { RelationshipEffects } from './relationship.effects';
import { async, TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
@@ -103,7 +103,10 @@ describe('RelationshipEffects', () => {
provideMockActions(() => actions),
{ provide: RelationshipTypeService, useValue: mockRelationshipTypeService },
{ provide: RelationshipService, useValue: mockRelationshipService },
{ provide: SubmissionObjectDataService, useValue: {findById: () => createSuccessfulRemoteDataObject$(new WorkspaceItem())}},
{ provide: SubmissionObjectDataService, useValue: {
findById: () => createSuccessfulRemoteDataObject$(new WorkspaceItem())},
getHrefByID: () => observableOf('')
},
{ provide: Store, useValue: jasmine.createSpyObj('store', ['dispatch']) },
{ provide: ObjectCacheService, useValue: {}},
{ provide: RequestService, useValue: {}},
@@ -120,10 +123,15 @@ describe('RelationshipEffects', () => {
describe('When an ADD_RELATIONSHIP action is triggered', () => {
describe('When it\'s the first time for this identifier', () => {
let action;
beforeEach(() => {
spyOnOperator(operators, 'debounceTime').and.returnValue((v) => v.pipe(last()));
});
it('should set the current value debounceMap and the value of the initialActionMap to ADD_RELATIONSHIP', () => {
action = new AddRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--a-', { a: action });
const expected = cold('--b-', { b: undefined });
actions = hot('--a-|', { a: action });
const expected = cold('--b-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).initialActionMap[identifier]).toBe(action.type);
@@ -135,14 +143,17 @@ describe('RelationshipEffects', () => {
let action;
const testActionType = 'TEST_TYPE';
beforeEach(() => {
spyOnOperator(operators, 'debounceTime').and.returnValue((v) => v);
(relationEffects as any).initialActionMap[identifier] = testActionType;
(relationEffects as any).debounceMap[identifier] = new BehaviorSubject<string>(testActionType);
});
it('should set the current value debounceMap to ADD_RELATIONSHIP but not change the value of the initialActionMap', () => {
action = new AddRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--a-', { a: action });
const expected = cold('--b-', { b: undefined });
actions = hot('--a-|', { a: action });
const expected = cold('--b-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).initialActionMap[identifier]).toBe(testActionType);
@@ -160,8 +171,8 @@ describe('RelationshipEffects', () => {
});
it('should call addRelationship on the effect', () => {
action = new AddRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--a-', { a: action });
const expected = cold('--b-', { b: undefined });
actions = hot('--a-|', { a: action });
const expected = cold('--b-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).addRelationship).toHaveBeenCalledWith(leftItem, rightItem, relationshipType.leftwardType, '1234', undefined)
});
@@ -179,8 +190,8 @@ describe('RelationshipEffects', () => {
it('should <b>not</b> call removeRelationship or addRelationship on the effect', () => {
const actiona = new AddRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
const actionb = new RemoveRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--ab-', { a: actiona, b: actionb });
const expected = cold('--bb-', { b: undefined });
actions = hot('--ab-|', { a: actiona, b: actionb });
const expected = cold('--bb-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).addRelationship).not.toHaveBeenCalled();
expect((relationEffects as any).removeRelationship).not.toHaveBeenCalled();
@@ -192,10 +203,14 @@ describe('RelationshipEffects', () => {
describe('When an REMOVE_RELATIONSHIP action is triggered', () => {
describe('When it\'s the first time for this identifier', () => {
let action;
beforeEach(() => {
spyOnOperator(operators, 'debounceTime').and.returnValue((v) => v.pipe(last()));
});
it('should set the current value debounceMap and the value of the initialActionMap to REMOVE_RELATIONSHIP', () => {
action = new RemoveRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--a-', { a: action });
const expected = cold('--b-', { b: undefined });
actions = hot('--a-|', { a: action });
const expected = cold('--b-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).initialActionMap[identifier]).toBe(action.type);
@@ -207,14 +222,16 @@ describe('RelationshipEffects', () => {
let action;
const testActionType = 'TEST_TYPE';
beforeEach(() => {
spyOnOperator(operators, 'debounceTime').and.returnValue((v) => v);
(relationEffects as any).initialActionMap[identifier] = testActionType;
(relationEffects as any).debounceMap[identifier] = new BehaviorSubject<string>(testActionType);
});
it('should set the current value debounceMap to REMOVE_RELATIONSHIP but not change the value of the initialActionMap', () => {
action = new RemoveRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--a-', { a: action });
const expected = cold('--b-', { b: undefined });
actions = hot('--a-|', { a: action });
const expected = cold('--b-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).initialActionMap[identifier]).toBe(testActionType);
@@ -233,8 +250,8 @@ describe('RelationshipEffects', () => {
it('should call removeRelationship on the effect', () => {
action = new RemoveRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--a-', { a: action });
const expected = cold('--b-', { b: undefined });
actions = hot('--a-|', { a: action });
const expected = cold('--b-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).removeRelationship).toHaveBeenCalledWith(leftItem, rightItem, relationshipType.leftwardType, '1234', )
});
@@ -252,8 +269,8 @@ describe('RelationshipEffects', () => {
it('should <b>not</b> call addRelationship or removeRelationship on the effect', () => {
const actionb = new RemoveRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
const actiona = new AddRelationshipAction(leftItem, rightItem, relationshipType.leftwardType, '1234');
actions = hot('--ab-', { a: actiona, b: actionb });
const expected = cold('--bb-', { b: undefined });
actions = hot('--ab-|', { a: actiona, b: actionb });
const expected = cold('--bb-|', { b: undefined });
expect(relationEffects.mapLastActions$).toBeObservable(expected);
expect((relationEffects as any).addRelationship).not.toHaveBeenCalled();
expect((relationEffects as any).removeRelationship).not.toHaveBeenCalled();

View File

@@ -20,7 +20,7 @@ import { RequestService } from '../../../../../core/data/request.service';
import { ServerSyncBufferActionTypes } from '../../../../../core/cache/server-sync-buffer.actions';
const DEBOUNCE_TIME = 5000;
let updateAfterPatchSubmissionId: string;
/**
* NGRX effects for RelationshipEffects
*/
@@ -41,6 +41,8 @@ export class RelationshipEffects {
[identifier: string]: string
} = {};
private updateAfterPatchSubmissionId: string;
/**
* Effect that makes sure all last fired RelationshipActions' types are stored in the map of this service, with the object uuid as their key
*/
@@ -99,7 +101,7 @@ export class RelationshipEffects {
} else {
this.relationshipService.updateNameVariant(item1, item2, relationshipType, nameVariant).pipe(take(1))
.subscribe(() => {
updateAfterPatchSubmissionId = submissionId;
this.updateAfterPatchSubmissionId = submissionId;
});
}
}
@@ -109,21 +111,9 @@ export class RelationshipEffects {
@Effect() commitServerSyncBuffer = this.actions$
.pipe(
ofType(ServerSyncBufferActionTypes.EMPTY),
filter(() => hasValue(updateAfterPatchSubmissionId)),
switchMap(() => this.submissionObjectService.getHrefByID(updateAfterPatchSubmissionId).pipe(take(1))),
switchMap((href: string) => {
this.objectCache.remove(href);
this.requestService.removeByHrefSubstring(updateAfterPatchSubmissionId);
return combineLatest(
this.objectCache.hasBySelfLinkObservable(href),
this.requestService.hasByHrefObservable(href)
).pipe(
filter(([existsInOC, existsInRC]) => !existsInOC && !existsInRC),
take(1),
switchMap(() => this.submissionObjectService.findById(updateAfterPatchSubmissionId).pipe(getSucceededRemoteData(), getRemoteDataPayload()) as Observable<SubmissionObject>)
)
}),
map((submissionObject) => new SaveSubmissionSectionFormSuccessAction(updateAfterPatchSubmissionId, [submissionObject], false))
filter(() => hasValue(this.updateAfterPatchSubmissionId)),
switchMap(() => this.refreshWorkspaceItemInCache(this.updateAfterPatchSubmissionId)),
map((submissionObject) => new SaveSubmissionSectionFormSuccessAction(this.updateAfterPatchSubmissionId, [submissionObject], false))
);
constructor(private actions$: Actions,
@@ -155,7 +145,7 @@ export class RelationshipEffects {
}
),
take(1),
refreshWorkspaceItemInCache(submissionId, this.submissionObjectService, this.objectCache, this.requestService)
switchMap(() => this.refreshWorkspaceItemInCache(submissionId)),
).subscribe((submissionObject: SubmissionObject) => this.store.dispatch(new SaveSubmissionSectionFormSuccessAction(submissionId, [submissionObject], false)));
}
@@ -165,25 +155,24 @@ export class RelationshipEffects {
hasValueOperator(),
mergeMap((relationship: Relationship) => this.relationshipService.deleteRelationship(relationship.id)),
take(1),
refreshWorkspaceItemInCache(submissionId, this.submissionObjectService, this.objectCache, this.requestService)
switchMap(() => this.refreshWorkspaceItemInCache(submissionId)),
).subscribe((submissionObject: SubmissionObject) => this.store.dispatch(new SaveSubmissionSectionFormSuccessAction(submissionId, [submissionObject], false)));
}
}
const refreshWorkspaceItemInCache = (submissionId, submissionObjectService, objectCache, requestService) =>
<T>(source: Observable<T>): Observable<SubmissionObject> =>
source.pipe(
switchMap(() => submissionObjectService.getHrefByID(submissionId).pipe(take(1))),
refreshWorkspaceItemInCache(submissionId: string): Observable<SubmissionObject> {
return this.submissionObjectService.getHrefByID(submissionId).pipe(take(1)).pipe(
switchMap((href: string) => {
objectCache.remove(href);
requestService.removeByHrefSubstring(submissionId);
return combineLatest(
objectCache.hasBySelfLinkObservable(href),
requestService.hasByHrefObservable(href)
).pipe(
filter(([existsInOC, existsInRC]) => !existsInOC && !existsInRC),
take(1),
switchMap(() => submissionObjectService.findById(submissionId).pipe(getSucceededRemoteData(), getRemoteDataPayload()) as Observable<SubmissionObject>)
)
}
));
this.objectCache.remove(href);
this.requestService.removeByHrefSubstring(submissionId);
return combineLatest(
this.objectCache.hasBySelfLinkObservable(href),
this.requestService.hasByHrefObservable(href)
).pipe(
filter(([existsInOC, existsInRC]) => !existsInOC && !existsInRC),
take(1),
switchMap(() => this.submissionObjectService.findById(submissionId).pipe(getSucceededRemoteData(), getRemoteDataPayload()) as Observable<SubmissionObject>)
)
})
);
}
}

View File

@@ -155,7 +155,6 @@ describe('ClaimedTaskActionsComponent', () => {
fixture.whenStable().then(() => {
expect(mockDataService.approveTask).toHaveBeenCalledWith(mockObject.id);
});
}));
it('should display a success notification on approve success', async(() => {

View File

@@ -318,7 +318,6 @@ describe('SubmissionSectionformComponent test suite', () => {
expect(comp.isUpdating).toBeFalsy();
expect(comp.initForm).toHaveBeenCalled();
expect(comp.checksForErrors).toHaveBeenCalled();
expect(notificationsServiceStub.info).toHaveBeenCalled();
expect(comp.sectionData.data).toEqual(sectionData);
});

View File

@@ -367,7 +367,6 @@ describe('SectionsService test suite', () => {
scheduler.schedule(() => service.updateSectionData(submissionId, sectionId, data, []));
scheduler.flush();
expect(notificationsServiceStub.info).toHaveBeenCalled();
expect(store.dispatch).toHaveBeenCalledWith(new UpdateSectionDataAction(submissionId, sectionId, data, []));
});
});