diff --git a/src/app/services/create-file-from-template.service.spec.ts b/src/app/services/create-file-from-template.service.spec.ts new file mode 100644 index 000000000..70e7c89be --- /dev/null +++ b/src/app/services/create-file-from-template.service.spec.ts @@ -0,0 +1,156 @@ +/*! + * @license + * Alfresco Example Content Application + * + * Copyright (C) 2005 - 2019 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 { TestBed, fakeAsync, tick } from '@angular/core/testing'; +import { EffectsModule } from '@ngrx/effects'; +import { AppStore, SnackbarErrorAction } from '@alfresco/aca-shared/store'; +import { TemplateEffects } from '../store/effects/template.effects'; +import { AppTestingModule } from '../testing/app-testing.module'; +import { Store } from '@ngrx/store'; +import { MatDialog } from '@angular/material/dialog'; +import { AlfrescoApiService, AlfrescoApiServiceMock } from '@alfresco/adf-core'; +import { CreateFileFromTemplateService } from './create-file-from-template.service'; +import { of } from 'rxjs'; + +describe('CreateFileFromTemplateService', () => { + let dialog: MatDialog; + let store: Store; + let alfrescoApiService: AlfrescoApiService; + let createFileFromTemplateService: CreateFileFromTemplateService; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [AppTestingModule, EffectsModule.forRoot([TemplateEffects])], + providers: [ + CreateFileFromTemplateService, + { provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock } + ] + }); + + store = TestBed.get(Store); + alfrescoApiService = TestBed.get(AlfrescoApiService); + dialog = TestBed.get(MatDialog); + createFileFromTemplateService = TestBed.get(CreateFileFromTemplateService); + }); + + it('should open dialog with `Node Templates` folder id as data property', () => { + spyOn( + alfrescoApiService.getInstance().nodes, + 'getNodeInfo' + ).and.returnValue(of({ id: 'templates-folder-id' })); + spyOn(dialog, 'open'); + + createFileFromTemplateService.openTemplatesDialog(); + + expect(dialog.open['calls'].argsFor(0)[1].data).toEqual( + jasmine.objectContaining({ currentFolderId: 'templates-folder-id' }) + ); + }); + + it('should remove parents for templates node breadcrumb path', () => { + spyOn( + alfrescoApiService.getInstance().nodes, + 'getNodeInfo' + ).and.returnValue( + of({ + id: 'templates-folder-id', + path: { + elements: [], + name: '/Company Home/Data Dictionary' + } + }) + ); + spyOn(dialog, 'open'); + + createFileFromTemplateService.openTemplatesDialog(); + + const breadcrumb = dialog.open['calls'] + .argsFor(0)[1] + .data.breadcrumbTransform({ + name: 'Node Templates', + path: { + elements: [{ name: 'Company Home' }, { name: 'Data Dictionary' }], + name: '/Company Home/Data Dictionary' + } + }); + + expect(breadcrumb.path.elements).toEqual([]); + }); + + it('should return false if selected node is not a template file', () => { + spyOn( + alfrescoApiService.getInstance().nodes, + 'getNodeInfo' + ).and.returnValue(of({ id: 'templates-folder-id' })); + spyOn(dialog, 'open'); + + createFileFromTemplateService.openTemplatesDialog(); + + const isSelectionValid = dialog.open['calls'] + .argsFor(0)[1] + .data.isSelectionValid({ + isFile: false + }); + + expect(isSelectionValid).toBe(false); + }); + + it('should return true if selected node is a template file', () => { + spyOn( + alfrescoApiService.getInstance().nodes, + 'getNodeInfo' + ).and.returnValue(of({ id: 'templates-folder-id' })); + spyOn(dialog, 'open'); + + createFileFromTemplateService.openTemplatesDialog(); + + const isSelectionValid = dialog.open['calls'] + .argsFor(0)[1] + .data.isSelectionValid({ + isFile: true + }); + + expect(isSelectionValid).toBe(true); + }); + + it('should raise an error when getNodeInfo fails', fakeAsync(() => { + spyOn( + alfrescoApiService.getInstance().nodes, + 'getNodeInfo' + ).and.returnValue( + Promise.reject({ + message: `{ "error": { "statusCode": 404 } } ` + }) + ); + spyOn(store, 'dispatch'); + + createFileFromTemplateService.openTemplatesDialog(); + tick(); + + expect(store.dispatch).toHaveBeenCalledWith( + new SnackbarErrorAction('APP.MESSAGES.ERRORS.GENERIC') + ); + })); +}); diff --git a/src/app/store/effects/template.effects.spec.ts b/src/app/store/effects/template.effects.spec.ts new file mode 100644 index 000000000..51e976b27 --- /dev/null +++ b/src/app/store/effects/template.effects.spec.ts @@ -0,0 +1,93 @@ +/*! + * @license + * Alfresco Example Content Application + * + * Copyright (C) 2005 - 2019 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 { TestBed, fakeAsync, tick } from '@angular/core/testing'; +import { AppTestingModule } from '../../testing/app-testing.module'; +import { TemplateEffects } from './template.effects'; +import { EffectsModule } from '@ngrx/effects'; +import { Store } from '@ngrx/store'; +import { + CreateFileFromTemplate, + SnackbarErrorAction +} from '@alfresco/aca-shared/store'; +import { CreateFileFromTemplateService } from '../../services/create-file-from-template.service'; +import { of } from 'rxjs'; +import { AlfrescoApiServiceMock, AlfrescoApiService } from '@alfresco/adf-core'; +import { ContentManagementService } from '../../services/content-management.service'; + +describe('TemplateEffects', () => { + let store: Store; + let createFileFromTemplateService: CreateFileFromTemplateService; + let alfrescoApiService: AlfrescoApiService; + let contentManagementService: ContentManagementService; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [AppTestingModule, EffectsModule.forRoot([TemplateEffects])], + providers: [ + CreateFileFromTemplateService, + { provide: AlfrescoApiService, useClass: AlfrescoApiServiceMock } + ] + }); + + store = TestBed.get(Store); + createFileFromTemplateService = TestBed.get(CreateFileFromTemplateService); + alfrescoApiService = TestBed.get(AlfrescoApiService); + contentManagementService = TestBed.get(ContentManagementService); + + spyOn(contentManagementService.reload, 'next'); + spyOn(store, 'select').and.returnValue(of({ id: 'parent-id' })); + spyOn(createFileFromTemplateService, 'openTemplatesDialog').and.returnValue( + of([{ id: 'template-id' }]) + ); + }); + + it('should reload content on template copy', fakeAsync(() => { + spyOn(alfrescoApiService.getInstance().nodes, 'copyNode').and.returnValue( + of({}) + ); + store.dispatch(new CreateFileFromTemplate()); + tick(); + + expect(contentManagementService.reload.next).toHaveBeenCalled(); + })); + + it('should raise error when copy template fails', fakeAsync(() => { + spyOn(store, 'dispatch').and.callThrough(); + spyOn(alfrescoApiService.getInstance().nodes, 'copyNode').and.returnValue( + Promise.reject({ + message: `{ "error": { "statusCode": 404 } } ` + }) + ); + + store.dispatch(new CreateFileFromTemplate()); + tick(); + + expect(contentManagementService.reload.next).not.toHaveBeenCalled(); + expect(store.dispatch['calls'].argsFor(1)[0]).toEqual( + new SnackbarErrorAction('APP.MESSAGES.ERRORS.GENERIC') + ); + })); +});