refactor(aio): move SearchService initialisation into SearchBoxComponent

The AppComponent really doesn't need to know about the search service.
This commit is contained in:
Peter Bacon Darwin 2017-03-26 11:11:45 +01:00 committed by Pete Bacon Darwin
parent a6fd22c399
commit 228238e602
4 changed files with 10 additions and 17 deletions

View File

@ -4,11 +4,9 @@ import { By } from '@angular/platform-browser';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import { GaService } from 'app/shared/ga.service'; import { GaService } from 'app/shared/ga.service';
import { SearchService } from 'app/search/search.service';
import { SearchResultsComponent } from 'app/search/search-results/search-results.component'; import { SearchResultsComponent } from 'app/search/search-results/search-results.component';
import { SearchBoxComponent } from 'app/search/search-box/search-box.component'; import { SearchBoxComponent } from 'app/search/search-box/search-box.component';
import { AutoScrollService } from 'app/shared/auto-scroll.service'; import { AutoScrollService } from 'app/shared/auto-scroll.service';
import { MockSearchService } from 'testing/search.service';
import { LocationService } from 'app/shared/location.service'; import { LocationService } from 'app/shared/location.service';
import { MockLocationService } from 'testing/location.service'; import { MockLocationService } from 'testing/location.service';
import { Logger } from 'app/shared/logger.service'; import { Logger } from 'app/shared/logger.service';
@ -24,7 +22,6 @@ describe('AppComponent', () => {
imports: [ AppModule ], imports: [ AppModule ],
providers: [ providers: [
{ provide: APP_BASE_HREF, useValue: '/' }, { provide: APP_BASE_HREF, useValue: '/' },
{ provide: SearchService, useClass: MockSearchService },
{ provide: GaService, useClass: TestGaService }, { provide: GaService, useClass: TestGaService },
{ provide: LocationService, useFactory: () => new MockLocationService(initialUrl) }, { provide: LocationService, useFactory: () => new MockLocationService(initialUrl) },
{ provide: Logger, useClass: MockLogger } { provide: Logger, useClass: MockLogger }
@ -92,14 +89,6 @@ describe('AppComponent', () => {
}); });
}); });
describe('initialisation', () => {
it('should initialize the search worker', inject([SearchService], (searchService: SearchService) => {
fixture.detectChanges(); // triggers ngOnInit
expect(searchService.initWorker).toHaveBeenCalled();
expect(searchService.loadIndex).toHaveBeenCalled();
}));
});
describe('click intercepting', () => { describe('click intercepting', () => {
it('should intercept clicks on anchors and call `location.handleAnchorClick()`', it('should intercept clicks on anchors and call `location.handleAnchorClick()`',
inject([LocationService], (location: LocationService) => { inject([LocationService], (location: LocationService) => {

View File

@ -6,7 +6,6 @@ import { LocationService } from 'app/shared/location.service';
import { DocumentService, DocumentContents } from 'app/documents/document.service'; import { DocumentService, DocumentContents } from 'app/documents/document.service';
import { DocViewerComponent } from 'app/layout/doc-viewer/doc-viewer.component'; import { DocViewerComponent } from 'app/layout/doc-viewer/doc-viewer.component';
import { NavigationService, NavigationViews, NavigationNode, VersionInfo } from 'app/navigation/navigation.service'; import { NavigationService, NavigationViews, NavigationNode, VersionInfo } from 'app/navigation/navigation.service';
import { SearchService } from 'app/search/search.service';
import { SearchResultsComponent } from 'app/search/search-results/search-results.component'; import { SearchResultsComponent } from 'app/search/search-results/search-results.component';
import { AutoScrollService } from 'app/shared/auto-scroll.service'; import { AutoScrollService } from 'app/shared/auto-scroll.service';
@ -40,8 +39,7 @@ export class AppComponent implements OnInit {
gaService: GaService, gaService: GaService,
navigationService: NavigationService, navigationService: NavigationService,
private autoScroll: AutoScrollService, private autoScroll: AutoScrollService,
private locationService: LocationService, private locationService: LocationService) {
private searchService: SearchService) {
this.currentDocument = documentService.currentDocument; this.currentDocument = documentService.currentDocument;
locationService.currentUrl.subscribe(url => gaService.locationChanged(url)); locationService.currentUrl.subscribe(url => gaService.locationChanged(url));
this.navigationViews = navigationService.navigationViews; this.navigationViews = navigationService.navigationViews;
@ -50,9 +48,6 @@ export class AppComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.searchService.initWorker('app/search/search-worker.js');
this.searchService.loadIndex();
this.onResize(window.innerWidth); this.onResize(window.innerWidth);
// The url changed, so scroll to the anchor in the hash fragment. // The url changed, so scroll to the anchor in the hash fragment.

View File

@ -30,6 +30,12 @@ describe('SearchBoxComponent', () => {
}); });
describe('initialisation', () => { describe('initialisation', () => {
it('should initialize the search worker', inject([SearchService], (searchService: SearchService) => {
fixture.detectChanges(); // triggers ngOnInit
expect(searchService.initWorker).toHaveBeenCalled();
expect(searchService.loadIndex).toHaveBeenCalled();
}));
it('should get the current search query from the location service', inject([LocationService], (location: MockLocationService) => { it('should get the current search query from the location service', inject([LocationService], (location: MockLocationService) => {
location.search.and.returnValue({ search: 'initial search' }); location.search.and.returnValue({ search: 'initial search' });
spyOn(component, 'onSearch'); spyOn(component, 'onSearch');

View File

@ -25,6 +25,9 @@ export class SearchBoxComponent implements OnInit {
constructor(private searchService: SearchService, private locationService: LocationService) { } constructor(private searchService: SearchService, private locationService: LocationService) { }
ngOnInit() { ngOnInit() {
this.searchService.initWorker('app/search/search-worker.js');
this.searchService.loadIndex();
const query = this.locationService.search()['search']; const query = this.locationService.search()['search'];
if (query) { if (query) {
this.searchBox.nativeElement.value = query; this.searchBox.nativeElement.value = query;