Re-enable search service tests

This commit is contained in:
Will Abson
2016-10-12 12:08:48 +01:00
parent ae54a127e0
commit 78a0253ebb
4 changed files with 61 additions and 55 deletions

View File

@@ -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 = <any> 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) => {

View File

@@ -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;

View File

@@ -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();
}
);
});
});
*/

View File

@@ -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<any> {
// 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');
}
}