mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-05-12 17:04:46 +00:00
[ACA-2643] Upload new version - refactoring (#1138)
* refactor upload version effect * subscriber noop fn * move logic to subscribe * tests
This commit is contained in:
parent
9b74063032
commit
9e0e50d57d
@ -34,13 +34,34 @@ import {
|
||||
FileUploadCompleteEvent,
|
||||
FileModel
|
||||
} from '@alfresco/adf-core';
|
||||
import { UnlockWriteAction } from '@alfresco/aca-shared/store';
|
||||
import {
|
||||
UnlockWriteAction,
|
||||
SnackbarErrorAction
|
||||
} from '@alfresco/aca-shared/store';
|
||||
import { ContentManagementService } from '../../services/content-management.service';
|
||||
import { of, throwError } from 'rxjs';
|
||||
|
||||
function createFileList(fileName, type = 'text/plain') {
|
||||
const data = new Blob([''], { type });
|
||||
const arrayOfBlob = new Array<Blob>();
|
||||
arrayOfBlob.push(data);
|
||||
const file = new File(arrayOfBlob, fileName);
|
||||
const files = [file];
|
||||
|
||||
const reducer = (dataTransfer, currentFile) => {
|
||||
dataTransfer.items.add(currentFile);
|
||||
return dataTransfer;
|
||||
};
|
||||
return files.reduce(reducer, new DataTransfer()).files;
|
||||
}
|
||||
|
||||
describe('UploadEffects', () => {
|
||||
let store: Store<any>;
|
||||
let uploadService: UploadService;
|
||||
let effects: UploadEffects;
|
||||
let zone: NgZone;
|
||||
let contentManagementService: ContentManagementService;
|
||||
let uploadVersionInput: HTMLInputElement;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
@ -52,17 +73,21 @@ describe('UploadEffects', () => {
|
||||
return fn();
|
||||
});
|
||||
|
||||
contentManagementService = TestBed.get(ContentManagementService);
|
||||
store = TestBed.get(Store);
|
||||
uploadService = TestBed.get(UploadService);
|
||||
effects = TestBed.get(UploadEffects);
|
||||
});
|
||||
|
||||
it('should work', () => {
|
||||
expect(store).toBeDefined();
|
||||
expect(uploadService).toBeDefined();
|
||||
expect(effects).toBeDefined();
|
||||
beforeEach(() => {
|
||||
uploadVersionInput = document.querySelector('#app-upload-file-version');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
uploadVersionInput.remove();
|
||||
});
|
||||
|
||||
describe('uploadAndUnlock()', () => {
|
||||
it('should not upload and unlock file if param not provided', () => {
|
||||
effects.uploadAndUnlock(null);
|
||||
expect(zone.run).not.toHaveBeenCalled();
|
||||
@ -169,4 +194,44 @@ describe('UploadEffects', () => {
|
||||
|
||||
expect(store.dispatch).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('upload file version', () => {
|
||||
beforeEach(() => {
|
||||
const dialog = { afterClosed: () => of({}) };
|
||||
spyOn(contentManagementService, 'versionUploadDialog').and.returnValue(
|
||||
dialog
|
||||
);
|
||||
spyOn(effects, 'uploadAndUnlock').and.stub();
|
||||
});
|
||||
|
||||
it('should upload file', () => {
|
||||
spyOn(contentManagementService, 'getNodeInfo').and.returnValue(
|
||||
of({
|
||||
entry: {
|
||||
id: 'file1',
|
||||
properties: {}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
uploadVersionInput.files = createFileList('bogus.txt');
|
||||
uploadVersionInput.dispatchEvent(new CustomEvent('change'));
|
||||
expect(effects.uploadAndUnlock).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should raise error when getNodeInfo fails', () => {
|
||||
spyOn(store, 'dispatch').and.stub();
|
||||
spyOn(contentManagementService, 'getNodeInfo').and.returnValue(
|
||||
throwError('error')
|
||||
);
|
||||
|
||||
uploadVersionInput.files = createFileList('bogus.txt');
|
||||
uploadVersionInput.dispatchEvent(new CustomEvent('change'));
|
||||
expect(store.dispatch).toHaveBeenCalledWith(
|
||||
new SnackbarErrorAction('VERSION.ERROR.GENERIC')
|
||||
);
|
||||
expect(effects.uploadAndUnlock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -37,17 +37,8 @@ import { FileModel, FileUtils, UploadService } from '@alfresco/adf-core';
|
||||
import { Injectable, NgZone, RendererFactory2 } from '@angular/core';
|
||||
import { Actions, Effect, ofType } from '@ngrx/effects';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { forkJoin, fromEvent, of } from 'rxjs';
|
||||
import {
|
||||
catchError,
|
||||
distinctUntilChanged,
|
||||
filter,
|
||||
flatMap,
|
||||
map,
|
||||
switchMap,
|
||||
take,
|
||||
tap
|
||||
} from 'rxjs/operators';
|
||||
import { forkJoin, of } from 'rxjs';
|
||||
import { tap, filter, catchError, flatMap, map, take } from 'rxjs/operators';
|
||||
import { ContentManagementService } from '../../services/content-management.service';
|
||||
|
||||
@Injectable()
|
||||
@ -78,7 +69,9 @@ export class UploadEffects {
|
||||
this.fileVersionInput.id = 'app-upload-file-version';
|
||||
this.fileVersionInput.type = 'file';
|
||||
this.fileVersionInput.style.display = 'none';
|
||||
this.fileVersionInput.addEventListener('change', event => event);
|
||||
this.fileVersionInput.addEventListener('change', () =>
|
||||
this.uploadVersion()
|
||||
);
|
||||
renderer.appendChild(document.body, this.fileVersionInput);
|
||||
|
||||
this.folderInput = renderer.createElement('input') as HTMLInputElement;
|
||||
@ -110,19 +103,38 @@ export class UploadEffects {
|
||||
@Effect({ dispatch: false })
|
||||
uploadVersion$ = this.actions$.pipe(
|
||||
ofType<UploadFileVersionAction>(UploadActionTypes.UploadFileVersion),
|
||||
switchMap(() => {
|
||||
map(() => {
|
||||
this.fileVersionInput.click();
|
||||
return fromEvent(this.fileVersionInput, 'change').pipe(
|
||||
distinctUntilChanged(),
|
||||
flatMap(() => this.contentService.versionUploadDialog().afterClosed()),
|
||||
})
|
||||
);
|
||||
|
||||
private uploadVersion() {
|
||||
this.contentService
|
||||
.versionUploadDialog()
|
||||
.afterClosed()
|
||||
.pipe(
|
||||
tap(form => {
|
||||
if (!form) {
|
||||
this.fileVersionInput.value = '';
|
||||
}
|
||||
}),
|
||||
filter(form => !!form),
|
||||
flatMap(form => forkJoin(of(form), this.contentService.getNodeInfo())),
|
||||
map(([form, node]) => {
|
||||
flatMap(form =>
|
||||
forkJoin(
|
||||
of(form),
|
||||
this.contentService.getNodeInfo().pipe(
|
||||
catchError(_ => {
|
||||
this.store.dispatch(
|
||||
new SnackbarErrorAction('VERSION.ERROR.GENERIC')
|
||||
);
|
||||
return of(null);
|
||||
})
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
.subscribe(([form, node]) => {
|
||||
if (form && node) {
|
||||
const file = this.fileVersionInput.files[0];
|
||||
const fileModel = new FileModel(
|
||||
file,
|
||||
@ -139,17 +151,12 @@ export class UploadEffects {
|
||||
},
|
||||
node.id
|
||||
);
|
||||
this.uploadAndUnlock(fileModel);
|
||||
}
|
||||
|
||||
this.fileVersionInput.value = '';
|
||||
this.uploadAndUnlock(fileModel);
|
||||
}),
|
||||
catchError(_ => {
|
||||
this.fileVersionInput.value = '';
|
||||
return of(new SnackbarErrorAction('VERSION.ERROR.GENERIC'));
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private upload(event: any): void {
|
||||
this.store
|
||||
|
Loading…
x
Reference in New Issue
Block a user