From fa0a0dd78c791a28072b5612161d795bbbc3c88c Mon Sep 17 00:00:00 2001 From: lotte Date: Wed, 5 Feb 2020 10:38:58 +0100 Subject: [PATCH] fixed relationship effects tests --- .../relationship.effects.spec.ts | 53 ++++++++++------ .../relationship.effects.ts | 61 ++++++++----------- .../claimed-task-actions.component.spec.ts | 1 - .../form/section-form.component.spec.ts | 1 - .../sections/sections.service.spec.ts | 1 - 5 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.spec.ts index e3ad4b3656..abf1d01150 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.spec.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.spec.ts @@ -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(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 not 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(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 not 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(); diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.ts index 8e6fdeb5ab..80e5eb8831 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/relation-lookup-modal/relationship.effects.ts @@ -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) - ) - }), - 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) => - (source: Observable): Observable => - source.pipe( - switchMap(() => submissionObjectService.getHrefByID(submissionId).pipe(take(1))), + refreshWorkspaceItemInCache(submissionId: string): Observable { + 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) - ) - } - )); + 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) + ) + }) + ); + } +} diff --git a/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.spec.ts b/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.spec.ts index 71991bdf25..80030112d7 100644 --- a/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.spec.ts +++ b/src/app/shared/mydspace-actions/claimed-task/claimed-task-actions.component.spec.ts @@ -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(() => { diff --git a/src/app/submission/sections/form/section-form.component.spec.ts b/src/app/submission/sections/form/section-form.component.spec.ts index be13c14941..ed35add49d 100644 --- a/src/app/submission/sections/form/section-form.component.spec.ts +++ b/src/app/submission/sections/form/section-form.component.spec.ts @@ -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); }); diff --git a/src/app/submission/sections/sections.service.spec.ts b/src/app/submission/sections/sections.service.spec.ts index 4a1cea5972..3d63747f01 100644 --- a/src/app/submission/sections/sections.service.spec.ts +++ b/src/app/submission/sections/sections.service.spec.ts @@ -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, [])); }); });