diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/karma.conf.js b/ng2-components/ng2-alfresco-activiti-tasklist/karma.conf.js index 0dbade2c19..ee5d5a7808 100644 --- a/ng2-components/ng2-alfresco-activiti-tasklist/karma.conf.js +++ b/ng2-components/ng2-alfresco-activiti-tasklist/karma.conf.js @@ -4,7 +4,7 @@ module.exports = function (config) { var configuration = { basePath: '.', - frameworks: ['jasmine'], + frameworks: ['jasmine-ajax', 'jasmine'], files: [ // paths loaded by Karma @@ -64,6 +64,7 @@ module.exports = function (config) { plugins: [ 'karma-jasmine', 'karma-coverage', + 'karma-jasmine-ajax', 'karma-chrome-launcher', 'karma-mocha-reporter', 'karma-jasmine-html-reporter' diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/package.json b/ng2-components/ng2-alfresco-activiti-tasklist/package.json index 5ab6852405..6e122e61c5 100644 --- a/ng2-components/ng2-alfresco-activiti-tasklist/package.json +++ b/ng2-components/ng2-alfresco-activiti-tasklist/package.json @@ -88,6 +88,7 @@ "karma-coverage": "1.0.0", "karma-coveralls": "1.1.2", "karma-jasmine": "1.0.2", + "karma-jasmine-ajax": "0.1.13", "karma-mocha-reporter": "2.0.3", "karma-jasmine-html-reporter": "0.2.0", "license-check": "1.1.5", diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/assets/AlfrescoSettingsService.service.mock.ts b/ng2-components/ng2-alfresco-activiti-tasklist/src/assets/AlfrescoSettingsService.service.mock.ts new file mode 100644 index 0000000000..593b36c19f --- /dev/null +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/assets/AlfrescoSettingsService.service.mock.ts @@ -0,0 +1,36 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Injectable } from '@angular/core'; + +@Injectable() +export class AlfrescoSettingsServiceMock { + + static DEFAULT_HOST_ADDRESS: string = 'fakehost'; + + private providers: string[] = ['ECM', 'BPM']; + + private _host: string = AlfrescoSettingsServiceMock.DEFAULT_HOST_ADDRESS; + + public get host(): string { + return this._host; + } + + getProviders(): string [] { + return this.providers; + } +} diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/assets/translation.service.mock.ts b/ng2-components/ng2-alfresco-activiti-tasklist/src/assets/translation.service.mock.ts new file mode 100644 index 0000000000..3ddf9a9050 --- /dev/null +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/assets/translation.service.mock.ts @@ -0,0 +1,37 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Observable } from 'rxjs/Rx'; +import { EventEmitter } from '@angular/core'; + +export interface LangChangeEvent { + lang: string; + translations: any; +} + +export class TranslationMock { + + public onLangChange: EventEmitter = new EventEmitter(); + + addTranslationFolder() { + + } + + public get(key: string|Array, interpolateParams?: Object): Observable { + return Observable.of(key); + } +} diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.spec.ts b/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.spec.ts new file mode 100644 index 0000000000..9fc30e894f --- /dev/null +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.spec.ts @@ -0,0 +1,164 @@ +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { it, describe, inject, beforeEach, beforeEachProviders } from '@angular/core/testing'; +import { ActivitiTaskListService } from './activiti-tasklist.service'; +import { AlfrescoSettingsService, AlfrescoAuthenticationService } from 'ng2-alfresco-core'; +import { AlfrescoSettingsServiceMock } from '../assets/AlfrescoSettingsService.service.mock'; +import { HTTP_PROVIDERS } from '@angular/http'; + +declare let AlfrescoApi: any; +declare let jasmine: any; + +describe('AlfrescoUploadService', () => { + let service, options: any; + + options = { + host: 'fakehost', + url: '/some/cool/url', + baseUrlPath: 'fakebasepath', + formFields: { + siteid: 'fakeSite', + containerid: 'fakeFolder' + } + }; + + beforeEachProviders(() => { + return [ + HTTP_PROVIDERS, + { provide: AlfrescoSettingsService, useClass: AlfrescoSettingsServiceMock }, + { provide: AlfrescoAuthenticationService, useClass: AlfrescoAuthenticationService }, + ActivitiTaskListService + ]; + }); + + beforeEach( inject([ActivitiTaskListService], (activitiService: ActivitiTaskListService) => { + jasmine.Ajax.install(); + service = activitiService; + })); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('should return the task list filters', (done) => { + let fakeFilter = { + size: 2, total: 2, start: 0, + data: [ + { + id: 1, name: 'FakeInvolvedTasks', recent: false, icon: 'glyphicon-align-left', + filter: {sort: 'created-desc', name: '', state: 'open', assignment: 'fake-involved'} + }, + { + id: 2, name: 'FakeMyTasks', recent: false, icon: 'glyphicon-align-left', + filter: {sort: 'created-desc', name: '', state: 'open', assignment: 'fake-assignee'} + } + ] + }; + + let filters = service.getTaskListFilters(); + filters.subscribe(res => { + expect(res).toBeDefined(); + expect(res.size).toEqual(2); + expect(res.total).toEqual(2); + expect(res.data.length).toEqual(2); + expect(res.data[0].name).toEqual('FakeInvolvedTasks'); + expect(res.data[1].name).toEqual('FakeMyTasks'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeFilter) + }); + }); + + it('should return the task list filtered', (done) => { + let fakeTaskList = { + size: 1, total: 1, start: 0, + data: [ + { + id: 1, name: 'FakeNameTask', description: null, category: null, + assignee: { + id: 1, + firstName: null, + lastName: 'Fake Admin', + email: 'fake-admin' + }, + created: '2016-07-15T11:19:17.440+0000' + } + ] + }; + + let fakeFilter = { + page: 2, filterId: 2, appDefinitionId: null, + filter: {sort: 'created-desc', name: '', state: 'open', assignment: 'fake-assignee'} + }; + + let taskList = service.getTasks(fakeFilter); + taskList.subscribe(res => { + expect(res).toBeDefined(); + expect(res.size).toEqual(1); + expect(res.total).toEqual(1); + expect(res.data.length).toEqual(1); + expect(res.data[0].name).toEqual('FakeNameTask'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeTaskList) + }); + }); + + it('should throw an exception when the response is wrong', (done) => { + let fakeTaskList = { + error: 'wrong request' + }; + + let fakeFilter = { + page: 2, filterId: 2, appDefinitionId: null, + wrongfilter: {sort: 'created-desc', name: '', state: 'open', assignment: 'fake-assignee'} + }; + + let fakePromise = new Promise(function (resolve, reject) { + reject(fakeTaskList); + }); + spyOn(service, 'callApiTasksFiltered').and.returnValue(fakePromise); + + let taskList = service.getTasks(fakeFilter); + + service.getTasks(fakeFilter).subscribe((res) => { + let tasks = res.data; + service.loadTasks(tasks); + }); + taskList.subscribe( + (res) => { + + }, + (err: any) => { + expect(err).toBeDefined(); + expect(err.error).toEqual('wrong request'); + done(); + }); + + + }); + +}); diff --git a/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts b/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts index 56af454e54..f1afbeb47d 100644 --- a/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts +++ b/ng2-components/ng2-alfresco-activiti-tasklist/src/services/activiti-tasklist.service.ts @@ -43,7 +43,10 @@ export class ActivitiTaskListService { data.filterId = filter.id; data.filter = filter.filter; data = JSON.stringify(data); - return this.callApiTasksFiltered(data); + + return Observable.fromPromise(this.callApiTasksFiltered(data)) + .map((res: Response) => res.json()) + .catch(this.handleError); } private callApiTasksFiltered(data: Object): Observable { @@ -55,9 +58,7 @@ export class ActivitiTaskListService { let options = new RequestOptions({headers: headers}); return this.http - .post(url, data, options) - .map((res: Response) => res.json()) - .catch(this.handleError); + .post(url, data, options); } private callApiTaskFilters(): Observable {