From d0d7765af313885c1141cd6423cdbb62098c40d9 Mon Sep 17 00:00:00 2001 From: Cilibiu Bogdan Date: Fri, 8 Mar 2019 11:39:16 +0200 Subject: [PATCH] [ACA] Search - show error message on error (#1003) * show error message * remove fdescribe --- .../search-results.component.spec.ts | 41 +++++++++++++++++-- .../search-results.component.ts | 36 +++++++++++++++- src/assets/i18n/en.json | 6 ++- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/app/components/search/search-results/search-results.component.spec.ts b/src/app/components/search/search-results/search-results.component.spec.ts index 6481c785b..c8c5936cf 100644 --- a/src/app/components/search/search-results/search-results.component.spec.ts +++ b/src/app/components/search/search-results/search-results.component.spec.ts @@ -1,11 +1,22 @@ -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { + async, + ComponentFixture, + TestBed, + fakeAsync, + tick +} from '@angular/core/testing'; import { SearchResultsComponent } from './search-results.component'; import { AppTestingModule } from '../../../testing/app-testing.module'; import { AppSearchResultsModule } from '../search-results.module'; -import { CoreModule, AppConfigService } from '@alfresco/adf-core'; +import { + CoreModule, + AppConfigService, + AlfrescoApiService, + AlfrescoApiServiceMock +} from '@alfresco/adf-core'; import { Store } from '@ngrx/store'; -import { NavigateToFolder } from '../../../store/actions'; +import { NavigateToFolder, SnackbarErrorAction } from '../../../store/actions'; import { Pagination } from '@alfresco/js-api'; import { SearchQueryBuilderService } from '@alfresco/adf-content-services'; import { ActivatedRoute } from '@angular/router'; @@ -16,11 +27,16 @@ describe('SearchComponent', () => { let config: AppConfigService; let store: Store; let queryBuilder: SearchQueryBuilderService; + let alfrescoApi: AlfrescoApiService; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [CoreModule.forRoot(), AppTestingModule, AppSearchResultsModule], providers: [ + { + provide: AlfrescoApiService, + useClass: AlfrescoApiServiceMock + }, { provide: ActivatedRoute, useValue: { @@ -43,6 +59,7 @@ describe('SearchComponent', () => { config = TestBed.get(AppConfigService); store = TestBed.get(Store); queryBuilder = TestBed.get(SearchQueryBuilderService); + alfrescoApi = TestBed.get(AlfrescoApiService); fixture = TestBed.createComponent(SearchResultsComponent); component = fixture.componentInstance; @@ -52,6 +69,24 @@ describe('SearchComponent', () => { fixture.detectChanges(); })); + it('should raise an error if search fails', fakeAsync(() => { + spyOn(alfrescoApi.searchApi, 'search').and.returnValue( + Promise.reject({ + message: `{ "error": { "statusCode": 500 } } ` + }) + ); + + spyOn(queryBuilder, 'buildQuery').and.returnValue({}); + spyOn(store, 'dispatch').and.stub(); + + queryBuilder.execute(); + tick(); + + expect(store.dispatch).toHaveBeenCalledWith( + new SnackbarErrorAction('APP.BROWSE.SEARCH.ERRORS.500') + ); + })); + it('should decode encoded URI', () => { expect(queryBuilder.userQuery).toEqual( '(TYPE: "cm:folder" AND (=cm: name: email OR cm: name: budget))' diff --git a/src/app/components/search/search-results/search-results.component.ts b/src/app/components/search/search-results/search-results.component.ts index 8b388b1d5..80c798134 100644 --- a/src/app/components/search/search-results/search-results.component.ts +++ b/src/app/components/search/search-results/search-results.component.ts @@ -37,9 +37,10 @@ import { AppStore } from '../../../store/states/app.state'; import { NavigateToFolder } from '../../../store/actions'; import { AppExtensionService } from '../../../extensions/extension.service'; import { ContentManagementService } from '../../../services/content-management.service'; -import { AppConfigService } from '@alfresco/adf-core'; -import { Observable } from 'rxjs'; +import { AppConfigService, AlfrescoApiService } from '@alfresco/adf-core'; +import { Observable, Subject } from 'rxjs'; import { showFacetFilter } from '../../../store/selectors/app.selectors'; +import { SnackbarErrorAction } from '../../../store/actions'; @Component({ selector: 'aca-search-results', @@ -62,8 +63,10 @@ export class SearchResultsComponent extends PageComponent implements OnInit { hasSelectedFilters = false; sorting = ['name', 'asc']; isLoading = false; + searchQueryError: Subject = new Subject(); constructor( + private alfrescoApiService: AlfrescoApiService, private queryBuilder: SearchQueryBuilderService, private route: ActivatedRoute, private config: AppConfigService, @@ -84,6 +87,25 @@ export class SearchResultsComponent extends PageComponent implements OnInit { ngOnInit() { super.ngOnInit(); + // todo: remove once ADF-4193 is resolved + this.queryBuilder.execute = async () => { + const query = this.queryBuilder.buildQuery(); + if (query) { + try { + const response = await this.alfrescoApiService.searchApi.search( + query + ); + this.queryBuilder.executed.next(response); + } catch (error) { + this.searchQueryError.next(error); + + this.queryBuilder.executed.next({ + list: { pagination: { totalItems: 0 }, entries: [] } + }); + } + } + }; + this.sorting = this.getSorting(); this.subscriptions.push( @@ -97,6 +119,16 @@ export class SearchResultsComponent extends PageComponent implements OnInit { this.onSearchResultLoaded(data); this.isLoading = false; + }), + + this.searchQueryError.subscribe(error => { + const { statusCode } = JSON.parse(error.message).error; + + this.store.dispatch( + new SnackbarErrorAction( + `APP.BROWSE.SEARCH.ERRORS.${statusCode || 'GENERIC'}` + ) + ); }) ); diff --git a/src/assets/i18n/en.json b/src/assets/i18n/en.json index 61b297b58..6c0e04755 100644 --- a/src/assets/i18n/en.json +++ b/src/assets/i18n/en.json @@ -169,7 +169,11 @@ }, "UNKNOWN_LOCATION": "Unknown", "NO_RESULTS": "Your search returned 0 results", - "TOGGLE_SEARCH_FILTER": "Toggle search filter" + "TOGGLE_SEARCH_FILTER": "Toggle search filter", + "ERRORS": { + "500": "There was an error processing the search query [500]", + "GENERIC": "There was an error processing the search query" + } }, "SEARCH_LIBRARIES": { "TITLE": "Libraries found...",