From fa43e54e0896eb826c1a731cb38e160578a1dbbc Mon Sep 17 00:00:00 2001 From: Will Abson Date: Wed, 12 Oct 2016 12:07:04 +0100 Subject: [PATCH 1/6] Update search package test commands --- ng2-components/ng2-alfresco-search/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ng2-components/ng2-alfresco-search/package.json b/ng2-components/ng2-alfresco-search/package.json index af08568b8b..aee37698b8 100644 --- a/ng2-components/ng2-alfresco-search/package.json +++ b/ng2-components/ng2-alfresco-search/package.json @@ -15,8 +15,8 @@ "tsc:w": "tsc -w", "pretest": "npm run build", "test": "karma start karma.conf.js --reporters mocha,coverage --single-run", - "test-browser": "npm run build && concurrently \"karma start karma.conf.js --reporters kjhtml\" \"npm run watch-task\"", - "posttest": "remap-istanbul -i coverage/report/coverage-final.json -o coverage/report -t html && remap-istanbul -i coverage/report/coverage-final.json -o coverage/report/coverage-final.json", + "test-browser": "concurrently \"karma start karma.conf.js --reporters kjhtml\" \"npm run watch-task\"", + "posttest": "node_modules/.bin/remap-istanbul -i coverage/report/coverage-final.json -o coverage/report -t html && node_modules/.bin/remap-istanbul -i coverage/report/coverage-final.json -o coverage/report/coverage-final.json", "coverage": "npm run test && wsrv -o -p 9875 ./coverage/report", "prepublish": "npm run build", "travis": "npm link ng2-alfresco-core" From ae54a127e0e689f03dca91a960ce9f471b5073e1 Mon Sep 17 00:00:00 2001 From: Will Abson Date: Wed, 12 Oct 2016 12:08:03 +0100 Subject: [PATCH 2/6] Fix Karma warnings logged to console --- ng2-components/ng2-alfresco-search/karma.conf.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ng2-components/ng2-alfresco-search/karma.conf.js b/ng2-components/ng2-alfresco-search/karma.conf.js index c9d211a3ae..e1a9ddc3f8 100644 --- a/ng2-components/ng2-alfresco-search/karma.conf.js +++ b/ng2-components/ng2-alfresco-search/karma.conf.js @@ -42,9 +42,11 @@ module.exports = function (config) { {pattern: 'dist/**/*.html', included: true, served: true, watched: true}, {pattern: 'dist/**/*.css', included: true, served: true, watched: true}, + // mimetype icons + {pattern: 'dist/src/img/*.svg', included: false, served: true, watched: false}, + // ng2-components { pattern: 'node_modules/ng2-alfresco-core/dist/**/*.js', included: false, served: true, watched: false }, - { pattern: 'node_modules/ng2-alfresco-search/dist/**/*.js', included: false, served: true, watched: false }, // paths to support debugging with source maps in dev tools {pattern: 'src/**/*.ts', included: false, watched: false}, From 78a0253ebb745ba938b21a0679d5a5e8e62141de Mon Sep 17 00:00:00 2001 From: Will Abson Date: Wed, 12 Oct 2016 12:08:48 +0100 Subject: [PATCH 3/6] Re-enable search service tests --- ...esco-search-autocomplete.component.spec.ts | 26 ++++---- .../alfresco-search-autocomplete.component.ts | 4 +- .../services/alfresco-search.service.spec.ts | 63 +++++++++++-------- .../src/services/alfresco-search.service.ts | 23 +++---- 4 files changed, 61 insertions(+), 55 deletions(-) diff --git a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.spec.ts b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.spec.ts index 7694f261e1..d88a184b92 100644 --- a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.spec.ts +++ b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.spec.ts @@ -134,24 +134,23 @@ describe('AlfrescoSearchAutocompleteComponent', () => { expect(component.displaySearchResults).toHaveBeenCalledWith(searchTerm.currentValue); }); - it('should clear results straight away when a new search term is entered', (done) => { + it('should clear results straight away when a new search term is entered', async(() => { let searchService = fixture.debugElement.injector.get(AlfrescoSearchService); spyOn(searchService, 'getSearchNodesPromise') .and.returnValue(Promise.resolve(result)); - component.resultsEmitter.subscribe(x => { + component.searchTerm = 'searchTerm'; + component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''} }); + + fixture.whenStable().then(() => { fixture.detectChanges(); component.searchTerm = 'searchTerm2'; component.ngOnChanges({searchTerm: { currentValue: 'searchTerm2', previousValue: 'searchTerm'} }); fixture.detectChanges(); expect(element.querySelectorAll('table[data-automation-id="autocomplete_results"] tbody tr').length).toBe(0); - done(); }); - - component.searchTerm = 'searchTerm'; - component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''} }); - }); + })); it('should display the returned search results', (done) => { @@ -231,25 +230,24 @@ describe('AlfrescoSearchAutocompleteComponent', () => { component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''}}); }); - it('should clear errors straight away when a new search is performed', (done) => { + it('should clear errors straight away when a new search is performed', async(() => { let searchService = fixture.debugElement.injector.get(AlfrescoSearchService); spyOn(searchService, 'getSearchNodesPromise') .and.returnValue(Promise.reject(errorJson)); - component.errorEmitter.subscribe(() => { + component.searchTerm = 'searchTerm'; + component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''}}); + + fixture.whenStable().then(() => { fixture.detectChanges(); component.searchTerm = 'searchTerm2'; component.ngOnChanges({searchTerm: { currentValue: 'searchTerm2', previousValue: 'searchTerm'} }); fixture.detectChanges(); let errorEl = element.querySelector('[data-automation-id="autocomplete_error_message"]'); expect(errorEl).toBeNull(); - done(); }); - - component.searchTerm = 'searchTerm'; - component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''}}); - }); + })); it('should emit preview when file item clicked', (done) => { diff --git a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.ts b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.ts index 313bbb9e70..c5ffccb716 100644 --- a/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.ts +++ b/ng2-components/ng2-alfresco-search/src/components/alfresco-search-autocomplete.component.ts @@ -76,8 +76,8 @@ export class AlfrescoSearchAutocompleteComponent implements OnInit, OnChanges { public displaySearchResults(searchTerm) { if (searchTerm !== null && searchTerm !== '') { this.alfrescoSearchService - .getSearchNodesPromise(searchTerm) - .then( + .getLiveSearchResults(searchTerm) + .subscribe( results => { this.results = results.list.entries; this.errorMessage = null; diff --git a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts index 010ec5b935..a7d1157253 100644 --- a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts +++ b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts @@ -15,8 +15,7 @@ * limitations under the License. */ -/* -import { beforeEachProviders } from '@angular/core/testing'; +import { ReflectiveInjector } from '@angular/core'; import { AlfrescoSearchService } from './alfresco-search.service'; import { AlfrescoAuthenticationService, AlfrescoSettingsService, AlfrescoApiService } from 'ng2-alfresco-core'; @@ -24,7 +23,9 @@ declare let jasmine: any; describe('AlfrescoSearchService', () => { - let service: any; + let service: AlfrescoSearchService; + let authenticationService: AlfrescoAuthenticationService; + let injector: ReflectiveInjector; let fakeSearch = { list: { @@ -55,39 +56,49 @@ describe('AlfrescoSearchService', () => { } }; - beforeEachProviders(() => { - return [ + let fakeApi = { + core: { + searchApi: { + liveSearchNodes: (term, opts) => Promise.resolve(fakeSearch) + } + } + }; + + beforeEach(() => { + injector = ReflectiveInjector.resolveAndCreate([ AlfrescoSearchService, AlfrescoSettingsService, AlfrescoApiService, AlfrescoAuthenticationService - ]; + ]); + service = injector.get(AlfrescoSearchService); + authenticationService = injector.get(AlfrescoAuthenticationService); + spyOn(authenticationService, 'getAlfrescoApi').and.returnValue(fakeApi); }); - beforeEach(inject([AlfrescoSearchService], (alfrescoSearchService: AlfrescoSearchService) => { - jasmine.Ajax.install(); - service = alfrescoSearchService; - })); - - afterEach(() => { - jasmine.Ajax.uninstall(); - }); - - it('should return search list', (done) => { - service.getSearchNodesPromise('MyDoc').then( - (res) => { - expect(res).toBeDefined(); - expect(res.list.entries[0].entry.name).toEqual('MyDoc'); + it('should call search API with the correct parameters', (done) => { + let searchTerm = 'searchTerm63688', options = { + include: [ 'path' ], + rootNodeId: '-root-', + nodeType: 'cm:content' + }; + spyOn(fakeApi.core.searchApi, 'liveSearchNodes').and.returnValue(Promise.resolve(fakeSearch)); + service.getLiveSearchResults(searchTerm).subscribe( + () => { + expect(fakeApi.core.searchApi.liveSearchNodes).toHaveBeenCalledWith(searchTerm, options); done(); } ); + }); - jasmine.Ajax.requests.mostRecent().respondWith({ - 'status': 200, - contentType: 'application/json', - responseText: JSON.stringify(fakeSearch) - }); + it('should return search results returned from the API', (done) => { + service.getLiveSearchResults('').subscribe( + (res: any) => { + expect(res).toBeDefined(); + expect(res).toEqual(fakeSearch); + done(); + } + ); }); }); -*/ diff --git a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.ts b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.ts index cbfc12dbf3..caf5308d34 100644 --- a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.ts +++ b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.ts @@ -28,16 +28,6 @@ export class AlfrescoSearchService { constructor(private authService: AlfrescoAuthenticationService) { } - public getSearchNodesPromise(term: string) { - let nodeId = '-root-'; - let opts = { - include: ['path'], - rootNodeId: nodeId, - nodeType: 'cm:content' - }; - return this.authService.getAlfrescoApi().core.searchApi.liveSearchNodes(term, opts); - } - /** * Execute a search against the repository * @@ -50,10 +40,17 @@ export class AlfrescoSearchService { .catch(this.handleError); } + private getSearchNodesPromise(term: string) { + let nodeId = '-root-'; + let opts = { + include: ['path'], + rootNodeId: nodeId, + nodeType: 'cm:content' + }; + return this.authService.getAlfrescoApi().core.searchApi.liveSearchNodes(term, opts); + } + private handleError(error: any): Observable { - // in a real world app, we may send the error to some remote logging infrastructure - // instead of just logging it to the console - console.error(error); return Observable.throw(error || 'Server error'); } } From fa73a55ebd161406e8751e56301ad67aedca26b4 Mon Sep 17 00:00:00 2001 From: Will Abson Date: Wed, 12 Oct 2016 14:57:52 +0100 Subject: [PATCH 4/6] Add additional test for error returned by search API --- .../services/alfresco-search.service.spec.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts index a7d1157253..8a9d531352 100644 --- a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts +++ b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts @@ -56,6 +56,16 @@ describe('AlfrescoSearchService', () => { } }; + let fakeError = { + error: { + errorKey: 'Search failed', + statusCode: 400, + briefSummary: '08220082 search failed', + stackTrace: 'For security reasons the stack trace is no longer displayed, but the property is kept for previous versions.', + descriptionURL: 'https://api-explorer.alfresco.com' + } + }; + let fakeApi = { core: { searchApi: { @@ -101,4 +111,16 @@ describe('AlfrescoSearchService', () => { ); }); + it('should notify errors returned from the API', (done) => { + spyOn(fakeApi.core.searchApi, 'liveSearchNodes').and.returnValue(Promise.reject(fakeError)); + service.getLiveSearchResults('').subscribe( + () => {}, + (res: any) => { + expect(res).toBeDefined(); + expect(res).toEqual(fakeError); + done(); + } + ); + }); + }); From be4aec7e0764db0f811dd714140a8000b08caada Mon Sep 17 00:00:00 2001 From: Will Abson Date: Thu, 13 Oct 2016 12:54:17 +0100 Subject: [PATCH 5/6] Refactor mocks into external file --- .../assets/alfresco-search.service.mock.ts | 63 +++++++++++++++++++ .../services/alfresco-search.service.spec.ts | 48 +------------- 2 files changed, 64 insertions(+), 47 deletions(-) create mode 100644 ng2-components/ng2-alfresco-search/src/assets/alfresco-search.service.mock.ts diff --git a/ng2-components/ng2-alfresco-search/src/assets/alfresco-search.service.mock.ts b/ng2-components/ng2-alfresco-search/src/assets/alfresco-search.service.mock.ts new file mode 100644 index 0000000000..0f15f11a81 --- /dev/null +++ b/ng2-components/ng2-alfresco-search/src/assets/alfresco-search.service.mock.ts @@ -0,0 +1,63 @@ +/*! + * @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. + */ + +export var fakeSearch = { + list: { + pagination: { + count: 1, + hasMoreItems: false, + totalItems: 1, + skipCount: 0, + maxItems: 100 + }, + entries: [ + { + entry: { + id: '123', + name: 'MyDoc', + content: { + mimetype: 'text/plain' + }, + createdByUser: { + displayName: 'John Doe' + }, + modifiedByUser: { + displayName: 'John Doe' + } + } + } + ] + } +}; + +export var fakeError = { + error: { + errorKey: 'Search failed', + statusCode: 400, + briefSummary: '08220082 search failed', + stackTrace: 'For security reasons the stack trace is no longer displayed, but the property is kept for previous versions.', + descriptionURL: 'https://api-explorer.alfresco.com' + } +}; + +export var fakeApi = { + core: { + searchApi: { + liveSearchNodes: (term, opts) => Promise.resolve(fakeSearch) + } + } +}; diff --git a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts index 8a9d531352..56f0093492 100644 --- a/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts +++ b/ng2-components/ng2-alfresco-search/src/services/alfresco-search.service.spec.ts @@ -18,6 +18,7 @@ import { ReflectiveInjector } from '@angular/core'; import { AlfrescoSearchService } from './alfresco-search.service'; import { AlfrescoAuthenticationService, AlfrescoSettingsService, AlfrescoApiService } from 'ng2-alfresco-core'; +import { fakeApi, fakeSearch, fakeError } from '../assets/alfresco-search.service.mock'; declare let jasmine: any; @@ -27,53 +28,6 @@ describe('AlfrescoSearchService', () => { let authenticationService: AlfrescoAuthenticationService; let injector: ReflectiveInjector; - let fakeSearch = { - list: { - pagination: { - count: 1, - hasMoreItems: false, - totalItems: 1, - skipCount: 0, - maxItems: 100 - }, - entries: [ - { - entry: { - id: '123', - name: 'MyDoc', - content: { - mimetype: 'text/plain' - }, - createdByUser: { - displayName: 'John Doe' - }, - modifiedByUser: { - displayName: 'John Doe' - } - } - } - ] - } - }; - - let fakeError = { - error: { - errorKey: 'Search failed', - statusCode: 400, - briefSummary: '08220082 search failed', - stackTrace: 'For security reasons the stack trace is no longer displayed, but the property is kept for previous versions.', - descriptionURL: 'https://api-explorer.alfresco.com' - } - }; - - let fakeApi = { - core: { - searchApi: { - liveSearchNodes: (term, opts) => Promise.resolve(fakeSearch) - } - } - }; - beforeEach(() => { injector = ReflectiveInjector.resolveAndCreate([ AlfrescoSearchService, From 1ba42f0e7262eaeba486acab90fa4e2f0694a2ba Mon Sep 17 00:00:00 2001 From: Will Abson Date: Thu, 13 Oct 2016 12:54:42 +0100 Subject: [PATCH 6/6] Revert "Update search package test commands" This reverts commit fa43e54e0896eb826c1a731cb38e160578a1dbbc. --- ng2-components/ng2-alfresco-search/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ng2-components/ng2-alfresco-search/package.json b/ng2-components/ng2-alfresco-search/package.json index aee37698b8..af08568b8b 100644 --- a/ng2-components/ng2-alfresco-search/package.json +++ b/ng2-components/ng2-alfresco-search/package.json @@ -15,8 +15,8 @@ "tsc:w": "tsc -w", "pretest": "npm run build", "test": "karma start karma.conf.js --reporters mocha,coverage --single-run", - "test-browser": "concurrently \"karma start karma.conf.js --reporters kjhtml\" \"npm run watch-task\"", - "posttest": "node_modules/.bin/remap-istanbul -i coverage/report/coverage-final.json -o coverage/report -t html && node_modules/.bin/remap-istanbul -i coverage/report/coverage-final.json -o coverage/report/coverage-final.json", + "test-browser": "npm run build && concurrently \"karma start karma.conf.js --reporters kjhtml\" \"npm run watch-task\"", + "posttest": "remap-istanbul -i coverage/report/coverage-final.json -o coverage/report -t html && remap-istanbul -i coverage/report/coverage-final.json -o coverage/report/coverage-final.json", "coverage": "npm run test && wsrv -o -p 9875 ./coverage/report", "prepublish": "npm run build", "travis": "npm link ng2-alfresco-core"