From 1554ca83ae2b6081342aa51e87afd964c8e3215a Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Thu, 14 Feb 2019 20:03:07 +0200 Subject: [PATCH] [ACA-2193] Upload new content version - unsubscribe upload event (#925) * remove console log * unsubscribe upload event after new version * unit tests --- .../evaluators/navigation.evaluators.ts | 4 - src/app/store/effects/node.effects.spec.ts | 3 +- src/app/store/effects/upload.effects.spec.ts | 172 ++++++++++++++++++ src/app/store/effects/upload.effects.ts | 30 ++- 4 files changed, 193 insertions(+), 16 deletions(-) create mode 100644 src/app/store/effects/upload.effects.spec.ts diff --git a/src/app/extensions/evaluators/navigation.evaluators.ts b/src/app/extensions/evaluators/navigation.evaluators.ts index 5657e7b26..94042059c 100644 --- a/src/app/extensions/evaluators/navigation.evaluators.ts +++ b/src/app/extensions/evaluators/navigation.evaluators.ts @@ -146,10 +146,6 @@ export function isSharedPreview( ...args: RuleParameter[] ): boolean { const { url } = context.navigation; - console.log( - '===== isSharedPreview: ', - url && url.startsWith('/shared/preview/') - ); return url && url.startsWith('/shared/preview/'); } diff --git a/src/app/store/effects/node.effects.spec.ts b/src/app/store/effects/node.effects.spec.ts index f64a65ef0..52b50a275 100644 --- a/src/app/store/effects/node.effects.spec.ts +++ b/src/app/store/effects/node.effects.spec.ts @@ -28,7 +28,6 @@ import { AppTestingModule } from '../../testing/app-testing.module'; import { NodeEffects } from './node.effects'; import { EffectsModule } from '@ngrx/effects'; import { Store } from '@ngrx/store'; -import { AppStore } from '../states/app.state'; import { ContentManagementService } from '../../services/content-management.service'; import { ShareNodeAction, @@ -50,7 +49,7 @@ import { import { SetCurrentFolderAction } from '../actions/app.actions'; describe('NodeEffects', () => { - let store: Store; + let store: Store; // let actions$: Actions; let contentService: ContentManagementService; diff --git a/src/app/store/effects/upload.effects.spec.ts b/src/app/store/effects/upload.effects.spec.ts new file mode 100644 index 000000000..b4a168a07 --- /dev/null +++ b/src/app/store/effects/upload.effects.spec.ts @@ -0,0 +1,172 @@ +/*! + * @license + * Alfresco Example Content Application + * + * Copyright (C) 2005 - 2018 Alfresco Software Limited + * + * This file is part of the Alfresco Example Content Application. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * The Alfresco Example Content Application is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Alfresco Example Content Application is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ + +import { Store } from '@ngrx/store'; +import { TestBed } from '@angular/core/testing'; +import { EffectsModule } from '@ngrx/effects'; +import { UploadEffects } from './upload.effects'; +import { AppTestingModule } from '../../testing/app-testing.module'; +import { NgZone } from '@angular/core'; +import { + UploadService, + FileUploadCompleteEvent, + FileModel +} from '@alfresco/adf-core'; +import { UnlockWriteAction } from '../actions'; + +describe('UploadEffects', () => { + let store: Store; + let uploadService: UploadService; + let effects: UploadEffects; + let zone: NgZone; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [AppTestingModule, EffectsModule.forRoot([UploadEffects])] + }); + + zone = TestBed.get(NgZone); + spyOn(zone, 'run').and.callFake((fn: () => any) => { + return fn(); + }); + + store = TestBed.get(Store); + uploadService = TestBed.get(UploadService); + effects = TestBed.get(UploadEffects); + }); + + it('should work', () => { + expect(store).toBeDefined(); + expect(uploadService).toBeDefined(); + expect(effects).toBeDefined(); + }); + + it('should not upload and unlock file if param not provided', () => { + effects.uploadAndUnlock(null); + expect(zone.run).not.toHaveBeenCalled(); + }); + + it('should upload the file before unlocking', () => { + const file: any = {}; + + spyOn(uploadService, 'addToQueue').and.stub(); + spyOn(uploadService, 'uploadFilesInTheQueue').and.stub(); + + effects.uploadAndUnlock(file); + + expect(uploadService.addToQueue).toHaveBeenCalled(); + expect(uploadService.uploadFilesInTheQueue).toHaveBeenCalled(); + }); + + it('should dispatch the unlock write action for a locked file', () => { + const file: FileModel = new FileModel( + { name: 'file1.png', size: 10 }, + null, + 'file1' + ); + + file.data = { + entry: { + id: 'file1', + properties: { + 'cm:lockType': 'WRITE_LOCK' + } + } + }; + + spyOn(uploadService, 'addToQueue').and.stub(); + spyOn(uploadService, 'uploadFilesInTheQueue').and.stub(); + spyOn(store, 'dispatch').and.stub(); + + effects.uploadAndUnlock(file); + uploadService.fileUploadComplete.next( + new FileUploadCompleteEvent(file, 100, file.data) + ); + + expect(store.dispatch).toHaveBeenCalledWith( + new UnlockWriteAction(file.data) + ); + }); + + it('should dispatch only one unlock action for a locked file', () => { + const file: FileModel = new FileModel( + { name: 'file1.png', size: 10 }, + null, + 'file1' + ); + + file.data = { + entry: { + id: 'file1', + properties: { + 'cm:lockType': 'WRITE_LOCK' + } + } + }; + + spyOn(uploadService, 'addToQueue').and.stub(); + spyOn(uploadService, 'uploadFilesInTheQueue').and.stub(); + spyOn(store, 'dispatch').and.stub(); + + effects.uploadAndUnlock(file); + + const completeEvent = new FileUploadCompleteEvent(file, 100, file.data); + uploadService.fileUploadComplete.next(completeEvent); + uploadService.fileUploadComplete.next(completeEvent); + uploadService.fileUploadComplete.next(completeEvent); + + expect(store.dispatch).toHaveBeenCalledWith( + new UnlockWriteAction(file.data) + ); + + expect(store.dispatch).toHaveBeenCalledTimes(1); + }); + + it('should dispatch no actions if file is not locked', () => { + const file: FileModel = new FileModel( + { name: 'file1.png', size: 10 }, + null, + 'file1' + ); + + file.data = { + entry: { + id: 'file1', + properties: {} + } + }; + + spyOn(uploadService, 'addToQueue').and.stub(); + spyOn(uploadService, 'uploadFilesInTheQueue').and.stub(); + spyOn(store, 'dispatch').and.stub(); + + effects.uploadAndUnlock(file); + uploadService.fileUploadComplete.next( + new FileUploadCompleteEvent(file, 100, file.data) + ); + + expect(store.dispatch).not.toHaveBeenCalled(); + }); +}); diff --git a/src/app/store/effects/upload.effects.ts b/src/app/store/effects/upload.effects.ts index 432b9cfca..1fd084392 100644 --- a/src/app/store/effects/upload.effects.ts +++ b/src/app/store/effects/upload.effects.ts @@ -143,7 +143,7 @@ export class UploadEffects { ); this.fileVersionInput.value = ''; - this.uploadVersion(fileModel); + this.uploadAndUnlock(fileModel); }), catchError(error => { this.fileVersionInput.value = ''; @@ -186,20 +186,30 @@ export class UploadEffects { } } - private uploadVersion(file: FileModel) { + uploadAndUnlock(file: FileModel) { + if (!file) { + return; + } + this.ngZone.run(() => { this.uploadService.addToQueue(file); this.uploadService.uploadFilesInTheQueue(); - this.uploadService.fileUploadComplete.subscribe(completed => { - if ( - file.data.entry.properties && - file.data.entry.properties['cm:lockType'] === 'WRITE_LOCK' && - completed.data.entry.id === file.data.entry.id - ) { - this.store.dispatch(new UnlockWriteAction(completed.data)); + const subscription = this.uploadService.fileUploadComplete.subscribe( + completed => { + if ( + file.data && + file.data.entry && + file.data.entry.properties && + file.data.entry.properties['cm:lockType'] === 'WRITE_LOCK' && + file.data.entry.id === completed.data.entry.id + ) { + this.store.dispatch(new UnlockWriteAction(completed.data)); + } + + subscription.unsubscribe(); } - }); + ); }); } }