From 0ee2a1654c3d31c7788d296235931bcbf976dee7 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Mon, 5 Sep 2016 13:56:35 +0200 Subject: [PATCH] improve test service --- ng2-components/ng2-alfresco-tag/README.md | 3 +- .../ng2-alfresco-tag/demo/package.json | 7 +- .../ng2-alfresco-tag/demo/src/main.ts | 4 +- .../ng2-alfresco-tag/demo/systemjs.config.js | 2 - ng2-components/ng2-alfresco-tag/karma.conf.js | 4 + ng2-components/ng2-alfresco-tag/package.json | 7 +- .../components/tag-actions.component.spec.ts | 36 ++++++ .../src/components/tag-actions.component.ts | 1 - .../src/components/tag-list.component.spec.ts | 53 ++++++++ .../src/components/tag-list.component.ts | 3 +- .../tag-node-list.component.spec.ts | 60 +++++++++ .../src/components/tag-node-list.component.ts | 22 +--- .../src/services/tag.service.spec.ts | 114 +++++++++++++++++ .../src/services/tag.service.ts | 119 ++++-------------- .../src/webscript.component.spec.ts | 3 +- 15 files changed, 305 insertions(+), 133 deletions(-) create mode 100644 ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts create mode 100644 ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts create mode 100644 ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts diff --git a/ng2-components/ng2-alfresco-tag/README.md b/ng2-components/ng2-alfresco-tag/README.md index a9be7dd0c5..acc53e0444 100644 --- a/ng2-components/ng2-alfresco-tag/README.md +++ b/ng2-components/ng2-alfresco-tag/README.md @@ -56,11 +56,10 @@ The following component needs to be added to your systemjs.config: - ng2-translate - ng2-alfresco-core -- ng2-alfresco-datatable Please refer to the following example to have an idea of how your systemjs.config should look like : -https://github.com/Alfresco/alfresco-ng2-components/blob/master/ng2-components/ng2-alfresco-webscript/demo/systemjs.config.js +https://github.com/Alfresco/alfresco-ng2-components/blob/master/ng2-components/ng2-alfresco-tag/demo/systemjs.config.js #### Style The style of this component is based on material design, so if you want to visualize it correctly you have to add the material diff --git a/ng2-components/ng2-alfresco-tag/demo/package.json b/ng2-components/ng2-alfresco-tag/demo/package.json index 3271a9e5c0..05f8895f5a 100644 --- a/ng2-components/ng2-alfresco-tag/demo/package.json +++ b/ng2-components/ng2-alfresco-tag/demo/package.json @@ -1,7 +1,7 @@ { "name": "ng2-alfresco-tag-demo", "description": "Alfresco Angular2 Tag - Demo", - "version": "0.2.0", + "version": "0.3.0", "author": "Alfresco Software, Ltd.", "main": "index.js", "scripts": { @@ -37,11 +37,10 @@ "material-design-icons": "2.2.3", "material-design-lite": "1.1.3", - "alfresco-js-api": "^0.2.0", + "alfresco-js-api": "^0.3.0", "ng2-translate": "2.2.2", - "ng2-alfresco-core": "^0.2.0", - "ng2-alfresco-datatable": "^0.2.0" + "ng2-alfresco-core": "^0.3.0" }, "devDependencies": { "concurrently": "2.0.0", diff --git a/ng2-components/ng2-alfresco-tag/demo/src/main.ts b/ng2-components/ng2-alfresco-tag/demo/src/main.ts index 39ca262b90..e6300c6a1c 100644 --- a/ng2-components/ng2-alfresco-tag/demo/src/main.ts +++ b/ng2-components/ng2-alfresco-tag/demo/src/main.ts @@ -33,8 +33,8 @@ import {TAGCOMPONENT, TAGSERVICES} from 'ng2-alfresco-tag'; template: `

-
-

+
+

Authentication failed to ip {{ ecmHost }} with user: admin, admin, you can still try to add a valid token to perform operations. diff --git a/ng2-components/ng2-alfresco-tag/demo/systemjs.config.js b/ng2-components/ng2-alfresco-tag/demo/systemjs.config.js index 9ae03109fd..a14e75e0ba 100644 --- a/ng2-components/ng2-alfresco-tag/demo/systemjs.config.js +++ b/ng2-components/ng2-alfresco-tag/demo/systemjs.config.js @@ -12,7 +12,6 @@ 'ng2-translate': 'node_modules/ng2-translate', 'ng2-alfresco-core': 'node_modules/ng2-alfresco-core/dist', - 'ng2-alfresco-datatable': 'node_modules/ng2-alfresco-datatable/dist', 'ng2-alfresco-tag': 'node_modules/ng2-alfresco-tag/dist' }; // packages tells the System loader how to load when no filename and/or no extension @@ -23,7 +22,6 @@ 'ng2-translate': { defaultExtension: 'js' }, 'ng2-alfresco-core': { main: 'index.js', defaultExtension: 'js' }, - 'ng2-alfresco-datatable': { main: 'index.js', defaultExtension: 'js' }, 'ng2-alfresco-tag': { main: 'index.js', defaultExtension: 'js' } }; var ngPackageNames = [ diff --git a/ng2-components/ng2-alfresco-tag/karma.conf.js b/ng2-components/ng2-alfresco-tag/karma.conf.js index c488ed18cb..c84efe0a76 100644 --- a/ng2-components/ng2-alfresco-tag/karma.conf.js +++ b/ng2-components/ng2-alfresco-tag/karma.conf.js @@ -17,6 +17,10 @@ module.exports = function (config) { {pattern: 'node_modules/@angular/**/*.js', included: false, watched: false}, {pattern: 'node_modules/@angular/**/*.map', included: false, watched: false}, {pattern: 'node_modules/ng2-alfresco-core/dist/**/*.js', included: false, served: true, watched: false}, + {pattern: 'node_modules/ng2-alfresco-datatable/dist/**/*.js', included: false, served: true, watched: false}, + {pattern: 'node_modules/ng2-alfresco-datatable/dist/**/*.html', included: false, served: true, watched: false}, + {pattern: 'node_modules/ng2-alfresco-datatable/dist/**/*.css', included: false, served: true, watched: false}, + {pattern: 'node_modules/ng2-translate/**/*.js', included: false, served: true, watched: false}, {pattern: 'node_modules/alfresco-js-api/dist/alfresco-js-api.js', included: true, watched: false}, {pattern: 'node_modules/material-design-lite/material.min.js', included: true, watched: false}, diff --git a/ng2-components/ng2-alfresco-tag/package.json b/ng2-components/ng2-alfresco-tag/package.json index d503697e15..780714bdf0 100644 --- a/ng2-components/ng2-alfresco-tag/package.json +++ b/ng2-components/ng2-alfresco-tag/package.json @@ -1,7 +1,7 @@ { "name": "ng2-alfresco-tag", "description": "Alfresco tag component", - "version": "0.2.0", + "version": "0.3.0", "author": "Alfresco Software, Ltd.", "main": "./dist/index.js", "typings": "./dist/index.d.ts", @@ -44,11 +44,10 @@ "@angular/upgrade": "2.0.0-rc.3", "systemjs": "0.19.27", "core-js": "^2.4.0", - "alfresco-js-api": "^0.2.0", + "alfresco-js-api": "^0.3.0", "ng2-translate": "2.2.2", - "ng2-alfresco-core": "^0.2.0", - "ng2-alfresco-datatable": "^0.2.0", + "ng2-alfresco-core": "^0.3.0", "reflect-metadata": "^0.1.3", "rxjs": "5.0.0-beta.6", "zone.js": "^0.6.12" diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts index 9f5dfcd9e9..78dbc712c5 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts @@ -15,3 +15,39 @@ * limitations under the License. */ + +import { describe, inject, beforeEachProviders, beforeEach, afterEach } from '@angular/core/testing'; +import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core'; +import { TagService } from '../services/tag.service'; + +declare let jasmine: any; + +describe('Tag actions', () => { + + let service; + + beforeEachProviders(() => { + + return [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + TagService + ]; + }); + + beforeEach(inject([TagService], (tagService: TagService) => { + service = tagService; + })); + + describe('Content tests', () => { + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + }); +}); diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts index 74c76e9077..bd0d7d50e4 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts @@ -48,7 +48,6 @@ export class TagActionsComponent { tagsEntries: any; - /** * Constructor * @param authService diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts new file mode 100644 index 0000000000..49b0c333d9 --- /dev/null +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts @@ -0,0 +1,53 @@ +/*! + * @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 { describe, inject, beforeEachProviders, beforeEach, afterEach } from '@angular/core/testing'; +import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core'; +import { TagService } from '../services/tag.service'; + +declare let jasmine: any; + +describe('Tag list', () => { + + let service; + + beforeEachProviders(() => { + + return [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + TagService + ]; + }); + + beforeEach(inject([TagService], (tagService: TagService) => { + service = tagService; + })); + + describe('Content tests', () => { + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + }); +}); diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts index c9a1bd0f59..9e48474af1 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts @@ -54,8 +54,7 @@ export class TagList { } refreshTagEcm() { - - this.tagService.getAllTheTags().then((data) => { + this.tagService.getAllTheTags().subscribe((data) => { this.tagsEntries = data; }); } diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts new file mode 100644 index 0000000000..7730604026 --- /dev/null +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts @@ -0,0 +1,60 @@ +/*! + * @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 { describe, inject, beforeEachProviders, beforeEach, afterEach } from '@angular/core/testing'; +import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core'; +import { TagService } from '../services/tag.service'; + +declare let jasmine: any; + +describe('Tag relative node list', () => { + + let service; + + beforeEachProviders(() => { + + return [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + TagService + ]; + }); + + describe('Content tests', () => { + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('getTagsByNodeId catch errors call', (done) => { + service.getTagsByNodeId('fake-node-id', 'fake-tag').subscribe(() => { + }, () => { + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 403 + }); + }); + + }); +}); diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts index f3025bd39f..05960c8f7a 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts @@ -15,9 +15,9 @@ * limitations under the License. */ -import {Component, Input} from '@angular/core'; -import {AlfrescoAuthenticationService} from 'ng2-alfresco-core'; -import {TagService} from '../services/tag.service'; +import { Component, Input } from '@angular/core'; +import { AlfrescoAuthenticationService } from 'ng2-alfresco-core'; +import { TagService } from '../services/tag.service'; /** * @@ -38,9 +38,6 @@ export class TagNodeList { @Input() nodeId: string; - @Input() - properties: string; - tagsEntries: any; /** @@ -48,7 +45,6 @@ export class TagNodeList { * @param authService */ constructor(public authService: AlfrescoAuthenticationService, private tagService: TagService) { - } ngOnChanges(changes) { @@ -56,15 +52,9 @@ export class TagNodeList { } refreshTag() { - if (this.nodeId) { - this.tagService.getTagsByNodeId(this.nodeId).then((data) => { - this.tagsEntries = data; - }); - } else if (this.properties) { - this.tagService.getTagsByProperties(this.properties).then((data) => { - this.tagsEntries = data; - }); - } + this.tagService.getTagsByNodeId(this.nodeId).then((data) => { + this.tagsEntries = data; + }); } removeTag(tag: string) { diff --git a/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts new file mode 100644 index 0000000000..61e0621be7 --- /dev/null +++ b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts @@ -0,0 +1,114 @@ +/*! + * @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 { describe, expect, it, inject, beforeEachProviders, beforeEach, afterEach } from '@angular/core/testing'; +import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core'; +import { TagService } from './tag.service'; + +declare let jasmine: any; + +describe('Tag service', () => { + + let service; + + beforeEachProviders(() => { + + return [ + AlfrescoSettingsService, + AlfrescoAuthenticationService, + TagService + ]; + }); + + beforeEach(inject([TagService], (tagService: TagService) => { + service = tagService; + })); + + describe('Content tests', () => { + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('removeTag should perform a call against the server', (done) => { + service.removeTag('fake-node-id', 'fake-tag').subscribe(() => { + expect(jasmine.Ajax.requests.mostRecent().method).toBe('DELETE'); + expect(jasmine.Ajax.requests.mostRecent().url) + .toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/tags/fake-tag'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + }); + + it('addTag should perform a call against the server', (done) => { + service.addTag('fake-node-id', 'fake-tag').subscribe(() => { + expect(jasmine.Ajax.requests.mostRecent().method).toBe('POST'); + expect(jasmine.Ajax.requests.mostRecent().url) + .toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/tags'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + }); + + it('getAllTheTags should perform a call against the server', (done) => { + service.getAllTheTags('fake-node-id', 'fake-tag').subscribe(() => { + expect(jasmine.Ajax.requests.mostRecent().method).toBe('GET'); + expect(jasmine.Ajax.requests.mostRecent().url) + .toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/tags'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + }); + + it('getTagsByNodeId should perform a call against the server', (done) => { + service.getTagsByNodeId('fake-node-id', 'fake-tag').subscribe(() => { + expect(jasmine.Ajax.requests.mostRecent().method).toBe('GET'); + expect(jasmine.Ajax.requests.mostRecent().url) + .toBe('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/fake-node-id/tags'); + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + }); + + it('getTagsByNodeId catch errors call', (done) => { + service.getTagsByNodeId('fake-node-id', 'fake-tag').subscribe(() => { + }, () => { + done(); + }); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 403 + }); + }); + }); +}); diff --git a/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts b/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts index ee1db27d9c..528f1c18e0 100644 --- a/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts +++ b/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts @@ -15,128 +15,51 @@ * limitations under the License. */ -import {Injectable} from '@angular/core'; -import {AlfrescoAuthenticationService} from 'ng2-alfresco-core'; +import { Injectable } from '@angular/core'; +import { AlfrescoAuthenticationService } from 'ng2-alfresco-core'; +import { Observable } from 'rxjs/Rx'; /** - * - * This component, provide a list of the tags relative a node with actions button to add or remove new tag - * - * @returns {TagComponent} . + * @returns {TagService} . */ declare let __moduleName: string; @Injectable() export class TagService { - cacheTagList: any = []; - - promiseCache: any; - /** * Constructor * @param authService */ constructor(public authService: AlfrescoAuthenticationService) { - this.promiseCache = new Promise((resolve) => { - this.getAllTheTags().then((data) => { - this.cacheTagList = data; - resolve(data); - }); - }); - } - - getTagsByProperties(properties: string) { - return new Promise((resolve) => { - if (!this.cacheTagList) { - this.promiseCache.then((data) => { - this.cacheTagList = data; - resolve(this._associateTagByNodeProperties(properties, data)); - }); - } else { - resolve(this._associateTagByNodeProperties(properties, this.cacheTagList)); - } - }); - } - - _associateTagByNodeProperties(properties: any, tagList: any) { - let tagsArray = []; - if (properties) { - try { - let jsonProps; - - if (typeof properties !== 'object') { - jsonProps = JSON.parse(properties); - } else { - jsonProps = properties; - } - - if (jsonProps.hasOwnProperty('cm:taggable')) { - jsonProps['cm:taggable'].forEach((currentTagId) => { - if (tagList && tagList.length > 0) { - let tag = tagList.filter((currentCacheTag) => { - if (currentCacheTag.entry.id === currentTagId) { - return currentCacheTag.entry; - } - }); - if (tag && tag.length > 0) { - tagsArray.push(tag[0]); - } - } - }); - } - } catch (error) { - console.log('error' + error); - } - } - return tagsArray; } getTagsByNodeId(nodeId: string): any { - return new Promise((resolve, reject) => { - this.authService.getAlfrescoApi().core.tagsApi.getNodeTags(nodeId).then((data) => { - resolve(data.list.entries); - }, function (error) { - console.log('Error' + error); - reject(); - }); - }); + return Observable.fromPromise(this.authService.getAlfrescoApi().core.tagsApi.getNodeTags(nodeId)) + .catch(this.handleError); } getAllTheTags() { - return new Promise((resolve, reject) => { - this.authService.getAlfrescoApi().core.tagsApi.getTags().then((data) => { - resolve(data.list.entries); - }, function (error) { - console.log('Error' + error); - reject(); - }); - }); + return Observable.fromPromise(this.authService.getAlfrescoApi().core.tagsApi.getTags()) + .catch(this.handleError); } addTag(nodeId: string, tagName: string): any { - return new Promise((resolve, reject) => { - let alfrescoApi = this.authService.getAlfrescoApi(); - let tagBody = new alfrescoApi.core.TagBody(); - tagBody.tag = tagName; - this.authService.getAlfrescoApi().core.tagsApi.addTag(nodeId, tagBody).then((res) => { - this.cacheTagList.push(res); - resolve(res); - }, function (error) { - console.log('Error' + error); - reject(); - }); - }); + let alfrescoApi = this.authService.getAlfrescoApi(); + let tagBody = new alfrescoApi.core.TagBody(); + tagBody.tag = tagName; + + return Observable.fromPromise(this.authService.getAlfrescoApi().core.tagsApi.addTag(nodeId, tagBody)) + .catch(this.handleError); } removeTag(nodeId: string, tag: string): any { - return new Promise((resolve, reject) => { - this.authService.getAlfrescoApi().core.tagsApi.removeTag(nodeId, tag).then(() => { - resolve(); - }, function (error) { - console.log('Error' + error); - reject(); - }); - }); + return Observable.fromPromise(this.authService.getAlfrescoApi().core.tagsApi.removeTag(nodeId, tag)) + .catch(this.handleError); + } + + private handleError(error: any) { + console.error(error); + return Observable.throw(error || 'Server error'); } } diff --git a/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts b/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts index fd11aa8534..c212100195 100644 --- a/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts +++ b/ng2-components/ng2-alfresco-webscript/src/webscript.component.spec.ts @@ -85,8 +85,7 @@ describe('Test ng2-alfresco-webscript', () => { component.ngOnChanges().then(() => { webscriptComponentFixture.detectChanges(); - let request = jasmine.Ajax.requests.mostRecent(); - expect(request.url).toBe('http://localhost:8080/alfresco/service/sample/folder/Company%20Home'); + expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://localhost:8080/alfresco/service/sample/folder/Company%20Home'); done(); });