diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts index be00030f2d..e7c9529708 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.spec.ts @@ -16,7 +16,6 @@ */ import { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; -import { EventEmitter } from '@angular/core'; import { RenditionsService } from './renditions.service'; import { AlfrescoSettingsService, AlfrescoApiService, AlfrescoAuthenticationService } from 'ng2-alfresco-core'; @@ -35,7 +34,7 @@ describe('RenditionsService', () => { ]; }); - beforeEach( inject([RenditionsService, AlfrescoApiService], (renditionsService: RenditionsService, apiService: AlfrescoApiService) => { + beforeEach(inject([RenditionsService, AlfrescoApiService], (renditionsService: RenditionsService, apiService: AlfrescoApiService) => { jasmine.Ajax.install(); service = renditionsService; apiService.setInstance(new AlfrescoApi({})); @@ -45,7 +44,41 @@ describe('RenditionsService', () => { jasmine.Ajax.uninstall(); }); - it('', (done) => { + it('Get redition service should call the server with the ID passed', (done) => { + service.getRenditionsByNodeId('fake-node-id').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); + done(); + }); + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json' + }); + }); + + it('Create redition service should call the server with the ID passed and the asked encoding', (done) => { + service.createRendition('fake-node-id', 'pdf').subscribe((res) => { + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/renditions'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json' + }); + }); + + it('Get redition service should catch the error', (done) => { + + service.getRenditionsByNodeId('fake-node-id').subscribe((res) => { + }, (res) => { + done(); + } + ); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 403, + contentType: 'application/json' + }); }); }); diff --git a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts index 7359e0a054..f94b64bd29 100644 --- a/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts +++ b/ng2-components/ng2-alfresco-upload/src/services/renditions.service.ts @@ -16,6 +16,7 @@ */ import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs/Rx'; import { AlfrescoApiService } from 'ng2-alfresco-core'; /** @@ -30,7 +31,18 @@ export class RenditionsService { } - private getRenditionsByNodeId(nodeId: string) { + getRenditionsByNodeId(nodeId: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.getRenditions(nodeId)) + .catch(this.handleError); + } + createRendition(nodeId: string, encoding: string) { + return Observable.fromPromise(this.apiService.getInstance().core.renditionsApi.createRendition(nodeId, encoding)) + .catch(this.handleError); + } + + private handleError(error: any): Observable { + console.error(error); + return Observable.throw(error || 'Server error'); } }