Re-add search control tests working for final NG 2.0.0

Refs #737
This commit is contained in:
Will Abson
2016-10-03 12:51:51 +01:00
parent 420b11449c
commit 28783322fd
5 changed files with 163 additions and 155 deletions

View File

@@ -15,10 +15,7 @@
* limitations under the License.
*/
/*
import { it, describe, expect, inject, beforeEachProviders, beforeEach, afterEach } from '@angular/core/testing';
import { PLATFORM_PIPES } from '@angular/core';
import { TestComponentBuilder } from '@angular/compiler/testing';
import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { AlfrescoSearchAutocompleteComponent } from './alfresco-search-autocomplete.component';
import { AlfrescoThumbnailService } from './../services/alfresco-thumbnail.service';
import { TranslationMock } from './../assets/translation.service.mock';
@@ -29,14 +26,13 @@ import {
AlfrescoAuthenticationService,
AlfrescoContentService,
AlfrescoTranslationService,
AlfrescoPipeTranslate
CoreModule
} from 'ng2-alfresco-core';
declare let jasmine: any;
describe('AlfrescoSearchAutocompleteComponent', () => {
let alfrescoSearchComponentFixture, element, component;
let alfrescoSearchComponentFixture: ComponentFixture<AlfrescoSearchAutocompleteComponent>, element: HTMLElement;
let component: AlfrescoSearchAutocompleteComponent;
let result = {
list: {
@@ -98,47 +94,41 @@ describe('AlfrescoSearchAutocompleteComponent', () => {
}
};
beforeEachProviders(() => {
return [
{ provide: PLATFORM_PIPES, useValue: AlfrescoPipeTranslate, multi: true },
{provide: AlfrescoTranslationService, useClass: TranslationMock},
AlfrescoThumbnailService,
AlfrescoSettingsService,
AlfrescoApiService,
AlfrescoAuthenticationService,
AlfrescoContentService,
AlfrescoSearchService
];
});
beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb
.createAsync(AlfrescoSearchAutocompleteComponent)
.then(fixture => {
jasmine.Ajax.install();
alfrescoSearchComponentFixture = fixture;
element = alfrescoSearchComponentFixture.nativeElement;
component = alfrescoSearchComponentFixture.componentInstance;
});
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
CoreModule
],
declarations: [ AlfrescoSearchAutocompleteComponent ], // declare the test component
providers: [
{provide: AlfrescoTranslationService, useClass: TranslationMock},
AlfrescoThumbnailService,
AlfrescoSettingsService,
AlfrescoApiService,
AlfrescoAuthenticationService,
AlfrescoContentService,
AlfrescoSearchService
]
}).compileComponents().then(() => {
alfrescoSearchComponentFixture = TestBed.createComponent(AlfrescoSearchAutocompleteComponent);
component = alfrescoSearchComponentFixture.componentInstance;
element = alfrescoSearchComponentFixture.nativeElement;
});
}));
afterEach(() => {
jasmine.Ajax.uninstall();
});
it('should setup i18n folder', () => {
let translation = jasmine.createSpyObj('AlfrescoTranslationService', [
'addTranslationFolder'
]);
let search = new AlfrescoSearchAutocompleteComponent(null, translation, null);
expect(search).toBeDefined();
});
// it('should setup i18n folder', () => {
// let translation = jasmine.createSpyObj('AlfrescoTranslationService', [
// 'addTranslationFolder'
// ]);
// let search = new AlfrescoSearchAutocompleteComponent(null, translation, null);
// expect(search).toBeDefined();
//
// });
it('should display search results when a search term is provided', () => {
let searchTerm = { currentValue: 'customSearchTerm', previousValue: ''};
spyOn(component, 'displaySearchResults').and.stub();
component.searchTerm = searchTerm;
component.searchTerm = 'searchTerm';
component.ngOnChanges({
searchTerm: searchTerm
});
@@ -147,6 +137,11 @@ describe('AlfrescoSearchAutocompleteComponent', () => {
});
it('should display the returned search results', (done) => {
let searchService = alfrescoSearchComponentFixture.debugElement.injector.get(AlfrescoSearchService);
spyOn(searchService, 'getSearchNodesPromise')
.and.returnValue(Promise.resolve(result));
component.resultsEmitter.subscribe(x => {
alfrescoSearchComponentFixture.detectChanges();
expect( element.querySelector('#result_user_0').innerHTML).toBe('John Doe');
@@ -154,93 +149,84 @@ describe('AlfrescoSearchAutocompleteComponent', () => {
done();
});
component.searchTerm = { currentValue: 'searchTerm', previousValue: ''};
component.ngOnChanges({searchTerm: component.searchTerm });
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: result
});
component.searchTerm = 'searchTerm';
component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''} });
});
it('should display the correct thumbnail for result items', (done) => {
let searchService = alfrescoSearchComponentFixture.debugElement.injector.get(AlfrescoSearchService);
spyOn(searchService, 'getSearchNodesPromise')
.and.returnValue(Promise.resolve(result));
component.baseComponentPath = 'http://localhost';
spyOn(component.alfrescoThumbnailService, 'getMimeTypeIcon').and.returnValue('fake-type-icon.svg');
spyOn(component.alfrescoThumbnailService, 'getMimeTypeKey').and.returnValue('FAKE_TYPE');
let thumbnailService = alfrescoSearchComponentFixture.debugElement.injector.get(AlfrescoThumbnailService);
spyOn(thumbnailService, 'getMimeTypeIcon').and.returnValue('fake-type-icon.svg');
spyOn(thumbnailService, 'getMimeTypeKey').and.returnValue('FAKE_TYPE');
component.resultsEmitter.subscribe(() => {
alfrescoSearchComponentFixture.detectChanges();
let imgEl = element.querySelector('#result_row_0 img');
let imgEl = <any> element.querySelector('#result_row_0 img');
expect(imgEl).not.toBeNull();
expect(imgEl.src).toBe('http://localhost/img/fake-type-icon.svg');
expect(imgEl.alt).toBe('SEARCH.ICONS.FAKE_TYPE');
done();
});
component.searchTerm = { currentValue: 'searchTerm', previousValue: ''};
component.ngOnChanges({searchTerm: component.searchTerm });
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: result
});
component.searchTerm = 'searchTerm';
component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''} });
});
it('should display no result if no result are returned', (done) => {
let searchService = alfrescoSearchComponentFixture.debugElement.injector.get(AlfrescoSearchService);
spyOn(searchService, 'getSearchNodesPromise')
.and.returnValue(Promise.resolve(noResult));
component.resultsEmitter.subscribe(x => {
alfrescoSearchComponentFixture.detectChanges();
expect(element.querySelector('#search_no_result')).not.toBeNull();
done();
});
component.searchTerm = { currentValue: 'searchTerm', previousValue: ''};
component.ngOnChanges({searchTerm: component.searchTerm});
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: noResult
});
component.searchTerm = 'searchTerm';
component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''}});
});
it('should display an error if an error is encountered running the search', (done) => {
let searchService = alfrescoSearchComponentFixture.debugElement.injector.get(AlfrescoSearchService);
spyOn(searchService, 'getSearchNodesPromise')
.and.returnValue(Promise.reject(errorJson));
component.errorEmitter.subscribe(() => {
alfrescoSearchComponentFixture.detectChanges();
let resultsEl = element.querySelector('[data-automation-id="autocomplete_results"]');
let errorEl = element.querySelector('[data-automation-id="autocomplete_error_message"]');
let errorEl = <any> element.querySelector('[data-automation-id="autocomplete_error_message"]');
expect(resultsEl).toBeNull();
expect(errorEl).not.toBeNull();
expect(errorEl.innerText).toBe('SEARCH.RESULTS.ERROR');
done();
});
component.searchTerm = { currentValue: 'searchTerm', previousValue: ''};
component.ngOnChanges({searchTerm: component.searchTerm});
jasmine.Ajax.requests.mostRecent().respondWith({
status: 500,
contentType: 'json',
responseText: errorJson
});
component.searchTerm = 'searchTerm';
component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''}});
});
it('should emit preview when file item clicked', (done) => {
let searchService = alfrescoSearchComponentFixture.debugElement.injector.get(AlfrescoSearchService);
spyOn(searchService, 'getSearchNodesPromise')
.and.returnValue(Promise.resolve(result));
component.resultsEmitter.subscribe(x => {
alfrescoSearchComponentFixture.detectChanges();
element.querySelector('#result_row_0').click();
(<any> element.querySelector('#result_row_0')).click();
});
component.searchTerm = { currentValue: 'searchTerm', previousValue: ''};
component.ngOnChanges({searchTerm: component.searchTerm});
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: result
});
component.searchTerm = 'searchTerm';
component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''}});
component.preview.subscribe(e => {
done();
@@ -248,23 +234,21 @@ describe('AlfrescoSearchAutocompleteComponent', () => {
});
it('should not emit preview if a non-file item is clicked', (done) => {
let searchService = alfrescoSearchComponentFixture.debugElement.injector.get(AlfrescoSearchService);
spyOn(searchService, 'getSearchNodesPromise')
.and.returnValue(Promise.resolve(folderResult));
spyOn(component.preview, 'emit');
component.resultsEmitter.subscribe(x => {
alfrescoSearchComponentFixture.detectChanges();
element.querySelector('#result_row_0').click();
(<any> element.querySelector('#result_row_0')).click();
expect(component.preview.emit).not.toHaveBeenCalled();
done();
});
component.searchTerm = { currentValue: 'searchTerm', previousValue: ''};
component.ngOnChanges({searchTerm: component.searchTerm});
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: folderResult
});
component.searchTerm = 'searchTerm';
component.ngOnChanges({searchTerm: { currentValue: 'searchTerm', previousValue: ''}});
});
});
*/